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
--- a/cmds/jsonnet/Cargo.toml
+++ b/cmds/jsonnet/Cargo.toml
@@ -13,4 +13,4 @@
 [dependencies.clap]
 version = "3.0.0-beta.1"
 default-features = false
-features = ["std", "derive", "suggestions", "color", "unstable", "wrap_help"]
+features = ["std", "derive"]
modifiedcmds/jsonnet/src/main.rsdiffbeforeafterboth
before · cmds/jsonnet/src/main.rs
1use clap::Clap;23#[derive(Clap)]4#[clap(version = "0.1.0", author = "Lach <iam@lach.pw>")]5struct Opts {6	#[clap(about = "File to compile")]7	input: String,8	#[clap(long, about = "Disable global std variable")]9	no_stdlib: bool,10}1112fn main() {13	let opts: Opts = Opts::parse();14	let evaluator = jsonnet_evaluator::EvaluationState::default();15	if !opts.no_stdlib {16		evaluator.add_stdlib();17	}18	evaluator19		.add_file(20			opts.input.clone(),21			String::from_utf8(std::fs::read(opts.input.clone()).unwrap()).unwrap(),22		)23		.unwrap();24	let result = evaluator.evaluate_file(&opts.input.clone());25	match result {26		Ok(v) => println!("{:?}", v),27		Err(err) => {28			use annotate_snippets::{29				display_list::{DisplayList, FormatOptions},30				snippet::{Annotation, AnnotationType, Slice, Snippet, SourceAnnotation},31			};32			for item in (err.1).0.iter() {33				let source = (item.0).1.clone().unwrap();34				let code = evaluator.get_source(&source.0);35				let snippet = Snippet {36					opt: FormatOptions {37						color: true,38						..Default::default()39					},40					title: Some(Annotation {41						label: Some(&item.1),42						id: None,43						annotation_type: AnnotationType::Error,44					}),45					footer: vec![],46					slices: vec![Slice {47						source: &code,48						line_start: 1,49						origin: Some(&source.0),50						fold: true,51						annotations: vec![SourceAnnotation {52							label: &"Example error annotation",53							annotation_type: AnnotationType::Error,54							range: (source.1, source.2),55						}],56					}],57				};5859				let dl = DisplayList::from(snippet);60				println!("{}", dl);61			}62		}63	}64}