git.delta.rocks / jrsonnet / refs/commits / 3302a3dd316f

difftreelog

feat CLI

Лач2020-06-03parent: #fab41c8.patch.diff
in: master

6 files changed

modifiedCargo.lockdiffbeforeafterboth
1# This file is automatically @generated by Cargo.1# This file is automatically @generated by Cargo.
2# It is not intended for manual editing.2# It is not intended for manual editing.
3[[package]]
4name = "annotate-snippets"
5version = "0.8.0"
6source = "registry+https://github.com/rust-lang/crates.io-index"
7checksum = "d78ea013094e5ea606b1c05fe35f1dd7ea1eb1ea259908d040b25bd5ec677ee5"
8
9[[package]]
10name = "atty"
11version = "0.2.14"
12source = "registry+https://github.com/rust-lang/crates.io-index"
13checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8"
14dependencies = [
15 "hermit-abi",
16 "libc",
17 "winapi 0.3.8",
18]
19
20[[package]]
21name = "autocfg"
22version = "1.0.0"
23source = "registry+https://github.com/rust-lang/crates.io-index"
24checksum = "f8aac770f1885fd7e387acedd76065302551364496e46b3dd00860b2f8359b9d"
25
26[[package]]
27name = "bitflags"
28version = "1.2.1"
29source = "registry+https://github.com/rust-lang/crates.io-index"
30checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693"
31
32[[package]]
33name = "clap"
34version = "3.0.0-beta.1"
35source = "registry+https://github.com/rust-lang/crates.io-index"
36checksum = "860643c53f980f0d38a5e25dfab6c3c93b2cb3aa1fe192643d17a293c6c41936"
37dependencies = [
38 "atty",
39 "bitflags",
40 "clap_derive",
41 "indexmap",
42 "lazy_static",
43 "os_str_bytes",
44 "strsim",
45 "term_size 1.0.0-beta1",
46 "termcolor",
47 "textwrap",
48 "unicode-width",
49 "vec_map",
50]
51
52[[package]]
53name = "clap_derive"
54version = "3.0.0-beta.1"
55source = "registry+https://github.com/rust-lang/crates.io-index"
56checksum = "fb51c9e75b94452505acd21d929323f5a5c6c4735a852adbd39ef5fb1b014f30"
57dependencies = [
58 "heck",
59 "proc-macro-error",
60 "proc-macro2",
61 "quote",
62 "syn",
63]
64
3[[package]]65[[package]]
4name = "closure"66name = "closure"
5version = "0.3.0"67version = "0.3.0"
6source = "registry+https://github.com/rust-lang/crates.io-index"68source = "registry+https://github.com/rust-lang/crates.io-index"
7checksum = "d6173fd61b610d15a7566dd7b7620775627441c4ab9dac8906e17cb93a24b782"69checksum = "d6173fd61b610d15a7566dd7b7620775627441c4ab9dac8906e17cb93a24b782"
70
71[[package]]
72name = "heck"
73version = "0.3.1"
74source = "registry+https://github.com/rust-lang/crates.io-index"
75checksum = "20564e78d53d2bb135c343b3f47714a56af2061f1c928fdb541dc7b9fdd94205"
76dependencies = [
77 "unicode-segmentation",
78]
79
80[[package]]
81name = "hermit-abi"
82version = "0.1.13"
83source = "registry+https://github.com/rust-lang/crates.io-index"
84checksum = "91780f809e750b0a89f5544be56617ff6b1227ee485bcb06ebe10cdf89bd3b71"
85dependencies = [
86 "libc",
87]
88
89[[package]]
90name = "indexmap"
91version = "1.4.0"
92source = "registry+https://github.com/rust-lang/crates.io-index"
93checksum = "c398b2b113b55809ceb9ee3e753fcbac793f1956663f3c36549c1346015c2afe"
94dependencies = [
95 "autocfg",
96]
97
98[[package]]
99name = "jsonnet"
100version = "0.1.0"
101dependencies = [
102 "annotate-snippets",
103 "clap",
104 "jsonnet-evaluator",
105]
8106
9[[package]]107[[package]]
10name = "jsonnet-evaluator"108name = "jsonnet-evaluator"
27name = "jsonnet-stdlib"125name = "jsonnet-stdlib"
28version = "0.1.0"126version = "0.1.0"
127
128[[package]]
129name = "kernel32-sys"
130version = "0.2.2"
131source = "registry+https://github.com/rust-lang/crates.io-index"
132checksum = "7507624b29483431c0ba2d82aece8ca6cdba9382bff4ddd0f7490560c056098d"
133dependencies = [
134 "winapi 0.2.8",
135 "winapi-build",
136]
137
138[[package]]
139name = "lazy_static"
140version = "1.4.0"
141source = "registry+https://github.com/rust-lang/crates.io-index"
142checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
143
144[[package]]
145name = "libc"
146version = "0.2.71"
147source = "registry+https://github.com/rust-lang/crates.io-index"
148checksum = "9457b06509d27052635f90d6466700c65095fdf75409b3fbdd903e988b886f49"
149
150[[package]]
151name = "os_str_bytes"
152version = "2.3.1"
153source = "registry+https://github.com/rust-lang/crates.io-index"
154checksum = "06de47b848347d8c4c94219ad8ecd35eb90231704b067e67e6ae2e36ee023510"
29155
30[[package]]156[[package]]
31name = "peg"157name = "peg"
54source = "registry+https://github.com/rust-lang/crates.io-index"180source = "registry+https://github.com/rust-lang/crates.io-index"
55checksum = "0c1a2897e69d986c7986747ebad425cf03746ec5e3e09bb3b2600f91301ba864"181checksum = "0c1a2897e69d986c7986747ebad425cf03746ec5e3e09bb3b2600f91301ba864"
182
183[[package]]
184name = "proc-macro-error"
185version = "0.4.12"
186source = "registry+https://github.com/rust-lang/crates.io-index"
187checksum = "18f33027081eba0a6d8aba6d1b1c3a3be58cbb12106341c2d5759fcd9b5277e7"
188dependencies = [
189 "proc-macro-error-attr",
190 "proc-macro2",
191 "quote",
192 "syn",
193 "version_check",
194]
195
196[[package]]
197name = "proc-macro-error-attr"
198version = "0.4.12"
199source = "registry+https://github.com/rust-lang/crates.io-index"
200checksum = "8a5b4b77fdb63c1eca72173d68d24501c54ab1269409f6b672c85deb18af69de"
201dependencies = [
202 "proc-macro2",
203 "quote",
204 "syn",
205 "syn-mid",
206 "version_check",
207]
56208
57[[package]]209[[package]]
58name = "proc-macro2"210name = "proc-macro2"
72 "proc-macro2",224 "proc-macro2",
73]225]
226
227[[package]]
228name = "strsim"
229version = "0.10.0"
230source = "registry+https://github.com/rust-lang/crates.io-index"
231checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623"
232
233[[package]]
234name = "syn"
235version = "1.0.21"
236source = "registry+https://github.com/rust-lang/crates.io-index"
237checksum = "4696caa4048ac7ce2bcd2e484b3cef88c1004e41b8e945a277e2c25dc0b72060"
238dependencies = [
239 "proc-macro2",
240 "quote",
241 "unicode-xid",
242]
243
244[[package]]
245name = "syn-mid"
246version = "0.5.0"
247source = "registry+https://github.com/rust-lang/crates.io-index"
248checksum = "7be3539f6c128a931cf19dcee741c1af532c7fd387baa739c03dd2e96479338a"
249dependencies = [
250 "proc-macro2",
251 "quote",
252 "syn",
253]
254
255[[package]]
256name = "term_size"
257version = "0.3.2"
258source = "registry+https://github.com/rust-lang/crates.io-index"
259checksum = "1e4129646ca0ed8f45d09b929036bafad5377103edd06e50bf574b353d2b08d9"
260dependencies = [
261 "libc",
262 "winapi 0.3.8",
263]
264
265[[package]]
266name = "term_size"
267version = "1.0.0-beta1"
268source = "registry+https://github.com/rust-lang/crates.io-index"
269checksum = "a8a17d8699e154863becdf18e4fd28bd0be27ca72856f54daf75c00f2566898f"
270dependencies = [
271 "kernel32-sys",
272 "libc",
273 "winapi 0.2.8",
274]
275
276[[package]]
277name = "termcolor"
278version = "1.1.0"
279source = "registry+https://github.com/rust-lang/crates.io-index"
280checksum = "bb6bfa289a4d7c5766392812c0a1f4c1ba45afa1ad47803c11e1f407d846d75f"
281dependencies = [
282 "winapi-util",
283]
284
285[[package]]
286name = "textwrap"
287version = "0.11.0"
288source = "registry+https://github.com/rust-lang/crates.io-index"
289checksum = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060"
290dependencies = [
291 "term_size 0.3.2",
292 "unicode-width",
293]
294
295[[package]]
296name = "unicode-segmentation"
297version = "1.6.0"
298source = "registry+https://github.com/rust-lang/crates.io-index"
299checksum = "e83e153d1053cbb5a118eeff7fd5be06ed99153f00dbcd8ae310c5fb2b22edc0"
300
301[[package]]
302name = "unicode-width"
303version = "0.1.7"
304source = "registry+https://github.com/rust-lang/crates.io-index"
305checksum = "caaa9d531767d1ff2150b9332433f32a24622147e5ebb1f26409d5da67afd479"
74306
75[[package]]307[[package]]
76name = "unicode-xid"308name = "unicode-xid"
77version = "0.2.0"309version = "0.2.0"
78source = "registry+https://github.com/rust-lang/crates.io-index"310source = "registry+https://github.com/rust-lang/crates.io-index"
79checksum = "826e7639553986605ec5979c7dd957c7895e93eabed50ab2ffa7f6128a75097c"311checksum = "826e7639553986605ec5979c7dd957c7895e93eabed50ab2ffa7f6128a75097c"
312
313[[package]]
314name = "vec_map"
315version = "0.8.2"
316source = "registry+https://github.com/rust-lang/crates.io-index"
317checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191"
318
319[[package]]
320name = "version_check"
321version = "0.9.2"
322source = "registry+https://github.com/rust-lang/crates.io-index"
323checksum = "b5a972e5669d67ba988ce3dc826706fb0a8b01471c088cb0b6110b805cc36aed"
324
325[[package]]
326name = "winapi"
327version = "0.2.8"
328source = "registry+https://github.com/rust-lang/crates.io-index"
329checksum = "167dc9d6949a9b857f3451275e911c3f44255842c1f7a76f33c55103a909087a"
330
331[[package]]
332name = "winapi"
333version = "0.3.8"
334source = "registry+https://github.com/rust-lang/crates.io-index"
335checksum = "8093091eeb260906a183e6ae1abdba2ef5ef2257a21801128899c3fc699229c6"
336dependencies = [
337 "winapi-i686-pc-windows-gnu",
338 "winapi-x86_64-pc-windows-gnu",
339]
340
341[[package]]
342name = "winapi-build"
343version = "0.1.1"
344source = "registry+https://github.com/rust-lang/crates.io-index"
345checksum = "2d315eee3b34aca4797b2da6b13ed88266e6d612562a0c46390af8299fc699bc"
346
347[[package]]
348name = "winapi-i686-pc-windows-gnu"
349version = "0.4.0"
350source = "registry+https://github.com/rust-lang/crates.io-index"
351checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
352
353[[package]]
354name = "winapi-util"
355version = "0.1.5"
356source = "registry+https://github.com/rust-lang/crates.io-index"
357checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178"
358dependencies = [
359 "winapi 0.3.8",
360]
361
362[[package]]
363name = "winapi-x86_64-pc-windows-gnu"
364version = "0.4.0"
365source = "registry+https://github.com/rust-lang/crates.io-index"
366checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
80367
modifiedCargo.tomldiffbeforeafterboth
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -3,4 +3,5 @@
 	"crates/jsonnet-parser",
 	"crates/jsonnet-evaluator",
 	"crates/jsonnet-stdlib",
+	"cmds/jsonnet"
 ]
addedcmds/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"]
addedcmds/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);
+			}
+		}
+	}
+}
modifiedcrates/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),
modifiedcrates/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 = {