use crate::node::NodeId;
use crate::value::ValueId;
pub type Result<T> = std::result::Result<T, IrError>;
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum GraphError {
DanglingValue(ValueId),
DanglingNode(NodeId),
CycleDetected,
MissingProducer(ValueId),
DuplicateOutput(ValueId),
InputHasProducer(ValueId),
ProducerLinkMismatch(ValueId),
ConsumerLinkMismatch(ValueId),
InvalidOpsetImport { domain: String, version: u64 },
}
impl std::fmt::Display for GraphError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
GraphError::DanglingValue(v) => write!(f, "dangling value id {v:?}"),
GraphError::DanglingNode(n) => write!(f, "dangling node id {n:?}"),
GraphError::CycleDetected => write!(f, "cycle detected in graph"),
GraphError::MissingProducer(v) => write!(f, "value {v:?} has no producer"),
GraphError::DuplicateOutput(v) => write!(f, "value {v:?} produced more than once"),
GraphError::InputHasProducer(v) => write!(f, "graph input {v:?} has a producer"),
GraphError::ProducerLinkMismatch(v) => {
write!(f, "producer link inconsistent for value {v:?}")
}
GraphError::ConsumerLinkMismatch(v) => {
write!(f, "consumer link inconsistent for value {v:?}")
}
GraphError::InvalidOpsetImport { domain, version } => {
write!(f, "invalid opset import: domain={domain} version={version}")
}
}
}
}
impl std::error::Error for GraphError {}
#[derive(Debug, thiserror::Error)]
pub enum IrError {
#[error("graph validation failed: {0:?}")]
GraphInvalid(Vec<GraphError>),
#[error("cycle detected in graph")]
CycleDetected,
#[error("unknown value id: {0:?}")]
UnknownValue(ValueId),
#[error("unknown node id: {0:?}")]
UnknownNode(NodeId),
#[error("shape mismatch: expected {expected:?}, got {actual:?}")]
ShapeMismatch {
expected: Vec<usize>,
actual: Vec<usize>,
},
#[error("broadcast incompatible: {a:?} vs {b:?}")]
BroadcastIncompatible { a: Vec<usize>, b: Vec<usize> },
#[error("unsupported opset: domain={domain}, version={version}")]
UnsupportedOpset { domain: String, version: u64 },
}
impl From<Vec<GraphError>> for IrError {
fn from(errors: Vec<GraphError>) -> Self {
IrError::GraphInvalid(errors)
}
}