From a307cfd45f8d486c7ff3d0b26e3a08d21cd758e8 Mon Sep 17 00:00:00 2001 From: Лач Date: Thu, 04 Jun 2020 10:32:09 +0000 Subject: [PATCH] feat: improve error display --- --- 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"] --- 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 ")] @@ -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), }], -- gitstuff