#![allow(unused_assignments)]
use crate::file::display_path;
use miette::{Diagnostic, NamedSource, SourceSpan};
use std::fmt;
use std::path::{Path, PathBuf};
use thiserror::Error;
#[derive(Debug, Error, Diagnostic)]
#[error("Invalid TOML in config file: {}", path.display())]
#[diagnostic(code(mise::config::parse_error))]
pub struct TomlParseError {
path: PathBuf,
#[source_code]
src: NamedSource<String>,
#[label("{message}")]
span: SourceSpan,
message: String,
}
#[derive(Debug)]
pub struct MiseDiagnostic {
message: String,
rendered: String,
}
impl fmt::Display for MiseDiagnostic {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.message)
}
}
impl std::error::Error for MiseDiagnostic {}
impl MiseDiagnostic {
pub fn new<D: Diagnostic + Send + Sync + 'static>(diagnostic: D) -> Self {
let message = diagnostic.to_string();
let rendered = format!("{:?}", miette::Report::new(diagnostic));
MiseDiagnostic { message, rendered }
}
pub fn render(&self) -> &str {
&self.rendered
}
}
pub fn toml_parse_error(err: &toml::de::Error, source: &str, path: &Path) -> eyre::Report {
let message = err.message().to_string();
let span = err
.span()
.map(|r| SourceSpan::from((r.start, r.end.saturating_sub(r.start))))
.unwrap_or_else(|| SourceSpan::from((0, 0)));
let diagnostic = TomlParseError {
path: path.to_path_buf(),
src: NamedSource::new(display_path(path), source.to_string()),
span,
message,
};
eyre::Report::new(MiseDiagnostic::new(diagnostic))
}