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}
after · cmds/jsonnet/src/main.rs
1use clap::Clap;2use jsonnet_evaluator::Val;34#[derive(Clap)]5#[clap(version = "0.1.0", author = "Lach <iam@lach.pw>")]6struct Opts {7	#[clap(about = "File to compile")]8	input: String,9	#[clap(long, about = "Disable global std variable")]10	no_stdlib: bool,11}1213fn main() {14	let opts: Opts = Opts::parse();15	let evaluator = jsonnet_evaluator::EvaluationState::default();16	if !opts.no_stdlib {17		evaluator.add_stdlib();18	}19	evaluator20		.add_file(21			opts.input.clone(),22			String::from_utf8(std::fs::read(opts.input.clone()).unwrap()).unwrap(),23		)24		.unwrap();25	let result = evaluator.evaluate_file(&opts.input);26	match result {27		Ok(v) => match v {28			Val::Str(s) => println!("{}", s),29			Val::Num(n) => println!("{}", n),30			_v => eprintln!(31				"jsonnet output is not a string.\nDid you forgot to set --format, or wrap your data with std.manifestJson?"32			),33		},34		Err(err) => {35			println!("Error: {:?}", err.0);36			use annotate_snippets::{37				display_list::{DisplayList, FormatOptions},38				snippet::{Annotation, AnnotationType, Slice, Snippet, SourceAnnotation},39			};40			for item in (err.1).0.iter() {41				let desc = &item.1;42				if (item.0).1.is_none() {43					continue;44				}45				let source = (item.0).1.clone().unwrap();46				let code = evaluator.get_source(&source.0);47				let snippet = Snippet {48					opt: FormatOptions {49						color: true,50						..Default::default()51					},52					title: Some(Annotation {53						label: Some(&item.1),54						id: None,55						annotation_type: AnnotationType::Error,56					}),57					footer: vec![],58					slices: vec![Slice {59						source: &code,60						line_start: 1,61						origin: Some(&source.0),62						fold: true,63						annotations: vec![SourceAnnotation {64							label: desc,65							annotation_type: AnnotationType::Error,66							range: (source.1, source.2),67						}],68					}],69				};7071				let dl = DisplayList::from(snippet);72				println!("{}", dl);73			}74		}75	}76}