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