git.delta.rocks / jrsonnet / refs/commits / a307cfd45f8d

difftreelog

feat improve error display

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

2 files changed

modifiedcmds/jsonnet/Cargo.tomldiffbeforeafterboth
before · cmds/jsonnet/Cargo.toml
1[package]2name = "jsonnet"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[dependencies]10jsonnet-evaluator = { path = "../../crates/jsonnet-evaluator" }11annotate-snippets = "0.8.0"1213[dependencies.clap]14version = "3.0.0-beta.1"15default-features = false16features = ["std", "derive", "suggestions", "color", "unstable", "wrap_help"]
modifiedcmds/jsonnet/src/main.rsdiffbeforeafterboth
--- a/cmds/jsonnet/src/main.rs
+++ b/cmds/jsonnet/src/main.rs
@@ -1,4 +1,5 @@
 use clap::Clap;
+use jsonnet_evaluator::Val;
 
 #[derive(Clap)]
 #[clap(version = "0.1.0", author = "Lach <iam@lach.pw>")]
@@ -21,15 +22,26 @@
 			String::from_utf8(std::fs::read(opts.input.clone()).unwrap()).unwrap(),
 		)
 		.unwrap();
-	let result = evaluator.evaluate_file(&opts.input.clone());
+	let result = evaluator.evaluate_file(&opts.input);
 	match result {
-		Ok(v) => println!("{:?}", v),
+		Ok(v) => match v {
+			Val::Str(s) => println!("{}", s),
+			Val::Num(n) => println!("{}", n),
+			_v => eprintln!(
+				"jsonnet output is not a string.\nDid you forgot to set --format, or wrap your data with std.manifestJson?"
+			),
+		},
 		Err(err) => {
+			println!("Error: {:?}", err.0);
 			use annotate_snippets::{
 				display_list::{DisplayList, FormatOptions},
 				snippet::{Annotation, AnnotationType, Slice, Snippet, SourceAnnotation},
 			};
 			for item in (err.1).0.iter() {
+				let desc = &item.1;
+				if (item.0).1.is_none() {
+					continue;
+				}
 				let source = (item.0).1.clone().unwrap();
 				let code = evaluator.get_source(&source.0);
 				let snippet = Snippet {
@@ -49,7 +61,7 @@
 						origin: Some(&source.0),
 						fold: true,
 						annotations: vec![SourceAnnotation {
-							label: &"Example error annotation",
+							label: desc,
 							annotation_type: AnnotationType::Error,
 							range: (source.1, source.2),
 						}],