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
--- 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
1use std::path::{Path, PathBuf};1use std::{
2 any::Any,
3 path::{Path, PathBuf},
4};
25
3use jrsonnet_gcmodule::Trace;6use jrsonnet_gcmodule::Trace;
4use jrsonnet_parser::{CodeLocation, Source};7use jrsonnet_parser::{CodeLocation, Source};
58
6use crate::{error::Error, LocError, State};9use crate::{error::Error, LocError};
710
8/// The way paths should be displayed11/// The way paths should be displayed
9#[derive(Clone, Trace)]12#[derive(Clone, Trace)]
48 fn write_trace(51 fn write_trace(
49 &self,52 &self,
50 out: &mut dyn std::fmt::Write,53 out: &mut dyn std::fmt::Write,
51 s: &State,
52 error: &LocError,54 error: &LocError,
53 ) -> Result<(), std::fmt::Error>;55 ) -> Result<(), std::fmt::Error>;
56 fn format(&self, error: &LocError) -> Result<String, std::fmt::Error> {
57 let mut out = String::new();
58 self.write_trace(&mut out, error)?;
59 Ok(out)
60 }
61 fn as_any(&self) -> &dyn Any;
62 fn as_any_mut(&mut self) -> &mut dyn Any;
54}63}
5564
56fn print_code_location(65fn print_code_location(
81#[derive(Trace)]90#[derive(Trace)]
82pub struct CompactFormat {91pub struct CompactFormat {
83 pub resolver: PathResolver,92 pub resolver: PathResolver,
93 pub max_trace: usize,
84 pub padding: usize,94 pub padding: usize,
85}95}
96impl Default for CompactFormat {
97 fn default() -> Self {
98 Self {
99 resolver: PathResolver::Absolute,
100 max_trace: 20,
101 padding: 4,
102 }
103 }
104}
86105
87impl TraceFormat for CompactFormat {106impl TraceFormat for CompactFormat {
88 fn write_trace(107 fn write_trace(
89 &self,108 &self,
90 out: &mut dyn std::fmt::Write,109 out: &mut dyn std::fmt::Write,
91 _s: &State,
92 error: &LocError,110 error: &LocError,
93 ) -> Result<(), std::fmt::Error> {111 ) -> Result<(), std::fmt::Error> {
94 write!(out, "{}", error.error())?;112 write!(out, "{}", error.error())?;
169 Ok(())187 Ok(())
170 }188 }
189
190 fn as_any(&self) -> &dyn Any {
191 self
192 }
193
194 fn as_any_mut(&mut self) -> &mut dyn Any {
195 self
196 }
171}197}
172198
173#[derive(Trace)]199#[derive(Trace)]
174pub struct JsFormat;200pub struct JsFormat {
201 pub max_trace: usize,
202}
175impl TraceFormat for JsFormat {203impl TraceFormat for JsFormat {
176 fn write_trace(204 fn write_trace(
177 &self,205 &self,
178 out: &mut dyn std::fmt::Write,206 out: &mut dyn std::fmt::Write,
179 _s: &State,
180 error: &LocError,207 error: &LocError,
181 ) -> Result<(), std::fmt::Error> {208 ) -> Result<(), std::fmt::Error> {
182 write!(out, "{}", error.error())?;209 write!(out, "{}", error.error())?;
202 Ok(())229 Ok(())
203 }230 }
231
232 fn as_any(&self) -> &dyn Any {
233 self
234 }
235
236 fn as_any_mut(&mut self) -> &mut dyn Any {
237 self
238 }
204}239}
205240
206/// rustc-like trace displaying241/// rustc-like trace displaying
207#[cfg(feature = "explaining-traces")]242#[cfg(feature = "explaining-traces")]
208#[derive(Trace)]243#[derive(Trace)]
209pub struct ExplainingFormat {244pub struct ExplainingFormat {
210 pub resolver: PathResolver,245 pub resolver: PathResolver,
246 pub max_trace: usize,
211}247}
212#[cfg(feature = "explaining-traces")]248#[cfg(feature = "explaining-traces")]
213impl TraceFormat for ExplainingFormat {249impl TraceFormat for ExplainingFormat {
214 fn write_trace(250 fn write_trace(
215 &self,251 &self,
216 out: &mut dyn std::fmt::Write,252 out: &mut dyn std::fmt::Write,
217 _s: &State,
218 error: &LocError,253 error: &LocError,
219 ) -> Result<(), std::fmt::Error> {254 ) -> Result<(), std::fmt::Error> {
220 write!(out, "{}", error.error())?;255 write!(out, "{}", error.error())?;
259 Ok(())294 Ok(())
260 }295 }
296
297 fn as_any(&self) -> &dyn Any {
298 self
299 }
300
301 fn as_any_mut(&mut self) -> &mut dyn Any {
302 self
303 }
261}304}
262305
263impl ExplainingFormat {306impl ExplainingFormat {