use crate::invariant::InvariantViolation;
use crate::kind::NodeKind;
use alloc::string::String;
use core::fmt;
use zerodds_ast_core::{AstCoreError, NodeId};
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum AstError {
Core(AstCoreError),
NotFound {
node: NodeId,
},
WrongKind {
node: NodeId,
expected: NodeKind,
found: NodeKind,
},
DuplicateName {
scope: NodeId,
name: String,
},
Invariant(alloc::vec::Vec<InvariantViolation>),
NotAnAggregate {
node: NodeId,
found: &'static str,
},
AlreadyComplete {
node: NodeId,
},
}
impl From<AstCoreError> for AstError {
fn from(error: AstCoreError) -> Self {
Self::Core(error)
}
}
impl fmt::Display for AstError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Core(error) => write!(f, "{error}"),
Self::NotFound { node } => write!(f, "no node with identity {node:?}"),
Self::WrongKind {
node,
expected,
found,
} => write!(
f,
"node {node:?} is a {}, expected a {}",
found.as_str(),
expected.as_str()
),
Self::DuplicateName { scope, name } => {
write!(f, "name `{name}` already exists in scope {scope:?}")
}
Self::NotAnAggregate { node, found } => {
write!(f, "node {node:?} is a {found} and holds no such part")
}
Self::AlreadyComplete { node } => {
write!(f, "node {node:?} already has a body")
}
Self::Invariant(violations) => {
write!(f, "commit rejected, {} violation(s):", violations.len())?;
for violation in violations {
write!(f, " {violation};")?;
}
Ok(())
}
}
}
}
impl core::error::Error for AstError {}