Skip to main content

pite_core/
error.rs

1// SPDX-License-Identifier: MIT OR Apache-2.0
2use thiserror::Error;
3
4/// Errors produced by the core tree and its registries.
5#[derive(Debug, Error)]
6pub enum CoreError {
7    #[error("node id must not be empty")]
8    EmptyNodeId,
9
10    #[error("node `{0}` already exists in the tree")]
11    DuplicateNode(String),
12
13    #[error("node `{id}` does not exist")]
14    UnknownNode { id: String },
15
16    #[error("node `{child}` references parent `{parent}`, which does not exist")]
17    MissingParent { child: String, parent: String },
18
19    #[error("the root node cannot be removed")]
20    RemoveRoot,
21
22    #[error("node type `{0}` is not registered")]
23    UnknownNodeType(String),
24
25    #[error("node `{0}` has an empty name")]
26    EmptyNodeName(String),
27
28    #[error("reparenting `{child}` under `{parent}` would create a cycle")]
29    Cycle { child: String, parent: String },
30}
31
32/// Convenience alias for core results.
33pub type Result<T> = std::result::Result<T, CoreError>;
34
35#[cfg(test)]
36mod tests {
37    use super::*;
38
39    #[test]
40    fn errors_render_readable_messages() {
41        assert_eq!(
42            CoreError::DuplicateNode("root".into()).to_string(),
43            "node `root` already exists in the tree"
44        );
45    }
46}