dendron/
node.rs

1//! Node.
2
3mod debug_print;
4mod edit;
5mod frozen;
6mod hot;
7mod internal;
8mod membership;
9mod plain;
10
11use core::cell::BorrowError;
12use core::fmt;
13
14pub(crate) use self::debug_print::DebugPrintSubtreeDescendant;
15pub use self::debug_print::{DebugPrettyPrint, DebugPrintNodeLocal, DebugPrintSubtree};
16pub use self::frozen::FrozenNode;
17pub use self::hot::HotNode;
18use self::internal::NodeCoreLinkWeak;
19pub(crate) use self::internal::{NodeCoreLink, NodeLink};
20pub use self::plain::{Node, NodeWeak};
21
22/// Hierarchy modification error.
23// `From<BorrowError> for Self` is not implemented because the crate should not
24// allow users to convert any `BorrowError` into this error, especially when
25// user-provided `BorrowError` is unrelated to the hierarchy modification.
26#[derive(Debug)]
27#[non_exhaustive]
28pub enum HierarchyError {
29    /// Attempt to make a node its own descendant or ancestor.
30    AncestorDescendantLoop,
31    /// Attempt to make a tree empty.
32    ///
33    /// A tree must have at least one node (the root node), so it cannot be empty.
34    EmptyTree,
35    /// Attempt to make a node the sibling of the root node.
36    SiblingsWithoutParent,
37    /// Failed to borrow node data.
38    BorrowNodeData(BorrowError),
39}
40
41impl fmt::Display for HierarchyError {
42    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
43        let msg = match *self {
44            Self::AncestorDescendantLoop => "attempt to make a node its own descendant or ancestor",
45            Self::EmptyTree => "attempt to make a tree empty",
46            Self::SiblingsWithoutParent => "attempt to make a node sibling of the root node",
47            Self::BorrowNodeData(_) => "failed to borrow the data associated to the node",
48        };
49        f.write_str(msg)
50    }
51}
52
53#[cfg(feature = "std")]
54impl std::error::Error for HierarchyError {
55    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
56        match self {
57            Self::BorrowNodeData(e) => Some(e),
58            _ => None,
59        }
60    }
61}