1use crate::node::NodeId;
11use crate::value::ValueId;
12
13pub type Result<T> = std::result::Result<T, IrError>;
15
16#[derive(Clone, Debug, PartialEq, Eq)]
18pub enum GraphError {
19 DanglingValue(ValueId),
21 DanglingNode(NodeId),
23 CycleDetected,
25 MissingProducer(ValueId),
27 DuplicateOutput(ValueId),
29 InputHasProducer(ValueId),
31 ProducerLinkMismatch(ValueId),
33 ConsumerLinkMismatch(ValueId),
35 InvalidOpsetImport { domain: String, version: u64 },
37}
38
39impl std::fmt::Display for GraphError {
40 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
41 match self {
42 GraphError::DanglingValue(v) => write!(f, "dangling value id {v:?}"),
43 GraphError::DanglingNode(n) => write!(f, "dangling node id {n:?}"),
44 GraphError::CycleDetected => write!(f, "cycle detected in graph"),
45 GraphError::MissingProducer(v) => write!(f, "value {v:?} has no producer"),
46 GraphError::DuplicateOutput(v) => write!(f, "value {v:?} produced more than once"),
47 GraphError::InputHasProducer(v) => write!(f, "graph input {v:?} has a producer"),
48 GraphError::ProducerLinkMismatch(v) => {
49 write!(f, "producer link inconsistent for value {v:?}")
50 }
51 GraphError::ConsumerLinkMismatch(v) => {
52 write!(f, "consumer link inconsistent for value {v:?}")
53 }
54 GraphError::InvalidOpsetImport { domain, version } => {
55 write!(f, "invalid opset import: domain={domain} version={version}")
56 }
57 }
58 }
59}
60
61impl std::error::Error for GraphError {}
62
63#[derive(Debug, thiserror::Error)]
65pub enum IrError {
66 #[error("graph validation failed: {0:?}")]
69 GraphInvalid(Vec<GraphError>),
70
71 #[error("cycle detected in graph")]
73 CycleDetected,
74
75 #[error("unknown value id: {0:?}")]
77 UnknownValue(ValueId),
78
79 #[error("unknown node id: {0:?}")]
81 UnknownNode(NodeId),
82
83 #[error("shape mismatch: expected {expected:?}, got {actual:?}")]
85 ShapeMismatch {
86 expected: Vec<usize>,
87 actual: Vec<usize>,
88 },
89
90 #[error("broadcast incompatible: {a:?} vs {b:?}")]
92 BroadcastIncompatible { a: Vec<usize>, b: Vec<usize> },
93
94 #[error("unsupported opset: domain={domain}, version={version}")]
96 UnsupportedOpset { domain: String, version: u64 },
97}
98
99impl From<Vec<GraphError>> for IrError {
100 fn from(errors: Vec<GraphError>) -> Self {
101 IrError::GraphInvalid(errors)
102 }
103}