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    /// Attempt to append an ancestor node to a descendant.
24    AppendAncestor,
25    /// Attempt to prepend an ancestor node to a descendant.
26    PrependAncestor,
27}
28
29impl NodeError {
30    fn as_str(self) -> &'static str {
31        match self {
32            NodeError::AppendSelf => "Can not append a node to itself",
33            NodeError::PrependSelf => "Can not prepend a node to itself",
34            NodeError::InsertBeforeSelf => "Can not insert a node before itself",
35            NodeError::InsertAfterSelf => "Can not insert a node after itself",
36            NodeError::Removed => "Removed node cannot have any parent, siblings, and children",
37            NodeError::AppendAncestor => "Can not append a node to its descendant",
38            NodeError::PrependAncestor => "Can not prepend a node to its descendant",
39        }
40    }
41}
42
43impl fmt::Display for NodeError {
44    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
45        f.write_str(self.as_str())
46    }
47}
48
49#[cfg(feature = "std")]
50impl error::Error for NodeError {}
51
52/// An error type that represents the given structure or argument is
53/// inconsistent or invalid.
54// Intended for internal use.
55#[derive(Debug, Clone, Copy)]
56pub(crate) enum ConsistencyError {
57    /// Specified a node as its parent.
58    ParentChildLoop,
59    /// Specified a node as its sibling.
60    SiblingsLoop,
61}
62
63impl fmt::Display for ConsistencyError {
64    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
65        match self {
66            ConsistencyError::ParentChildLoop => f.write_str("Specified a node as its parent"),
67            ConsistencyError::SiblingsLoop => f.write_str("Specified a node as its sibling"),
68        }
69    }
70}
71
72#[cfg(feature = "std")]
73impl error::Error for ConsistencyError {}