Skip to main content

laravel_nestedset/
error.rs

1//! The package's errors.
2
3use std::error::Error;
4use std::fmt;
5
6/// An operation on a tree that can't be done.
7#[derive(Debug, Clone, PartialEq, Eq)]
8#[non_exhaustive]
9pub enum NestedSetError {
10    /// The node an operation starts from is no longer in the table.
11    NodeNotFound {
12        /// The model's name.
13        model: &'static str,
14        /// The node's key.
15        key: String,
16    },
17    /// A move would put a node inside its own subtree.
18    MoveIntoOwnSubtree {
19        /// The model's name.
20        model: &'static str,
21        /// The moved node's key.
22        key: String,
23        /// The key of the node it was to move next to.
24        target: String,
25    },
26    /// A node made by `create_root_in` isn't in the scope it was given: the scope doesn't name
27    /// the model's scope columns.
28    OutsideScope {
29        /// The model's name.
30        model: &'static str,
31    },
32    /// An arrangement lists the same node twice.
33    DuplicateKey {
34        /// The model's name.
35        model: &'static str,
36        /// The node's key.
37        key: String,
38    },
39    /// A move would take a node into another tree of the table.
40    ScopeMismatch {
41        /// The model's name.
42        model: &'static str,
43        /// The moved node's key.
44        key: String,
45        /// The key of the node in the other tree.
46        target: String,
47    },
48}
49
50impl fmt::Display for NestedSetError {
51    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
52        match self {
53            Self::NodeNotFound { model, key } => {
54                write!(f, "The `{model}` node with the key `{key}` no longer exists.")
55            }
56            Self::MoveIntoOwnSubtree { model, key, target } => write!(
57                f,
58                "The `{model}` node `{key}` can't move next to `{target}`, which is the node itself or below it."
59            ),
60            Self::OutsideScope { model } => write!(
61                f,
62                "The new `{model}` root isn't in the scope it was created in: build the scope from the same columns as `{model}::scope`."
63            ),
64            Self::DuplicateKey { model, key } => {
65                write!(f, "The arrangement lists the `{model}` node `{key}` more than once.")
66            }
67            Self::ScopeMismatch { model, key, target } => write!(
68                f,
69                "The `{model}` node `{key}` can't move next to `{target}`, which is in another tree."
70            ),
71        }
72    }
73}
74
75impl Error for NestedSetError {}