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}