1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
//! Errors.

#[cfg(not(feature = "std"))]
use core::fmt;

#[cfg(feature = "std")]
use std::{error, fmt};

#[non_exhaustive]
#[derive(Debug, Clone, Copy)]
/// Possible node failures.
pub enum NodeError {
    /// Attempt to append a node to itself.
    AppendSelf,
    /// Attempt to prepend a node to itself.
    PrependSelf,
    /// Attempt to insert a node before itself.
    InsertBeforeSelf,
    /// Attempt to insert a node after itself.
    InsertAfterSelf,
    /// Attempt to insert a removed node, or insert to a removed node.
    Removed,
}

impl NodeError {
    fn as_str(self) -> &'static str {
        match self {
            NodeError::AppendSelf => "Can not append a node to itself",
            NodeError::PrependSelf => "Can not prepend a node to itself",
            NodeError::InsertBeforeSelf => "Can not insert a node before itself",
            NodeError::InsertAfterSelf => "Can not insert a node after itself",
            NodeError::Removed => "Removed node cannot have any parent, siblings, and children",
        }
    }
}

impl fmt::Display for NodeError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(self.as_str())
    }
}

#[cfg(feature = "std")]
impl error::Error for NodeError {}

/// An error type that represents the given structure or argument is
/// inconsistent or invalid.
// Intended for internal use.
#[derive(Debug, Clone, Copy)]
pub(crate) enum ConsistencyError {
    /// Specified a node as its parent.
    ParentChildLoop,
    /// Specified a node as its sibling.
    SiblingsLoop,
}

impl fmt::Display for ConsistencyError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            ConsistencyError::ParentChildLoop => f.write_str("Specified a node as its parent"),
            ConsistencyError::SiblingsLoop => f.write_str("Specified a node as its sibling"),
        }
    }
}

#[cfg(feature = "std")]
impl error::Error for ConsistencyError {}