use crate::BaseError;
use std::collections::BTreeMap;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum AdminDisplayKind {
Menu,
Button,
List,
Tree,
Form,
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum AdminTarget {
Action {
module: String,
action: String,
},
Table {
module: String,
table: String,
},
ApiOperation {
operation_id: String,
},
}
impl AdminTarget {
pub fn action(module: &str, action: &str) -> Result<Self, BaseError> {
validate_stable_id(module)?;
validate_stable_id(action)?;
Ok(Self::Action {
module: module.to_string(),
action: action.to_string(),
})
}
pub fn table(module: &str, table: &str) -> Result<Self, BaseError> {
validate_stable_id(module)?;
validate_stable_id(table)?;
Ok(Self::Table {
module: module.to_string(),
table: table.to_string(),
})
}
pub fn api_operation(operation_id: &str) -> Result<Self, BaseError> {
validate_stable_id(operation_id)?;
Ok(Self::ApiOperation {
operation_id: operation_id.to_string(),
})
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AdminMetadata {
pub id: String,
pub label: String,
pub kind: AdminDisplayKind,
pub target: AdminTarget,
pub icon: Option<String>,
pub group: Option<String>,
pub order: i32,
}
impl AdminMetadata {
pub fn new(
id: &str,
label: &str,
kind: AdminDisplayKind,
target: AdminTarget,
) -> Result<Self, BaseError> {
validate_stable_id(id)?;
if label.trim().is_empty() {
return Err(BaseError::ConfigError("后台元数据标题不能为空".to_string()));
}
Ok(Self {
id: id.to_string(),
label: label.to_string(),
kind,
target,
icon: None,
group: None,
order: 0,
})
}
pub fn icon(mut self, icon: impl Into<String>) -> Self {
self.icon = Some(icon.into());
self
}
pub fn group(mut self, group: impl Into<String>) -> Self {
self.group = Some(group.into());
self
}
pub fn order(mut self, order: i32) -> Self {
self.order = order;
self
}
}
#[derive(Debug, Clone)]
pub struct AdminMetadataRegistry {
entries: Vec<AdminMetadata>,
positions: BTreeMap<String, usize>,
}
impl AdminMetadataRegistry {
pub fn new(mut entries: Vec<AdminMetadata>) -> Result<Self, BaseError> {
entries.sort_by(|left, right| {
left.order
.cmp(&right.order)
.then_with(|| left.id.cmp(&right.id))
});
let mut positions = BTreeMap::new();
for (index, entry) in entries.iter().enumerate() {
if positions.insert(entry.id.clone(), index).is_some() {
return Err(BaseError::ConfigError(format!(
"后台元数据 ID 重复: {}",
entry.id
)));
}
}
Ok(Self { entries, positions })
}
pub fn entries(&self) -> &[AdminMetadata] {
&self.entries
}
pub fn get(&self, id: &str) -> Option<&AdminMetadata> {
self.positions
.get(id)
.and_then(|index| self.entries.get(*index))
}
}
fn validate_stable_id(id: &str) -> Result<(), BaseError> {
let mut chars = id.chars();
let Some(first) = chars.next() else {
return Err(BaseError::ConfigError("稳定 ID 不能为空".to_string()));
};
if !first.is_ascii_alphabetic()
|| !chars.all(|ch| ch.is_ascii_alphanumeric() || matches!(ch, '_' | '-' | '.' | ':'))
{
return Err(BaseError::ConfigError(format!("非法稳定 ID: {id:?}")));
}
Ok(())
}