jrsonnet_cli/
trace.rs

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