generational_indextree/
error.rs

1//! Errors.
2
3#[cfg(not(feature = "std"))]
4use core::fmt;
5
6#[cfg(feature = "std")]
7use std::{error, fmt};
8
9#[non_exhaustive]
10#[derive(Debug, Clone, Copy)]
11/// Possible node failures.
12pub enum NodeError {
13    /// Attempt to append a node to itself.
14    AppendSelf,
15    /// Attempt to prepend a node to itself.
16    PrependSelf,
17    /// Attempt to insert a node before itself.
18    InsertBeforeSelf,
19    /// Attempt to insert a node after itself.
20    InsertAfterSelf,
21    /// Attempt to insert a removed node, or insert to a removed node.
22    Removed,
23}
24
25impl NodeError {
26    fn as_str(self) -> &'static str {
27        match self {
28            NodeError::AppendSelf => "Can not append a node to itself",
29            NodeError::PrependSelf => "Can not prepend a node to itself",
30            NodeError::InsertBeforeSelf => "Can not insert a node before itself",
31            NodeError::InsertAfterSelf => "Can not insert a node after itself",
32            NodeError::Removed => "Removed node cannot have any parent, siblings, and children",
33        }
34    }
35}
36
37impl fmt::Display for NodeError {
38    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
39        f.write_str(self.as_str())
40    }
41}
42
43#[cfg(feature = "std")]
44impl error::Error for NodeError {}
45
46/// An error type that represents the given structure or argument is
47/// inconsistent or invalid.
48// Intended for internal use.
49#[derive(Debug, Clone, Copy)]
50pub(crate) enum ConsistencyError {
51    /// Specified a node as its parent.
52    ParentChildLoop,
53    /// Specified a node as its sibling.
54    SiblingsLoop,
55}
56
57impl fmt::Display for ConsistencyError {
58    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
59        match self {
60            ConsistencyError::ParentChildLoop => f.write_str("Specified a node as its parent"),
61            ConsistencyError::SiblingsLoop => f.write_str("Specified a node as its sibling"),
62        }
63    }
64}
65
66#[cfg(feature = "std")]
67impl error::Error for ConsistencyError {}