1use crate::types::NodeId;
2
3pub mod error_messages {
5 pub const DUPLICATE_NODE: &str = "重复的节点 ID";
6 pub const PARENT_NOT_FOUND: &str = "父节点不存在";
7 pub const CHILD_NOT_FOUND: &str = "子节点不存在";
8 pub const NODE_NOT_FOUND: &str = "节点不存在";
9 pub const ORPHAN_NODE: &str = "检测到孤立节点";
10 pub const INVALID_PARENTING: &str = "无效的父子关系";
11 pub const INVALID_NODE_ID: &str = "节点ID不匹配";
12 pub const EMPTY_POOL: &str = "节点池为空";
13 pub const CYCLIC_REFERENCE: &str = "检测到循环引用";
14 pub const INVALID_NODE_MOVE: &str = "无效的节点移动";
15 pub const NODE_LOCKED: &str = "节点已被锁定,无法执行操作";
16 pub const NODE_DELETED: &str = "节点已被删除";
17 pub const CANNOT_REMOVE_ROOT: &str = "无法删除根节点";
18}
19
20pub mod error_helpers {
22 use super::*;
23
24 pub fn duplicate_node(id: NodeId) -> anyhow::Error {
25 anyhow::anyhow!("{}: {}", error_messages::DUPLICATE_NODE, id)
26 }
27 pub fn schema_error(msg: &str) -> anyhow::Error {
28 anyhow::anyhow!("schema 错误:{}", msg)
29 }
30
31 pub fn parent_not_found(id: NodeId) -> anyhow::Error {
32 anyhow::anyhow!("{}: {}", error_messages::PARENT_NOT_FOUND, id)
33 }
34
35 pub fn child_not_found(id: NodeId) -> anyhow::Error {
36 anyhow::anyhow!("{}: {}", error_messages::CHILD_NOT_FOUND, id)
37 }
38
39 pub fn node_not_found(id: NodeId) -> anyhow::Error {
40 anyhow::anyhow!("{}: {}", error_messages::NODE_NOT_FOUND, id)
41 }
42
43 pub fn orphan_node(id: NodeId) -> anyhow::Error {
44 anyhow::anyhow!("{}: {}", error_messages::ORPHAN_NODE, id)
45 }
46
47 pub fn invalid_parenting(
48 child: NodeId,
49 alleged_parent: NodeId,
50 ) -> anyhow::Error {
51 anyhow::anyhow!(
52 "{}: 子节点 {} 不是父节点 {} 的子节点",
53 error_messages::INVALID_PARENTING,
54 child,
55 alleged_parent
56 )
57 }
58
59 pub fn invalid_node_id(
60 nodeid: NodeId,
61 new_node_id: NodeId,
62 ) -> anyhow::Error {
63 anyhow::anyhow!(
64 "{}: 新节点ID({})与要替换的节点ID({})不一致",
65 error_messages::INVALID_NODE_ID,
66 nodeid,
67 new_node_id
68 )
69 }
70
71 pub fn empty_pool() -> anyhow::Error {
72 anyhow::anyhow!(error_messages::EMPTY_POOL)
73 }
74
75 pub fn cyclic_reference(id: NodeId) -> anyhow::Error {
76 anyhow::anyhow!(
77 "{}: 节点 {} 不能成为自己的祖先",
78 error_messages::CYCLIC_REFERENCE,
79 id
80 )
81 }
82
83 pub fn invalid_node_move(id: NodeId) -> anyhow::Error {
84 anyhow::anyhow!(
85 "{}: 无法将节点 {} 移动到目标位置",
86 error_messages::INVALID_NODE_MOVE,
87 id
88 )
89 }
90
91 pub fn node_locked(id: NodeId) -> anyhow::Error {
92 anyhow::anyhow!("{}: {}", error_messages::NODE_LOCKED, id)
93 }
94
95 pub fn node_deleted(id: NodeId) -> anyhow::Error {
96 anyhow::anyhow!("{}: {}", error_messages::NODE_DELETED, id)
97 }
98
99 pub fn cannot_remove_root() -> anyhow::Error {
100 anyhow::anyhow!(error_messages::CANNOT_REMOVE_ROOT)
101 }
102}
103
104pub type PoolResult<T> = anyhow::Result<T>;