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
52 #[clap(flatten)]52 #[clap(flatten)]
53 general: GeneralOpts,53 general: GeneralOpts,
54
55 #[clap(flatten)]
56 trace: TraceOpts,
54 #[clap(flatten)]57 #[clap(flatten)]
55 manifest: ManifestOpts,58 manifest: ManifestOpts,
56 #[clap(flatten)]59 #[clap(flatten)]
114117
115fn main_catch(opts: Opts) -> bool {118fn main_catch(opts: Opts) -> bool {
116 let s = State::default();119 let s = State::default();
120 let trace = opts
121 .trace
122 .configure(&s)
123 .expect("this configurator doesn't fail");
117 if let Err(e) = main_real(&s, opts) {124 if let Err(e) = main_real(&s, opts) {
118 if let Error::Evaluation(e) = e {125 if let Error::Evaluation(e) = e {
126 let mut out = String::new();
119 eprintln!("{}", s.stringify_err(&e));127 trace.write_trace(&mut out, &e).expect("format error");
128 eprintln!("{out}")
120 } else {129 } else {
121 eprintln!("{}", e);130 eprintln!("{}", e);
122 }131 }
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
--- 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<dyn TraceFormat>;
+	fn configure(&self, _s: &State) -> Result<Self::Guards> {
 		let resolver = PathResolver::new_cwd_fallback();
-		match self
+		let max_trace = self.max_trace;
+		let format: Box<dyn TraceFormat> = 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)
 	}
 }
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 {