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
before · crates/jrsonnet-evaluator/Cargo.toml
1[package]2name = "jrsonnet-evaluator"3description = "jsonnet interpreter"4version = "0.3.0"5authors = ["Лач <iam@lach.pw>"]6license = "MIT"7edition = "2018"89# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html1011[features]12default = ["serialized-stdlib", "faster", "explaining-traces"]13# Serializes standard library AST instead of parsing them every run14serialized-stdlib = ["serde", "bincode", "jrsonnet-parser/deserialize"]15# Same as above, but with generated code instead of serde. Reduces memory usage, but increases binary size and compilation time16codegenerated-stdlib = []17# Replace some standard library functions with faster implementations (I.e manifestJsonEx)18# Library works fine without this feature, but requires more memory and time for std function calls19faster = []20# Rustc-like trace visualization21explaining-traces = ["annotate-snippets"]2223# Unlocks extra features, but works only on unstable24unstable = []2526[dependencies]27jrsonnet-parser = { path = "../jrsonnet-parser", version = "0.3.0" }28jrsonnet-stdlib = { path = "../jrsonnet-stdlib", version = "0.3.0" }29pathdiff = "0.2.0"3031closure = "0.3.0"32indexmap = "1.5.0"3334md5 = "0.7.0"35base64 = "0.12.3"3637# Serialized stdlib38[dependencies.serde]39version = "1.0.114"40optional = true41[dependencies.bincode]42version = "1.3.1"43optional = true4445# Explaining traces46[dependencies.annotate-snippets]47version = "0.9.0"48optional = true4950[build-dependencies]51jrsonnet-parser = { path = "../jrsonnet-parser", features = ["dump", "serialize", "deserialize"], version = "0.3.0" }52jrsonnet-stdlib = { path = "../jrsonnet-stdlib", version = "0.3.0" }53structdump = "0.1.2"54serde = "1.0.114"55bincode = "1.3.1"
after · crates/jrsonnet-evaluator/Cargo.toml
1[package]2name = "jrsonnet-evaluator"3description = "jsonnet interpreter"4version = "0.3.0"5authors = ["Лач <iam@lach.pw>"]6license = "MIT"7edition = "2018"89# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html1011[features]12default = ["serialized-stdlib", "faster", "explaining-traces"]13# Serializes standard library AST instead of parsing them every run14serialized-stdlib = ["serde", "bincode", "jrsonnet-parser/deserialize"]15# Allow to convert Val into serde_json::Value and backwards16serde-json = ["serde", "serde_json"]17# Same as above, but with generated code instead of serde. Reduces memory usage, but increases binary size and compilation time18codegenerated-stdlib = []19# Replace some standard library functions with faster implementations (I.e manifestJsonEx)20# Library works fine without this feature, but requires more memory and time for std function calls21faster = []22# Rustc-like trace visualization23explaining-traces = ["annotate-snippets"]2425# Unlocks extra features, but works only on unstable26unstable = []2728[dependencies]29jrsonnet-parser = { path = "../jrsonnet-parser", version = "0.3.0" }30jrsonnet-stdlib = { path = "../jrsonnet-stdlib", version = "0.3.0" }31pathdiff = "0.2.0"3233closure = "0.3.0"34indexmap = "1.5.1"3536md5 = "0.7.0"37base64 = "0.12.3"3839# Serialized stdlib40[dependencies.serde]41version = "1.0.115"42optional = true43[dependencies.bincode]44version = "1.3.1"45optional = true4647# Serde json48[dependencies.serde_json]49version = "1.0.57"50optional = true5152# Explaining traces53[dependencies.annotate-snippets]54version = "0.9.0"55optional = true5657[build-dependencies]58jrsonnet-parser = { path = "../jrsonnet-parser", features = ["dump", "serialize", "deserialize"], version = "0.3.0" }59jrsonnet-stdlib = { path = "../jrsonnet-stdlib", version = "0.3.0" }60structdump = "0.1.2"61serde = "1.0.115"62bincode = "1.3.1"
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;