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
75 #[clap(flatten)]75 #[clap(flatten)]
76 std: StdOpts,76 std: StdOpts,
77
78 #[clap(flatten)]
79 trace: TraceOpts,
8077
81 #[clap(flatten)]78 #[clap(flatten)]
82 gc: GcOpts,79 gc: GcOpts,
90 );87 );
91 fn configure(&self, s: &State) -> Result<Self::Guards> {88 fn configure(&self, s: &State) -> Result<Self::Guards> {
92 // Configure trace first, because tla-code/ext-code can throw89 // Configure trace first, because tla-code/ext-code can throw
93 self.trace.configure(s)?;
94 let misc_guards = self.misc.configure(s)?;90 let misc_guards = self.misc.configure(s)?;
95 let tla_guards = self.tla.configure(s)?;91 let tla_guards = self.tla.configure(s)?;
96 self.std.configure(s)?;92 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
262 write!(f, "\t{}", el.desc)?;262 write!(f, "\t{}", el.desc)?;
263 if let Some(loc) = &el.location {263 if let Some(loc) = &el.location {
264 write!(f, "at {}", loc.0 .0 .0)?;264 write!(f, "at {}", loc.0 .0 .0)?;
265 // loc.0
266 loc.0.map_source_locations(&[loc.1, loc.2]);265 loc.0.map_source_locations(&[loc.1, loc.2]);
267 }266 }
268 writeln!(f)?;267 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 {