moduforge_model/
error.rs

1use crate::types::NodeId;
2
3/// Represents all possible errors that can occur in the node pool operations.
4#[derive(Debug, thiserror::Error)]
5pub enum PoolError {
6    /// Error occurs when attempting to add a node with an ID that already exists in the pool.
7    #[error("重复的节点 ID: {0}")]
8    DuplicateNodeId(NodeId),
9
10    /// Error occurs when trying to access a parent node that doesn't exist in the pool.
11    #[error("父节点不存在: {0}")]
12    ParentNotFound(NodeId),
13
14    /// Error occurs when trying to access a child node that doesn't exist in the pool.
15    #[error("子节点不存在: {0}")]
16    ChildNotFound(NodeId),
17
18    /// Error occurs when trying to access a node that doesn't exist in the pool.
19    #[error("节点不存在: {0}")]
20    NodeNotFound(NodeId),
21
22    /// Error occurs when a node exists in the pool but has no parent.
23    #[error("检测到孤立节点: {0}")]
24    OrphanNode(NodeId),
25
26    /// Error occurs when attempting to establish an invalid parent-child relationship.
27    #[error(
28        "无效的父子关系: 子节点 {child} 不是父节点 {alleged_parent} 的子节点"
29    )]
30    InvalidParenting { child: NodeId, alleged_parent: NodeId },
31
32    /// Error occurs when attempting to replace a node with a different ID.
33    #[error(
34        "节点ID不匹配: 新节点ID({nodeid})与要替换的节点ID({new_node_id})不一致"
35    )]
36    InvalidNodeId { nodeid: NodeId, new_node_id: NodeId },
37
38    /// Error occurs when attempting to perform operations on an empty pool.
39    #[error("节点池为空")]
40    EmptyPool,
41
42    /// Error occurs when attempting to create a cycle in the node hierarchy.
43    #[error("检测到循环引用: 节点 {0} 不能成为自己的祖先")]
44    CyclicReference(NodeId),
45
46    /// Error occurs when attempting to move a node to an invalid position.
47    #[error("无效的节点移动: 无法将节点 {0} 移动到目标位置")]
48    InvalidNodeMove(NodeId),
49
50    /// Error occurs when attempting to perform operations on a locked node.
51    #[error("节点 {0} 已被锁定,无法执行操作")]
52    NodeLocked(NodeId),
53
54    /// Error occurs when attempting to perform operations on a deleted node.
55    #[error("节点 {0} 已被删除")]
56    NodeDeleted(NodeId),
57
58    /// Error occurs when attempting to remove the root node.
59    #[error("无法删除根节点")]
60    CannotRemoveRoot,
61}
62
63impl PoolError {
64    /// Returns the node ID associated with this error, if any.
65    pub fn node_id(&self) -> Option<&NodeId> {
66        match self {
67            PoolError::DuplicateNodeId(id) => Some(id),
68            PoolError::ParentNotFound(id) => Some(id),
69            PoolError::ChildNotFound(id) => Some(id),
70            PoolError::NodeNotFound(id) => Some(id),
71            PoolError::OrphanNode(id) => Some(id),
72            PoolError::InvalidParenting { child, .. } => Some(child),
73            PoolError::InvalidNodeId { nodeid, .. } => Some(nodeid),
74            PoolError::CyclicReference(id) => Some(id),
75            PoolError::InvalidNodeMove(id) => Some(id),
76            PoolError::NodeLocked(id) => Some(id),
77            PoolError::NodeDeleted(id) => Some(id),
78            PoolError::EmptyPool => None,
79            PoolError::CannotRemoveRoot => None,
80        }
81    }
82}
83
84/// A type alias for Result that uses PoolError as the error type.
85pub type PoolResult<T> = Result<T, PoolError>;