git.delta.rocks / jrsonnet / refs/commits / 4a1c9d0032c1

difftreelog

fix(evaluator) add missing intristics: char, encodeUTF8, md5

Лач2020-06-26parent: #a5ca2ae.patch.diff
in: master

3 files changed

modifiedcrates/jsonnet-evaluator/Cargo.tomldiffbeforeafterboth
before · crates/jsonnet-evaluator/Cargo.toml
1[package]2name = "jsonnet-evaluator"3version = "0.1.0"4authors = ["Лач <iam@lach.pw>"]5edition = "2018"67# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html89[features]10default = ["serialized-stdlib", "faster"]11serialized-stdlib = ["serde", "bincode"]12# Replace some standard library functions with faster implementations13# Library works fine without this feature, but requires more memory and time for std function calls14faster = []1516[dependencies]17jsonnet-parser = { path = "../jsonnet-parser" }18closure = "0.3.0"19jsonnet-stdlib = { path = "../jsonnet-stdlib" }20indexmap = "1.4.0"2122[dependencies.serde]23version = "1.0.114"24optional = true2526[dependencies.bincode]27version = "1.3.1"28optional = true2930[build-dependencies]31jsonnet-parser = { path = "../jsonnet-parser" }32jsonnet-stdlib = { path = "../jsonnet-stdlib" }33serde = "1.0.114"34bincode = "1.3.1"
after · crates/jsonnet-evaluator/Cargo.toml
1[package]2name = "jsonnet-evaluator"3version = "0.1.0"4authors = ["Лач <iam@lach.pw>"]5edition = "2018"67# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html89[features]10default = ["serialized-stdlib", "faster"]11serialized-stdlib = ["serde", "bincode"]12# Replace some standard library functions with faster implementations13# Library works fine without this feature, but requires more memory and time for std function calls14faster = []1516[dependencies]17jsonnet-parser = { path = "../jsonnet-parser" }18closure = "0.3.0"19jsonnet-stdlib = { path = "../jsonnet-stdlib" }20indexmap = "1.4.0"21md5 = "0.7.0"2223[dependencies.serde]24version = "1.0.114"25optional = true2627[dependencies.bincode]28version = "1.3.1"29optional = true3031[build-dependencies]32jsonnet-parser = { path = "../jsonnet-parser" }33jsonnet-stdlib = { path = "../jsonnet-stdlib" }34serde = "1.0.114"35bincode = "1.3.1"
modifiedcrates/jsonnet-evaluator/src/evaluate.rsdiffbeforeafterboth
--- a/crates/jsonnet-evaluator/src/evaluate.rs
+++ b/crates/jsonnet-evaluator/src/evaluate.rs
@@ -682,6 +682,29 @@
 							panic!("bad filter call");
 						}
 					}
+					("std", "char") => {
+						assert_eq!(args.len(), 1);
+						let ch = evaluate(context, &args[0].1)?
+							.unwrap_if_lazy()?
+							.try_cast_num("std.char first argument")?;
+						let mut out = String::new();
+						out.push(std::char::from_u32(ch as u32).unwrap());
+						Val::Str(out.into())
+					}
+					("std", "encodeUTF8") => {
+						assert_eq!(args.len(), 1);
+						let s = evaluate(context, &args[0].1)?
+							.unwrap_if_lazy()?
+							.try_cast_str("std.encodeUTF8 first argument")?;
+						Val::Arr(Rc::new(s.bytes().map(|b| Val::Num(b as f64)).collect()))
+					}
+					("std", "md5") => {
+						assert_eq!(args.len(), 1);
+						let s = evaluate(context, &args[0].1)?
+							.unwrap_if_lazy()?
+							.try_cast_str("std.md5 first argument")?;
+						Val::Str(format!("{:x}", md5::compute(s.as_bytes())).into())
+					}
 					// faster
 					("std", "join") => {
 						assert_eq!(args.len(), 2);
modifiedcrates/jsonnet-evaluator/src/val.rsdiffbeforeafterboth
--- a/crates/jsonnet-evaluator/src/val.rs
+++ b/crates/jsonnet-evaluator/src/val.rs
@@ -150,6 +150,10 @@
 		self.assert_type(context, ValType::Str)?;
 		Ok(matches_unwrap!(self.unwrap_if_lazy()?, Val::Str(v), v))
 	}
+	pub fn try_cast_num(self, context: &'static str) -> Result<f64> {
+		self.assert_type(context, ValType::Num)?;
+		Ok(matches_unwrap!(self.unwrap_if_lazy()?, Val::Num(v), v))
+	}
 	pub fn unwrap_if_lazy(&self) -> Result<Self> {
 		Ok(if let Val::Lazy(v) = self {
 			v.evaluate()?.unwrap_if_lazy()?