Skip to main content

jrsonnet_cli/
trace.rs

1use clap::{Parser, ValueEnum};
2use jrsonnet_evaluator::trace::{CompactFormat, HiDocFormat, PathResolver, TraceFormat};
3
4#[derive(PartialEq, Eq, ValueEnum, Clone)]
5pub enum TraceFormatName {
6	/// Only show `filename:line:column`
7	Compact,
8	/// Display source code with attached trace annotations
9	Explaining,
10	/// Trace formatting based on hi-doc library
11	HiDoc,
12}
13
14#[derive(Parser)]
15#[clap(next_help_heading = "STACK TRACE VISUAL")]
16pub struct TraceOpts {
17	/// Format of stack traces' display in console.
18	#[clap(long)]
19	trace_format: Option<TraceFormatName>,
20	/// Amount of stack trace elements to be displayed.
21	/// If set to `0` then full stack trace will be displayed.
22	#[clap(long, short = 't', default_value = "20")]
23	max_trace: usize,
24}
25impl TraceOpts {
26	pub fn trace_format(&self) -> Box<dyn TraceFormat> {
27		let resolver = PathResolver::new_cwd_fallback();
28		let max_trace = self.max_trace;
29		let format: Box<dyn TraceFormat> = match self
30			.trace_format
31			.as_ref()
32			.unwrap_or(&TraceFormatName::Compact)
33		{
34			TraceFormatName::Compact => Box::new(CompactFormat {
35				resolver,
36				padding: 4,
37				max_trace,
38			}),
39			TraceFormatName::Explaining | TraceFormatName::HiDoc => Box::new(HiDocFormat {
40				resolver,
41				max_trace,
42			}),
43		};
44		format
45	}
46}