use std::fmt;
use crate::Backtrace;
use colored::Colorize;
use derivative::Derivative;
use leo_span::source_map::is_color;
#[cfg(not(target_arch = "wasm32"))]
use color_backtrace::{BacktracePrinter, Verbosity};
pub(crate) const INDENT: &str = " ";
pub fn compute_exit_code(code_identifier: i8, code: i32) -> i32 {
let base = if code_identifier > 99 {
code_identifier as i32 * 100_000
} else if code_identifier as i32 > 9 {
code_identifier as i32 * 10_000
} else {
code_identifier as i32 * 1_000
};
base + code
}
pub fn format_error_code(type_: &str, code_identifier: i8, code: i32) -> String {
format!("E{type_}{code_identifier:0>3}{code:0>4}")
}
pub fn format_warning_code(type_: &str, code_identifier: i8, code: i32) -> String {
format!("W{type_}{code_identifier:0>3}{code:0>4}")
}
#[derive(Derivative)]
#[derivative(Clone, Debug, Default, Hash, PartialEq, Eq)]
pub struct Backtraced {
pub message: String,
pub help: Option<String>,
pub note: Option<String>,
pub code: i32,
pub type_: String,
pub error: bool,
#[derivative(PartialEq = "ignore")]
#[derivative(Hash = "ignore")]
pub backtrace: Backtrace,
}
impl Backtraced {
pub fn new_from_backtrace<S>(
message: S,
help: Option<String>,
code: i32,
type_: String,
error: bool,
backtrace: Backtrace,
) -> Self
where
S: ToString,
{
Self { message: message.to_string(), help, note: None, code, type_, error, backtrace }
}
pub fn error(code_prefix: &str, code: i32, message: impl ToString) -> Self {
Self::new_from_backtrace(message, None, code, code_prefix.to_string(), true, Backtrace::new())
}
pub fn warning(code_prefix: &str, code: i32, message: impl ToString) -> Self {
Self::new_from_backtrace(message, None, code, code_prefix.to_string(), false, Backtrace::new())
}
pub fn with_help(mut self, help: impl fmt::Display) -> Self {
self.help = Some(help.to_string());
self
}
pub fn with_note(mut self, note: impl fmt::Display) -> Self {
self.note = Some(note.to_string());
self
}
pub fn exit_code(&self) -> i32 {
compute_exit_code(37, self.code)
}
pub fn error_code(&self) -> String {
format_error_code(&self.type_, 37, self.code)
}
pub fn warning_code(&self) -> String {
format_warning_code(&self.type_, 37, self.code)
}
pub fn is_error(&self) -> bool {
self.error
}
}
impl fmt::Display for Backtraced {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let (kind, code) = if self.error { ("Error", self.error_code()) } else { ("Warning", self.warning_code()) };
let message = format!("{kind} [{code}]: {message}", message = self.message,);
if is_color() {
if self.error {
writeln!(f, "{}", message.bold().red())?;
} else {
writeln!(f, "{}", message.bold().yellow())?;
}
} else {
writeln!(f, "{message}")?;
};
if self.help.is_some() || self.note.is_some() {
write!(f, "{INDENT } |")?;
if let Some(help) = &self.help {
write!(f, "\n{INDENT } = {help}")?;
}
if let Some(note) = &self.note {
write!(f, "\n{INDENT } = note: {note}")?;
}
}
#[cfg(not(target_arch = "wasm32"))]
{
let leo_backtrace = std::env::var("LEO_BACKTRACE").unwrap_or_default().trim().to_owned();
match leo_backtrace.as_ref() {
"1" => {
let mut printer = BacktracePrinter::default();
printer = printer.lib_verbosity(Verbosity::Medium);
let trace = printer.format_trace_to_string(&self.backtrace).map_err(|_| fmt::Error)?;
write!(f, "{trace}")?;
}
"full" => {
let mut printer = BacktracePrinter::default();
printer = printer.lib_verbosity(Verbosity::Full);
let trace = printer.format_trace_to_string(&self.backtrace).map_err(|_| fmt::Error)?;
write!(f, "{trace}")?;
}
_ => {}
}
}
Ok(())
}
}
impl std::error::Error for Backtraced {
fn description(&self) -> &str {
&self.message
}
}