--- a/cmds/jrsonnet/src/main.rs +++ b/cmds/jrsonnet/src/main.rs @@ -51,6 +51,9 @@ input: InputOpts, #[clap(flatten)] general: GeneralOpts, + + #[clap(flatten)] + trace: TraceOpts, #[clap(flatten)] manifest: ManifestOpts, #[clap(flatten)] @@ -114,9 +117,15 @@ fn main_catch(opts: Opts) -> bool { let s = State::default(); + let trace = opts + .trace + .configure(&s) + .expect("this configurator doesn't fail"); if let Err(e) = main_real(&s, opts) { if let Error::Evaluation(e) = e { - eprintln!("{}", s.stringify_err(&e)); + let mut out = String::new(); + trace.write_trace(&mut out, &e).expect("format error"); + eprintln!("{out}") } else { eprintln!("{}", e); } --- a/crates/jrsonnet-cli/src/lib.rs +++ b/crates/jrsonnet-cli/src/lib.rs @@ -76,9 +76,6 @@ std: StdOpts, #[clap(flatten)] - trace: TraceOpts, - - #[clap(flatten)] gc: GcOpts, } @@ -90,7 +87,6 @@ ); fn configure(&self, s: &State) -> Result { // Configure trace first, because tla-code/ext-code can throw - self.trace.configure(s)?; let misc_guards = self.misc.configure(s)?; let tla_guards = self.tla.configure(s)?; self.std.configure(s)?; --- a/crates/jrsonnet-cli/src/trace.rs +++ b/crates/jrsonnet-cli/src/trace.rs @@ -1,7 +1,7 @@ use clap::{Parser, ValueEnum}; use jrsonnet_evaluator::{ error::Result, - trace::{CompactFormat, ExplainingFormat, PathResolver}, + trace::{CompactFormat, ExplainingFormat, PathResolver, TraceFormat}, State, }; @@ -27,21 +27,25 @@ max_trace: usize, } impl ConfigureState for TraceOpts { - type Guards = (); - fn configure(&self, s: &State) -> Result<()> { + type Guards = Box; + fn configure(&self, _s: &State) -> Result { let resolver = PathResolver::new_cwd_fallback(); - match self + let max_trace = self.max_trace; + let format: Box = match self .trace_format .as_ref() .unwrap_or(&TraceFormatName::Compact) { - TraceFormatName::Compact => s.set_trace_format(CompactFormat { + TraceFormatName::Compact => Box::new(CompactFormat { resolver, padding: 4, + max_trace, }), - TraceFormatName::Explaining => s.set_trace_format(ExplainingFormat { resolver }), - } - s.set_max_trace(self.max_trace); - Ok(()) + TraceFormatName::Explaining => Box::new(ExplainingFormat { + resolver, + max_trace, + }), + }; + Ok(format) } } --- a/crates/jrsonnet-evaluator/src/error.rs +++ b/crates/jrsonnet-evaluator/src/error.rs @@ -262,7 +262,6 @@ write!(f, "\t{}", el.desc)?; if let Some(loc) = &el.location { write!(f, "at {}", loc.0 .0 .0)?; - // loc.0 loc.0.map_source_locations(&[loc.1, loc.2]); } writeln!(f)?; --- a/crates/jrsonnet-evaluator/src/trace/mod.rs +++ b/crates/jrsonnet-evaluator/src/trace/mod.rs @@ -1,9 +1,12 @@ -use std::path::{Path, PathBuf}; +use std::{ + any::Any, + path::{Path, PathBuf}, +}; use jrsonnet_gcmodule::Trace; use jrsonnet_parser::{CodeLocation, Source}; -use crate::{error::Error, LocError, State}; +use crate::{error::Error, LocError}; /// The way paths should be displayed #[derive(Clone, Trace)] @@ -48,9 +51,15 @@ fn write_trace( &self, out: &mut dyn std::fmt::Write, - s: &State, error: &LocError, ) -> Result<(), std::fmt::Error>; + fn format(&self, error: &LocError) -> Result { + let mut out = String::new(); + self.write_trace(&mut out, error)?; + Ok(out) + } + fn as_any(&self) -> &dyn Any; + fn as_any_mut(&mut self) -> &mut dyn Any; } fn print_code_location( @@ -81,14 +90,23 @@ #[derive(Trace)] pub struct CompactFormat { pub resolver: PathResolver, + pub max_trace: usize, pub padding: usize, } +impl Default for CompactFormat { + fn default() -> Self { + Self { + resolver: PathResolver::Absolute, + max_trace: 20, + padding: 4, + } + } +} impl TraceFormat for CompactFormat { fn write_trace( &self, out: &mut dyn std::fmt::Write, - _s: &State, error: &LocError, ) -> Result<(), std::fmt::Error> { write!(out, "{}", error.error())?; @@ -168,15 +186,24 @@ } Ok(()) } + + fn as_any(&self) -> &dyn Any { + self + } + + fn as_any_mut(&mut self) -> &mut dyn Any { + self + } } #[derive(Trace)] -pub struct JsFormat; +pub struct JsFormat { + pub max_trace: usize, +} impl TraceFormat for JsFormat { fn write_trace( &self, out: &mut dyn std::fmt::Write, - _s: &State, error: &LocError, ) -> Result<(), std::fmt::Error> { write!(out, "{}", error.error())?; @@ -201,6 +228,14 @@ } Ok(()) } + + fn as_any(&self) -> &dyn Any { + self + } + + fn as_any_mut(&mut self) -> &mut dyn Any { + self + } } /// rustc-like trace displaying @@ -208,13 +243,13 @@ #[derive(Trace)] pub struct ExplainingFormat { pub resolver: PathResolver, + pub max_trace: usize, } #[cfg(feature = "explaining-traces")] impl TraceFormat for ExplainingFormat { fn write_trace( &self, out: &mut dyn std::fmt::Write, - _s: &State, error: &LocError, ) -> Result<(), std::fmt::Error> { write!(out, "{}", error.error())?; @@ -258,6 +293,14 @@ } Ok(()) } + + fn as_any(&self) -> &dyn Any { + self + } + + fn as_any_mut(&mut self) -> &mut dyn Any { + self + } } impl ExplainingFormat {