use crate::source::Span;
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum IrError {
DanglingReference { what: &'static str, id: u32 },
Unsupported { message: String, span: Option<Span> },
Invalid {
code: &'static str,
message: String,
span: Option<Span>,
},
}
impl IrError {
pub fn code(&self) -> &'static str {
match self {
IrError::DanglingReference { .. } => "dangling-reference",
IrError::Unsupported { .. } => "unsupported",
IrError::Invalid { code, .. } => code,
}
}
pub fn message(&self) -> String {
match self {
IrError::DanglingReference { what, id } => {
format!("dangling {what} reference: id {id}")
}
IrError::Unsupported { message, .. } => message.clone(),
IrError::Invalid { message, .. } => message.clone(),
}
}
pub fn span(&self) -> Option<Span> {
match self {
IrError::Unsupported { span, .. } => *span,
IrError::Invalid { span, .. } => *span,
IrError::DanglingReference { .. } => None,
}
}
}
impl std::fmt::Display for IrError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}: {}", self.code(), self.message())
}
}
impl std::error::Error for IrError {}