--- 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" --- a/crates/jrsonnet-evaluator/Cargo.toml +++ b/crates/jrsonnet-evaluator/Cargo.toml @@ -12,6 +12,8 @@ default = ["serialized-stdlib", "faster", "explaining-traces"] # Serializes standard library AST instead of parsing them every run serialized-stdlib = ["serde", "bincode", "jrsonnet-parser/deserialize"] +# Allow to convert Val into serde_json::Value and backwards +serde-json = ["serde", "serde_json"] # Same as above, but with generated code instead of serde. Reduces memory usage, but increases binary size and compilation time codegenerated-stdlib = [] # Replace some standard library functions with faster implementations (I.e manifestJsonEx) @@ -29,19 +31,24 @@ pathdiff = "0.2.0" closure = "0.3.0" -indexmap = "1.5.0" +indexmap = "1.5.1" md5 = "0.7.0" base64 = "0.12.3" # Serialized stdlib [dependencies.serde] -version = "1.0.114" +version = "1.0.115" optional = true [dependencies.bincode] version = "1.3.1" optional = true +# Serde json +[dependencies.serde_json] +version = "1.0.57" +optional = true + # Explaining traces [dependencies.annotate-snippets] version = "0.9.0" @@ -51,5 +58,5 @@ jrsonnet-parser = { path = "../jrsonnet-parser", features = ["dump", "serialize", "deserialize"], version = "0.3.0" } jrsonnet-stdlib = { path = "../jrsonnet-stdlib", version = "0.3.0" } structdump = "0.1.2" -serde = "1.0.114" +serde = "1.0.115" bincode = "1.3.1" --- /dev/null +++ b/crates/jrsonnet-evaluator/src/integrations/mod.rs @@ -0,0 +1,2 @@ +#[cfg(feature = "serde-json")] +pub mod serde; --- /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 { + 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))) + } + } + } +} --- 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;