use std::fmt;
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub struct DiagnosticCode(&'static str);
impl DiagnosticCode {
pub const fn new(value: &'static str) -> Self {
Self(value)
}
pub const fn as_str(self) -> &'static str {
self.0
}
}
impl fmt::Display for DiagnosticCode {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_str(self.0)
}
}
pub const MADS001: DiagnosticCode = DiagnosticCode::new("MADS001");
pub const MADS002: DiagnosticCode = DiagnosticCode::new("MADS002");
pub const MADS003: DiagnosticCode = DiagnosticCode::new("MADS003");
pub const MADS004: DiagnosticCode = DiagnosticCode::new("MADS004");
pub const MADS005: DiagnosticCode = DiagnosticCode::new("MADS005");
pub const MADS006: DiagnosticCode = DiagnosticCode::new("MADS006");
pub const MADS007: DiagnosticCode = DiagnosticCode::new("MADS007");
pub const MADS008: DiagnosticCode = DiagnosticCode::new("MADS008");
pub const MADS009: DiagnosticCode = DiagnosticCode::new("MADS009");
pub const MADS010: DiagnosticCode = DiagnosticCode::new("MADS010");
pub const MADS011: DiagnosticCode = DiagnosticCode::new("MADS011");
pub const MADS020: DiagnosticCode = DiagnosticCode::new("MADS020");
pub const MADS030: DiagnosticCode = DiagnosticCode::new("MADS030");
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub struct SourceLocation {
pub file: &'static str,
pub line: u32,
pub column: u32,
}
impl SourceLocation {
pub const fn new(file: &'static str, line: u32, column: u32) -> Self {
Self { file, line, column }
}
}
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub struct Diagnostic {
code: DiagnosticCode,
title: String,
message: String,
subject: Option<String>,
location: Option<SourceLocation>,
suggestions: Vec<String>,
}
impl Diagnostic {
pub fn new(code: DiagnosticCode, title: impl Into<String>, message: impl Into<String>) -> Self {
Self {
code,
title: title.into(),
message: message.into(),
subject: None,
location: None,
suggestions: Vec::new(),
}
}
pub fn with_subject(mut self, subject: impl Into<String>) -> Self {
self.subject = Some(subject.into());
self
}
pub fn with_location(mut self, location: SourceLocation) -> Self {
self.location = Some(location);
self
}
pub fn with_suggestion(mut self, suggestion: impl Into<String>) -> Self {
self.suggestions.push(suggestion.into());
self
}
pub const fn code(&self) -> DiagnosticCode {
self.code
}
pub fn title(&self) -> &str {
&self.title
}
pub fn message(&self) -> &str {
&self.message
}
pub fn subject(&self) -> Option<&str> {
self.subject.as_deref()
}
pub const fn location(&self) -> Option<SourceLocation> {
self.location
}
pub fn suggestions(&self) -> &[String] {
&self.suggestions
}
}
impl fmt::Display for Diagnostic {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(formatter, "error[{}]: {}", self.code, self.title)?;
if let Some(location) = self.location {
write!(
formatter,
"\n --> {}:{}:{}",
location.file, location.line, location.column
)?;
}
if let Some(subject) = &self.subject {
write!(formatter, "\n = subject: {subject}")?;
}
write!(formatter, "\n = {}", self.message)?;
for suggestion in &self.suggestions {
write!(formatter, "\n help: {suggestion}")?;
}
Ok(())
}
}
#[derive(Debug)]
pub struct Error {
diagnostics: Vec<Diagnostic>,
source: Option<Box<dyn std::error::Error + Send + Sync>>,
}
impl Error {
pub fn new(diagnostic: Diagnostic) -> Self {
Self {
diagnostics: vec![diagnostic],
source: None,
}
}
pub fn from_diagnostics(
primary: Diagnostic,
related: impl IntoIterator<Item = Diagnostic>,
) -> Self {
let mut diagnostics = vec![primary];
diagnostics.extend(related);
Self {
diagnostics,
source: None,
}
}
pub(crate) fn with_related_diagnostics(
mut self,
related: impl IntoIterator<Item = Diagnostic>,
) -> Self {
self.diagnostics.extend(related);
self
}
pub fn with_source<E>(diagnostic: Diagnostic, source: E) -> Self
where
E: std::error::Error + Send + Sync + 'static,
{
Self {
diagnostics: vec![diagnostic],
source: Some(Box::new(source)),
}
}
pub fn code(&self) -> DiagnosticCode {
self.diagnostics[0].code()
}
pub fn diagnostic(&self) -> &Diagnostic {
&self.diagnostics[0]
}
pub fn diagnostics(&self) -> &[Diagnostic] {
&self.diagnostics
}
}
impl fmt::Display for Error {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
for (index, diagnostic) in self.diagnostics.iter().enumerate() {
if index > 0 {
formatter.write_str("\n\n")?;
}
diagnostic.fmt(formatter)?;
}
Ok(())
}
}
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
self.source
.as_deref()
.map(|source| source as &(dyn std::error::Error + 'static))
}
}
pub type Result<T> = std::result::Result<T, Error>;