1use crate::BaseError;
7use std::collections::BTreeMap;
8
9#[derive(Debug, Clone, Copy, PartialEq, Eq)]
11#[non_exhaustive]
12pub enum AdminDisplayKind {
13 Menu,
15 Button,
17 List,
19 Tree,
21 Form,
23}
24
25#[derive(Debug, Clone, PartialEq, Eq)]
27#[non_exhaustive]
28pub enum AdminTarget {
29 Action {
31 module: String,
33 action: String,
35 },
36 Table {
38 module: String,
40 table: String,
42 },
43 ApiOperation {
45 operation_id: String,
47 },
48}
49
50impl AdminTarget {
51 pub fn action(module: &str, action: &str) -> Result<Self, BaseError> {
53 validate_stable_id(module)?;
54 validate_stable_id(action)?;
55 Ok(Self::Action {
56 module: module.to_string(),
57 action: action.to_string(),
58 })
59 }
60
61 pub fn table(module: &str, table: &str) -> Result<Self, BaseError> {
63 validate_stable_id(module)?;
64 validate_stable_id(table)?;
65 Ok(Self::Table {
66 module: module.to_string(),
67 table: table.to_string(),
68 })
69 }
70
71 pub fn api_operation(operation_id: &str) -> Result<Self, BaseError> {
73 validate_stable_id(operation_id)?;
74 Ok(Self::ApiOperation {
75 operation_id: operation_id.to_string(),
76 })
77 }
78}
79
80#[derive(Debug, Clone, PartialEq, Eq)]
82pub struct AdminMetadata {
83 pub id: String,
85 pub label: String,
87 pub kind: AdminDisplayKind,
89 pub target: AdminTarget,
91 pub icon: Option<String>,
93 pub group: Option<String>,
95 pub order: i32,
97}
98
99impl AdminMetadata {
100 pub fn new(
102 id: &str,
103 label: &str,
104 kind: AdminDisplayKind,
105 target: AdminTarget,
106 ) -> Result<Self, BaseError> {
107 validate_stable_id(id)?;
108 if label.trim().is_empty() {
109 return Err(BaseError::ConfigError("后台元数据标题不能为空".to_string()));
110 }
111 Ok(Self {
112 id: id.to_string(),
113 label: label.to_string(),
114 kind,
115 target,
116 icon: None,
117 group: None,
118 order: 0,
119 })
120 }
121
122 pub fn icon(mut self, icon: impl Into<String>) -> Self {
124 self.icon = Some(icon.into());
125 self
126 }
127
128 pub fn group(mut self, group: impl Into<String>) -> Self {
130 self.group = Some(group.into());
131 self
132 }
133
134 pub fn order(mut self, order: i32) -> Self {
136 self.order = order;
137 self
138 }
139}
140
141#[derive(Debug, Clone)]
143pub struct AdminMetadataRegistry {
144 entries: Vec<AdminMetadata>,
145 positions: BTreeMap<String, usize>,
146}
147
148impl AdminMetadataRegistry {
149 pub fn new(mut entries: Vec<AdminMetadata>) -> Result<Self, BaseError> {
151 entries.sort_by(|left, right| {
152 left.order
153 .cmp(&right.order)
154 .then_with(|| left.id.cmp(&right.id))
155 });
156 let mut positions = BTreeMap::new();
157 for (index, entry) in entries.iter().enumerate() {
158 if positions.insert(entry.id.clone(), index).is_some() {
159 return Err(BaseError::ConfigError(format!(
160 "后台元数据 ID 重复: {}",
161 entry.id
162 )));
163 }
164 }
165 Ok(Self { entries, positions })
166 }
167
168 pub fn entries(&self) -> &[AdminMetadata] {
170 &self.entries
171 }
172
173 pub fn get(&self, id: &str) -> Option<&AdminMetadata> {
175 self.positions
176 .get(id)
177 .and_then(|index| self.entries.get(*index))
178 }
179}
180
181fn validate_stable_id(id: &str) -> Result<(), BaseError> {
182 let mut chars = id.chars();
183 let Some(first) = chars.next() else {
184 return Err(BaseError::ConfigError("稳定 ID 不能为空".to_string()));
185 };
186 if !first.is_ascii_alphabetic()
187 || !chars.all(|ch| ch.is_ascii_alphanumeric() || matches!(ch, '_' | '-' | '.' | ':'))
188 {
189 return Err(BaseError::ConfigError(format!("非法稳定 ID: {id:?}")));
190 }
191 Ok(())
192}