1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
use clap::{Parser, ValueEnum};
use jrsonnet_evaluator::{
	error::Result,
	trace::{CompactFormat, ExplainingFormat, PathResolver, TraceFormat},
	State,
};

use crate::ConfigureState;

#[derive(PartialEq, Eq, ValueEnum, Clone)]
pub enum TraceFormatName {
	/// Only show `filename:line:column`
	Compact,
	/// Display source code with attached trace annotations
	Explaining,
}

#[derive(Parser)]
#[clap(next_help_heading = "STACK TRACE VISUAL")]
pub struct TraceOpts {
	/// Format of stack traces' display in console.
	#[clap(long)]
	trace_format: Option<TraceFormatName>,
	/// Amount of stack trace elements to be displayed.
	/// If set to `0` then full stack trace will be displayed.
	#[clap(long, short = 't', default_value = "20")]
	max_trace: usize,
}
impl ConfigureState for TraceOpts {
	type Guards = Box<dyn TraceFormat>;
	fn configure(&self, _s: &State) -> Result<Self::Guards> {
		let resolver = PathResolver::new_cwd_fallback();
		let max_trace = self.max_trace;
		let format: Box<dyn TraceFormat> = match self
			.trace_format
			.as_ref()
			.unwrap_or(&TraceFormatName::Compact)
		{
			TraceFormatName::Compact => Box::new(CompactFormat {
				resolver,
				padding: 4,
				max_trace,
			}),
			TraceFormatName::Explaining => Box::new(ExplainingFormat {
				resolver,
				max_trace,
			}),
		};
		Ok(format)
	}
}