Skip to main content

tagtree/
error.rs

1use std::fmt;
2
3use crate::path::TagPathError;
4
5/// Errors returned by the public tag-tree and path-tree operations.
6#[derive(Debug, Clone, PartialEq, Eq)]
7pub enum Error {
8    /// A supplied path does not follow the slash-separated path rules.
9    InvalidPath(TagPathError),
10    /// The requested source or query path does not exist.
11    PathNotFound(String),
12    /// The root node cannot be moved into another path.
13    CannotMoveRoot,
14    /// The root path cannot be removed as a subtree.
15    CannotRemoveRoot,
16    /// A path cannot be moved into one of its own descendants.
17    CannotMoveIntoDescendant { from: String, to: String },
18}
19
20impl fmt::Display for Error {
21    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
22        match self {
23            Self::InvalidPath(error) => error.fmt(f),
24            Self::PathNotFound(path) => write!(f, "path not found: {path}"),
25            Self::CannotMoveRoot => write!(f, "cannot move the root path"),
26            Self::CannotRemoveRoot => write!(f, "cannot remove the root path"),
27            Self::CannotMoveIntoDescendant { from, to } => {
28                write!(f, "cannot move {from} into its descendant {to}")
29            }
30        }
31    }
32}
33
34impl std::error::Error for Error {}
35
36impl From<TagPathError> for Error {
37    fn from(error: TagPathError) -> Self {
38        Self::InvalidPath(error)
39    }
40}
41
42/// The result type used by public mutating and querying operations.
43pub type Result<T> = std::result::Result<T, Error>;