rune/diagnostics/
fatal.rsuse core::fmt;
use ::rust_alloc::boxed::Box;
#[cfg(feature = "emit")]
use crate::ast::{Span, Spanned};
use crate::compile::{self, LinkerError};
use crate::SourceId;
#[derive(Debug)]
pub struct FatalDiagnostic {
pub(crate) source_id: SourceId,
pub(crate) kind: Box<FatalDiagnosticKind>,
}
impl FatalDiagnostic {
pub fn source_id(&self) -> SourceId {
self.source_id
}
pub fn kind(&self) -> &FatalDiagnosticKind {
&self.kind
}
#[cfg(test)]
pub(crate) fn into_kind(self) -> FatalDiagnosticKind {
*self.kind
}
#[cfg(feature = "emit")]
pub(crate) fn span(&self) -> Option<Span> {
match &*self.kind {
FatalDiagnosticKind::CompileError(error) => Some(error.span()),
FatalDiagnosticKind::LinkError(..) => None,
FatalDiagnosticKind::Internal(..) => None,
}
}
}
impl fmt::Display for FatalDiagnostic {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.kind.fmt(f)
}
}
impl core::error::Error for FatalDiagnostic {
#[inline]
fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {
match &*self.kind {
FatalDiagnosticKind::CompileError(error) => Some(error),
FatalDiagnosticKind::LinkError(error) => Some(error),
_ => None,
}
}
}
#[derive(Debug)]
#[allow(missing_docs)]
#[non_exhaustive]
pub enum FatalDiagnosticKind {
CompileError(compile::Error),
LinkError(LinkerError),
Internal(&'static str),
}
impl fmt::Display for FatalDiagnosticKind {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
FatalDiagnosticKind::CompileError(error) => error.fmt(f),
FatalDiagnosticKind::LinkError(error) => error.fmt(f),
FatalDiagnosticKind::Internal(message) => message.fmt(f),
}
}
}
impl From<compile::Error> for FatalDiagnosticKind {
#[inline]
fn from(error: compile::Error) -> Self {
FatalDiagnosticKind::CompileError(error)
}
}
impl From<LinkerError> for FatalDiagnosticKind {
#[inline]
fn from(error: LinkerError) -> Self {
FatalDiagnosticKind::LinkError(error)
}
}