git.delta.rocks / jrsonnet / refs/commits / 06bf1b38ec36

difftreelog

refactor remove trace format from state

Yaroslav Bolyukin2022-11-09parent: #78be616.patch.diff
in: master

5 files changed

modifiedcmds/jrsonnet/src/main.rsdiffbeforeafterboth
--- 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);
 		}
modifiedcrates/jrsonnet-cli/src/lib.rsdiffbeforeafterboth
--- 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<Self::Guards> {
 		// 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)?;
modifiedcrates/jrsonnet-cli/src/trace.rsdiffbeforeafterboth
1use clap::{Parser, ValueEnum};1use clap::{Parser, ValueEnum};
2use jrsonnet_evaluator::{2use jrsonnet_evaluator::{
3 error::Result,3 error::Result,
4 trace::{CompactFormat, ExplainingFormat, PathResolver},4 trace::{CompactFormat, ExplainingFormat, PathResolver, TraceFormat},
5 State,5 State,
6};6};
77
27 max_trace: usize,27 max_trace: usize,
28}28}
29impl ConfigureState for TraceOpts {29impl ConfigureState for TraceOpts {
30 type Guards = ();30 type Guards = Box<dyn TraceFormat>;
31 fn configure(&self, s: &State) -> Result<()> {31 fn configure(&self, _s: &State) -> Result<Self::Guards> {
32 let resolver = PathResolver::new_cwd_fallback();32 let resolver = PathResolver::new_cwd_fallback();
33 let max_trace = self.max_trace;
33 match self34 let format: Box<dyn TraceFormat> = match self
34 .trace_format35 .trace_format
35 .as_ref()36 .as_ref()
36 .unwrap_or(&TraceFormatName::Compact)37 .unwrap_or(&TraceFormatName::Compact)
37 {38 {
38 TraceFormatName::Compact => s.set_trace_format(CompactFormat {39 TraceFormatName::Compact => Box::new(CompactFormat {
39 resolver,40 resolver,
40 padding: 4,41 padding: 4,
42 max_trace,
41 }),43 }),
42 TraceFormatName::Explaining => s.set_trace_format(ExplainingFormat { resolver }),44 TraceFormatName::Explaining => Box::new(ExplainingFormat {
45 resolver,
46 max_trace,
47 }),
43 }48 };
44 s.set_max_trace(self.max_trace);
45 Ok(())49 Ok(format)
46 }50 }
47}51}
4852
modifiedcrates/jrsonnet-evaluator/src/error.rsdiffbeforeafterboth
--- 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)?;
modifiedcrates/jrsonnet-evaluator/src/trace/mod.rsdiffbeforeafterboth
--- 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<String, std::fmt::Error> {
+		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 {