use crate::action::PermissionMode;
use crate::error::BaseError;
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct RouteDescriptor {
pub method: String,
pub path: String,
pub operation_id: String,
pub request_content_types: Vec<String>,
pub response_content_types: Vec<String>,
pub success_status: u16,
pub tags: Vec<String>,
}
impl RouteDescriptor {
pub fn new(
method: impl Into<String>,
path: impl Into<String>,
operation_id: impl Into<String>,
) -> Result<Self, BaseError> {
let method = method.into().trim().to_ascii_uppercase();
let path = path.into().trim().to_string();
let operation_id = operation_id.into().trim().to_string();
let descriptor = Self {
method,
path,
operation_id,
request_content_types: vec!["application/json".to_string()],
response_content_types: vec!["application/json".to_string()],
success_status: 200,
tags: Vec::new(),
};
descriptor.validate()?;
Ok(descriptor)
}
pub fn validate(&self) -> Result<(), BaseError> {
if self.method.is_empty()
|| self.method != self.method.to_ascii_uppercase()
|| !self
.method
.bytes()
.all(|byte| byte.is_ascii_alphabetic() || byte == b'-')
{
return Err(BaseError::ConfigError("route method 非法".to_string()));
}
if !self.path.starts_with('/')
|| self.path.contains(['?', '#'])
|| self.path.chars().any(char::is_whitespace)
{
return Err(BaseError::ConfigError(
"route path 必须是无 query/fragment 的绝对路径".to_string(),
));
}
if self.operation_id.trim().is_empty() {
return Err(BaseError::ConfigError("operation_id 不能为空".to_string()));
}
validate_non_blank_unique("request content type", &self.request_content_types, false)?;
validate_non_blank_unique("response content type", &self.response_content_types, false)?;
validate_non_blank_unique("route tag", &self.tags, true)?;
if !(100..=599).contains(&self.success_status) {
return Err(BaseError::ConfigError(
"success status 必须在 100..=599".to_string(),
));
}
Ok(())
}
pub fn with_request_content_types(mut self, values: Vec<String>) -> Result<Self, BaseError> {
validate_non_blank_unique("request content type", &values, false)?;
self.request_content_types = values;
Ok(self)
}
pub fn with_response_content_types(mut self, values: Vec<String>) -> Result<Self, BaseError> {
validate_non_blank_unique("response content type", &values, false)?;
self.response_content_types = values;
Ok(self)
}
pub fn with_success_status(mut self, status: u16) -> Result<Self, BaseError> {
if !(100..=599).contains(&status) {
return Err(BaseError::ConfigError(
"success status 必须在 100..=599".to_string(),
));
}
self.success_status = status;
Ok(self)
}
pub fn with_tags(mut self, tags: Vec<String>) -> Result<Self, BaseError> {
validate_non_blank_unique("route tag", &tags, true)?;
self.tags = tags;
Ok(self)
}
}
fn validate_non_blank_unique(
label: &str,
values: &[String],
allow_empty: bool,
) -> Result<(), BaseError> {
if (!allow_empty && values.is_empty()) || values.iter().any(|value| value.trim().is_empty()) {
return Err(BaseError::ConfigError(format!("{label} 不能为空")));
}
let mut sorted = values.to_vec();
sorted.sort();
if sorted.windows(2).any(|pair| pair[0] == pair[1]) {
return Err(BaseError::ConfigError(format!("{label} 不能重复")));
}
Ok(())
}
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct ActionDescriptor {
pub name: String,
pub display_name: String,
pub description: String,
pub permissions: Vec<String>,
pub permission_mode: PermissionMode,
pub is_public: bool,
pub input_schema: schemars::schema::RootSchema,
pub output_schema: schemars::schema::RootSchema,
pub route: RouteDescriptor,
}
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct ModuleDescriptor {
pub name: String,
pub display_name: String,
pub default_permissions: Vec<String>,
pub default_permission_mode: PermissionMode,
pub actions: Vec<ActionDescriptor>,
}
#[derive(Debug, Clone, Default)]
#[non_exhaustive]
pub struct ApiCatalog {
pub modules: Vec<ModuleDescriptor>,
}