difftreelog
feat CLI
in: master
6 files changed
Cargo.lockdiffbeforeafterboth10 packageslockfile v1
Might be heavy and slow!
closure
0.3.0crates.io↘ 0↖ 1sourceregistry+https://github.com/rust-lang/crates.io-indexchecksumd6173fd61b610d15a7566dd7b7620775627441c4ab9dac8906e17cb93a24b782used byjsonnet-evaluator
0.1.0workspace↘ 3↖ 0jsonnet-parser
0.1.0workspace↘ 2↖ 1depends onused byjsonnet-stdlib
0.1.0workspace↘ 0↖ 2peg
0.6.2crates.io↘ 2↖ 1sourceregistry+https://github.com/rust-lang/crates.io-indexchecksum9075875c14bb21f25f11cad4b6ad2e4dd443b8fb83900b2fbdd6ebd744b82e97depends onused bypeg-macros
0.6.2crates.io↘ 3↖ 1sourceregistry+https://github.com/rust-lang/crates.io-indexchecksumc24c165fd39e995246140cc78df55c56c6733ba87e6658cb3e197b8856c62852used bypeg-runtime
0.6.2crates.io↘ 0↖ 2sourceregistry+https://github.com/rust-lang/crates.io-indexchecksum0c1a2897e69d986c7986747ebad425cf03746ec5e3e09bb3b2600f91301ba864used byproc-macro2
1.0.12crates.io↘ 1↖ 2sourceregistry+https://github.com/rust-lang/crates.io-indexchecksum8872cf6f48eee44265156c111456a700ab3483686b3f96df4cf5481c89157319depends onused byquote
1.0.5crates.io↘ 1↖ 1sourceregistry+https://github.com/rust-lang/crates.io-indexchecksum42934bc9c8ab0d3b273a16d8551c8f0fcff46be73276ca083ec2414c15c4ba5edepends onused byunicode-xid
0.2.0crates.io↘ 0↖ 1sourceregistry+https://github.com/rust-lang/crates.io-indexchecksum826e7639553986605ec5979c7dd957c7895e93eabed50ab2ffa7f6128a75097cused by
Cargo.tomldiffbeforeafterboth--- a/Cargo.toml
+++ b/Cargo.toml
@@ -3,4 +3,5 @@
"crates/jsonnet-parser",
"crates/jsonnet-evaluator",
"crates/jsonnet-stdlib",
+ "cmds/jsonnet"
]
cmds/jsonnet/Cargo.tomldiffbeforeafterboth--- /dev/null
+++ b/cmds/jsonnet/Cargo.toml
@@ -0,0 +1,16 @@
+[package]
+name = "jsonnet"
+version = "0.1.0"
+authors = ["Лач <iam@lach.pw>"]
+edition = "2018"
+
+# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
+
+[dependencies]
+jsonnet-evaluator = { path = "../../crates/jsonnet-evaluator" }
+annotate-snippets = "0.8.0"
+
+[dependencies.clap]
+version = "3.0.0-beta.1"
+default-features = false
+features = ["std", "derive", "suggestions", "color", "unstable", "wrap_help"]
cmds/jsonnet/src/main.rsdiffbeforeafterboth--- /dev/null
+++ b/cmds/jsonnet/src/main.rs
@@ -0,0 +1,64 @@
+use clap::Clap;
+
+#[derive(Clap)]
+#[clap(version = "0.1.0", author = "Lach <iam@lach.pw>")]
+struct Opts {
+ #[clap(about = "File to compile")]
+ input: String,
+ #[clap(long, about = "Disable global std variable")]
+ no_stdlib: bool,
+}
+
+fn main() {
+ let opts: Opts = Opts::parse();
+ let evaluator = jsonnet_evaluator::EvaluationState::default();
+ if !opts.no_stdlib {
+ evaluator.add_stdlib();
+ }
+ evaluator
+ .add_file(
+ opts.input.clone(),
+ String::from_utf8(std::fs::read(opts.input.clone()).unwrap()).unwrap(),
+ )
+ .unwrap();
+ let result = evaluator.evaluate_file(&opts.input.clone());
+ match result {
+ Ok(v) => println!("{:?}", v),
+ Err(err) => {
+ use annotate_snippets::{
+ display_list::{DisplayList, FormatOptions},
+ snippet::{Annotation, AnnotationType, Slice, Snippet, SourceAnnotation},
+ };
+ for item in (err.1).0.iter() {
+ let source = (item.0).1.clone().unwrap();
+ let code = evaluator.get_source(&source.0);
+ let snippet = Snippet {
+ opt: FormatOptions {
+ color: true,
+ ..Default::default()
+ },
+ title: Some(Annotation {
+ label: Some(&item.1),
+ id: None,
+ annotation_type: AnnotationType::Error,
+ }),
+ footer: vec![],
+ slices: vec![Slice {
+ source: &code,
+ line_start: 1,
+ origin: Some(&source.0),
+ fold: true,
+ annotations: vec![SourceAnnotation {
+ label: &"Example error annotation",
+ annotation_type: AnnotationType::Error,
+ range: (source.1, source.2),
+ }],
+ }],
+ };
+
+ let dl = DisplayList::from(snippet);
+ println!("{}", dl);
+ }
+ }
+ }
+}
crates/jsonnet-evaluator/src/evaluate.rsdiffbeforeafterboth--- a/crates/jsonnet-evaluator/src/evaluate.rs
+++ b/crates/jsonnet-evaluator/src/evaluate.rs
@@ -77,6 +77,7 @@
(Val::Str(v1), Val::Str(v2)) => Val::Str(v1.to_owned() + &v2),
(Val::Str(v1), Val::Num(v2)) => Val::Str(format!("{}{}", v1, v2)),
(Val::Num(v1), Val::Str(v2)) => Val::Str(format!("{}{}", v1, v2)),
+ (Val::Str(v1), v2) => Val::Str(format!("{}{:?}", v1, v2)),
(Val::Obj(v1), Val::Obj(v2)) => Val::Obj(v2.with_super(v1.clone())),
(Val::Arr(a), Val::Arr(b)) => Val::Arr([&a[..], &b[..]].concat()),
(Val::Num(v1), Val::Num(v2)) => Val::Num(v1 + v2),
crates/jsonnet-evaluator/src/lib.rsdiffbeforeafterboth--- a/crates/jsonnet-evaluator/src/lib.rs
+++ b/crates/jsonnet-evaluator/src/lib.rs
@@ -84,6 +84,13 @@
Ok(())
}
+ pub fn get_source(&self, name: &str) -> String {
+ let ro_map = self.0.files.borrow();
+ let value = ro_map
+ .get(name)
+ .unwrap_or_else(|| panic!("file not added: {:?}", name));
+ value.0.clone()
+ }
pub fn evaluate_file(&self, name: &str) -> Result<Val> {
self.begin_state();
let expr: LocExpr = {