imlet/types/computation/
model_error.rs

1/// Error variants returned from model building and computation, in the event that something went wrong.
2#[derive(Debug)]
3pub enum ModelError {
4    /// A cyclic dependency was found. This means that one components inputs are dependent on itself.
5    CyclicDependency(String),
6    /// A referenced tag was not present in the model.
7    MissingTag(String),
8    /// A component was added with a tag that is already used.
9    DuplicateTag(String),
10    /// An input was specified to a component that was larger than the total inputs required.
11    InputIndexOutOfRange {
12        component: String,
13        num_inputs: usize,
14        index: usize,
15    },
16    /// A component was added with a list of inputs that doesn't match the number specified by the operation.
17    IncorrectInputCount {
18        component: String,
19        num_inputs: usize,
20        count: usize,
21    },
22    /// Model cannot be computed as a component as an input with no source.
23    MissingInput {
24        component: String,
25        index: usize,
26    },
27    TagGenerationFailed(String),
28    /// Can't compute as no output specified.
29    MissingOutput(),
30    /// Can't compute as no model config is defined.
31    MissingConfig(),
32    /// A required param is missing from a builder.
33    MissingRequiredParam(String),
34    /// Data size for the dense field does not match the specified size.
35    IncorrectDataSize(usize, usize),
36    /// Model has no default output assigned.
37    NoDefaultOutput,
38    /// A generic error with a custom message.
39    Custom(String),
40}
41
42impl std::fmt::Display for ModelError {
43    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
44        match self {
45            ModelError::CyclicDependency(component) => {
46                write!(
47                    f,
48                    "Cyclic dependency detected for {component}. The component is depending on itself."
49                )
50            }
51            ModelError::MissingTag(tag) => {
52                write!(f, "Could not find component with tag {tag} in model.")
53            }
54            ModelError::DuplicateTag(tag) => {
55                write!(f, "Component with tag {tag} already present in model.")
56            }
57            ModelError::InputIndexOutOfRange {
58                component,
59                num_inputs,
60                index,
61            } => {
62                write!(f, "Input index out of bounds for component {component}. The recieved index ({index}) is larger than the input count for the component ({num_inputs})")
63            }
64            ModelError::IncorrectInputCount {
65                component,
66                num_inputs,
67                count,
68            } => {
69                write!(f, "Incorrect inputs for component {component}. The recieved number ({count}) is larger than the input count for the component ({num_inputs})")
70            }
71            ModelError::MissingInput { component, index } => {
72                write!(
73                    f,
74                    "Component {component} is missing an input at index {index}. The model cannot be computed."
75                )
76            }
77            ModelError::TagGenerationFailed(tag) => {
78                write!(f, "Failed to generate increment for tag {tag}.")
79            }
80            ModelError::MissingOutput() => {
81                write!(f, "Failed to generate output as no output node specified.")
82            }
83            ModelError::MissingConfig() => write!(
84                f,
85                "Failed to generate output as no config is specified for the model."
86            ),
87            ModelError::MissingRequiredParam(param) => {
88                write!(f, "Required parameter {param} is missing from the builder.")
89            }
90            ModelError::Custom(message) => write!(f, "{message}"),
91            ModelError::IncorrectDataSize(data_size, field_size) => {
92                write!(f, "The provided data buffer of size (size={data_size}) is not matching the point count of the field (n={field_size}).")
93            }
94            ModelError::NoDefaultOutput => {
95                write!(f, "The model has no default output set. Please set one or provide a specific component.")
96            }
97        }
98    }
99}
100
101impl std::error::Error for ModelError {}