moduforge_core/
error.rs

1use crate::model::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("无效的父子关系: 子节点 {child} 不是父节点 {alleged_parent} 的子节点")]
28    InvalidParenting { child: NodeId, alleged_parent: NodeId },
29
30    /// Error occurs when attempting to replace a node with a different ID.
31    #[error("节点ID不匹配: 新节点ID({nodeid})与要替换的节点ID({new_node_id})不一致")]
32    InvalidNodeId { nodeid: NodeId, new_node_id: NodeId },
33
34    /// Error occurs when attempting to perform operations on an empty pool.
35    #[error("节点池为空")]
36    EmptyPool,
37
38    /// Error occurs when attempting to create a cycle in the node hierarchy.
39    #[error("检测到循环引用: 节点 {0} 不能成为自己的祖先")]
40    CyclicReference(NodeId),
41
42    /// Error occurs when attempting to move a node to an invalid position.
43    #[error("无效的节点移动: 无法将节点 {0} 移动到目标位置")]
44    InvalidNodeMove(NodeId),
45
46    /// Error occurs when attempting to perform operations on a locked node.
47    #[error("节点 {0} 已被锁定,无法执行操作")]
48    NodeLocked(NodeId),
49
50    /// Error occurs when attempting to perform operations on a deleted node.
51    #[error("节点 {0} 已被删除")]
52    NodeDeleted(NodeId),
53}
54
55impl PoolError {
56    /// Returns the node ID associated with this error, if any.
57    pub fn node_id(&self) -> Option<&NodeId> {
58        match self {
59            PoolError::DuplicateNodeId(id) => Some(id),
60            PoolError::ParentNotFound(id) => Some(id),
61            PoolError::ChildNotFound(id) => Some(id),
62            PoolError::NodeNotFound(id) => Some(id),
63            PoolError::OrphanNode(id) => Some(id),
64            PoolError::InvalidParenting { child, .. } => Some(child),
65            PoolError::InvalidNodeId { nodeid, .. } => Some(nodeid),
66            PoolError::CyclicReference(id) => Some(id),
67            PoolError::InvalidNodeMove(id) => Some(id),
68            PoolError::NodeLocked(id) => Some(id),
69            PoolError::NodeDeleted(id) => Some(id),
70            PoolError::EmptyPool => None,
71        }
72    }
73}
74
75/// A type alias for Result that uses PoolError as the error type.
76pub type PoolResult<T> = Result<T, PoolError>;
77
78/// A type alias for Result that uses StateError as the error type.
79pub type StateResult<T> = Result<T, StateError>;
80
81/// Represents all possible errors that can occur in state management operations.
82#[derive(Debug, thiserror::Error)]
83pub enum StateError {
84    /// Error occurs when plugin state initialization fails
85    #[error("插件状态初始化失败: {0}")]
86    PluginInitError(String),
87
88    /// Error occurs when applying plugin state fails
89    #[error("插件状态应用失败: {0}")]
90    PluginApplyError(String),
91
92    /// Error occurs when transaction application fails
93    #[error("事务应用失败: {0}")]
94    TransactionError(String),
95
96    /// Error occurs when configuration is invalid
97    #[error("配置错误: {0}")]
98    ConfigurationError(String),
99
100    /// Error occurs when field operations fail
101    #[error("字段操作失败: {0}")]
102    FieldError(String),
103
104    /// Error occurs when schema is missing or invalid
105    #[error("Schema错误: {0}")]
106    SchemaError(String),
107
108    /// Error occurs when plugin is not found
109    #[error("插件未找到: {0}")]
110    PluginNotFound(String),
111
112    /// Error occurs when plugin state is invalid
113    #[error("插件状态无效: {0}")]
114    InvalidPluginState(String),
115}