git.delta.rocks / jrsonnet / refs/commits / 409a660d0753

difftreelog

feat(evaluator) serde_json integration

Lach2020-08-15parent: #d445938.patch.diff
in: master

5 files changed

modifiedCargo.lockdiffbeforeafterboth
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -125,15 +125,21 @@
 
 [[package]]
 name = "indexmap"
-version = "1.5.0"
+version = "1.5.1"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "5b88cd59ee5f71fea89a62248fc8f387d44400cefe05ef548466d61ced9029a7"
+checksum = "86b45e59b16c76b11bf9738fd5d38879d3bd28ad292d7b313608becb17ae2df9"
 dependencies = [
  "autocfg",
  "hashbrown",
 ]
 
 [[package]]
+name = "itoa"
+version = "0.4.6"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "dc6f3ad7b9d11a0c00842ff8de1b60ee58661048eb8049ed33c73594f359d7e6"
+
+[[package]]
 name = "jrsonnet"
 version = "0.3.0"
 dependencies = [
@@ -167,6 +173,7 @@
  "md5",
  "pathdiff",
  "serde",
+ "serde_json",
  "structdump",
 ]
 
@@ -315,19 +322,25 @@
 ]
 
 [[package]]
+name = "ryu"
+version = "1.0.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "71d301d4193d031abdd79ff7e3dd721168a9572ef3fe51a1517aba235bd8f86e"
+
+[[package]]
 name = "serde"
-version = "1.0.114"
+version = "1.0.115"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "5317f7588f0a5078ee60ef675ef96735a1442132dc645eb1d12c018620ed8cd3"
+checksum = "e54c9a88f2da7238af84b5101443f0c0d0a3bbdc455e34a5c9497b1903ed55d5"
 dependencies = [
  "serde_derive",
 ]
 
 [[package]]
 name = "serde_derive"
-version = "1.0.114"
+version = "1.0.115"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "2a0be94b04690fbaed37cddffc5c134bf537c8e3329d53e982fe04c374978f8e"
+checksum = "609feed1d0a73cc36a0182a840a9b37b4a82f0b1150369f0536a9e3f2a31dc48"
 dependencies = [
  "proc-macro2",
  "quote",
@@ -335,6 +348,17 @@
 ]
 
 [[package]]
+name = "serde_json"
+version = "1.0.57"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "164eacbdb13512ec2745fb09d51fd5b22b0d65ed294a1dcf7285a360c80a675c"
+dependencies = [
+ "itoa",
+ "ryu",
+ "serde",
+]
+
+[[package]]
 name = "strsim"
 version = "0.10.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
modifiedcrates/jrsonnet-evaluator/Cargo.tomldiffbeforeafterboth
12default = ["serialized-stdlib", "faster", "explaining-traces"]12default = ["serialized-stdlib", "faster", "explaining-traces"]
13# Serializes standard library AST instead of parsing them every run13# Serializes standard library AST instead of parsing them every run
14serialized-stdlib = ["serde", "bincode", "jrsonnet-parser/deserialize"]14serialized-stdlib = ["serde", "bincode", "jrsonnet-parser/deserialize"]
15# Allow to convert Val into serde_json::Value and backwards
16serde-json = ["serde", "serde_json"]
15# Same as above, but with generated code instead of serde. Reduces memory usage, but increases binary size and compilation time17# Same as above, but with generated code instead of serde. Reduces memory usage, but increases binary size and compilation time
16codegenerated-stdlib = []18codegenerated-stdlib = []
17# Replace some standard library functions with faster implementations (I.e manifestJsonEx)19# Replace some standard library functions with faster implementations (I.e manifestJsonEx)
29pathdiff = "0.2.0"31pathdiff = "0.2.0"
3032
31closure = "0.3.0"33closure = "0.3.0"
32indexmap = "1.5.0"34indexmap = "1.5.1"
3335
34md5 = "0.7.0"36md5 = "0.7.0"
35base64 = "0.12.3"37base64 = "0.12.3"
3638
37# Serialized stdlib39# Serialized stdlib
38[dependencies.serde]40[dependencies.serde]
39version = "1.0.114"41version = "1.0.115"
40optional = true42optional = true
41[dependencies.bincode]43[dependencies.bincode]
42version = "1.3.1"44version = "1.3.1"
43optional = true45optional = true
46
47# Serde json
48[dependencies.serde_json]
49version = "1.0.57"
50optional = true
4451
45# Explaining traces52# Explaining traces
46[dependencies.annotate-snippets]53[dependencies.annotate-snippets]
51jrsonnet-parser = { path = "../jrsonnet-parser", features = ["dump", "serialize", "deserialize"], version = "0.3.0" }58jrsonnet-parser = { path = "../jrsonnet-parser", features = ["dump", "serialize", "deserialize"], version = "0.3.0" }
52jrsonnet-stdlib = { path = "../jrsonnet-stdlib", version = "0.3.0" }59jrsonnet-stdlib = { path = "../jrsonnet-stdlib", version = "0.3.0" }
53structdump = "0.1.2"60structdump = "0.1.2"
54serde = "1.0.114"61serde = "1.0.115"
55bincode = "1.3.1"62bincode = "1.3.1"
5663
addedcrates/jrsonnet-evaluator/src/integrations/mod.rsdiffbeforeafterboth
--- /dev/null
+++ b/crates/jrsonnet-evaluator/src/integrations/mod.rs
@@ -0,0 +1,2 @@
+#[cfg(feature = "serde-json")]
+pub mod serde;
addedcrates/jrsonnet-evaluator/src/integrations/serde.rsdiffbeforeafterboth
--- /dev/null
+++ b/crates/jrsonnet-evaluator/src/integrations/serde.rs
@@ -0,0 +1,77 @@
+use crate::{
+	error::{Error::*, LocError, Result},
+	throw, LazyBinding, LazyVal, ObjMember, ObjValue, Val,
+};
+use jrsonnet_parser::Visibility;
+use serde_json::{Map, Number, Value};
+use std::{
+	collections::HashMap,
+	convert::{TryFrom, TryInto},
+	rc::Rc,
+};
+
+impl TryFrom<&Val> for Value {
+	type Error = LocError;
+	fn try_from(v: &Val) -> Result<Self> {
+		Ok(match v {
+			Val::Bool(b) => Value::Bool(*b),
+			Val::Null => Value::Null,
+			Val::Str(s) => Value::String((&s as &str).into()),
+			Val::Num(n) => Value::Number(Number::from_f64(*n).expect("to json number")),
+			Val::Lazy(v) => (&v.evaluate()?).try_into()?,
+			Val::Arr(a) => {
+				let mut out = Vec::with_capacity(a.len());
+				for item in a.iter() {
+					out.push(item.try_into()?);
+				}
+				Value::Array(out)
+			}
+			Val::Obj(o) => {
+				let mut out = Map::new();
+				for key in o.visible_fields() {
+					out.insert(
+						(&key as &str).into(),
+						(&o.get(key)?.expect("field exists")).try_into()?,
+					);
+				}
+				Value::Object(out)
+			}
+			Val::Func(_) | Val::Intristic(_, _) => {
+				throw!(RuntimeError("tried to manifest function".into()))
+			}
+		})
+	}
+}
+
+impl From<&Value> for Val {
+	fn from(v: &Value) -> Self {
+		match v {
+			Value::Null => Val::Null,
+			Value::Bool(v) => Val::Bool(*v),
+			Value::Number(n) => Val::Num(n.as_f64().expect("as f64")),
+			Value::String(s) => Val::Str((s as &str).into()),
+			Value::Array(a) => {
+				let mut out = Vec::with_capacity(a.len());
+				for v in a {
+					out.push(v.into());
+				}
+				Val::Arr(Rc::new(out))
+			}
+			Value::Object(o) => {
+				let mut entries = HashMap::with_capacity(o.len());
+				for (k, v) in o {
+					entries.insert(
+						(k as &str).into(),
+						ObjMember {
+							add: false,
+							visibility: Visibility::Normal,
+							invoke: LazyBinding::Bound(LazyVal::new_resolved(v.into())),
+							location: None,
+						},
+					);
+				}
+				Val::Obj(ObjValue::new(None, Rc::new(entries)))
+			}
+		}
+	}
+}
modifiedcrates/jrsonnet-evaluator/src/lib.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/lib.rs
+++ b/crates/jrsonnet-evaluator/src/lib.rs
@@ -8,6 +8,7 @@
 mod evaluate;
 mod function;
 mod import;
+mod integrations;
 mod map;
 mod obj;
 pub mod trace;