373 lines
9.8 KiB
Rust
373 lines
9.8 KiB
Rust
//! 宪法宏元数据模块 - 完整实现
|
||
//!
|
||
//! 提供运行时元数据生成、反射支持和序列化功能。
|
||
|
||
use serde::{Deserialize, Serialize};
|
||
use std::collections::HashMap;
|
||
|
||
/// 函数元数据
|
||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||
pub struct FunctionMetadata {
|
||
/// 宪法条款ID
|
||
pub clause_id: &'static str,
|
||
/// 函数名称
|
||
pub function_name: &'static str,
|
||
/// 输入参数
|
||
pub inputs: &'static str,
|
||
/// 输出类型
|
||
pub output: &'static str,
|
||
/// 检查表达式
|
||
pub check_expression: &'static str,
|
||
/// 义务类型
|
||
pub obligation_type: &'static str,
|
||
}
|
||
|
||
impl FunctionMetadata {
|
||
/// 创建新的函数元数据
|
||
pub const fn new(
|
||
clause_id: &'static str,
|
||
function_name: &'static str,
|
||
inputs: &'static str,
|
||
output: &'static str,
|
||
check_expression: &'static str,
|
||
obligation_type: &'static str,
|
||
) -> Self {
|
||
Self {
|
||
clause_id,
|
||
function_name,
|
||
inputs,
|
||
output,
|
||
check_expression,
|
||
obligation_type,
|
||
}
|
||
}
|
||
|
||
/// 获取完整的函数签名
|
||
pub fn signature(&self) -> String {
|
||
format!("{}({}) -> {}", self.function_name, self.inputs, self.output)
|
||
}
|
||
|
||
/// 检查是否有检查表达式
|
||
pub fn has_check(&self) -> bool {
|
||
!self.check_expression.is_empty()
|
||
}
|
||
|
||
/// 检查是否有义务类型
|
||
pub fn has_obligation(&self) -> bool {
|
||
!self.obligation_type.is_empty()
|
||
}
|
||
|
||
/// 转换为JSON字符串
|
||
pub fn to_json(&self) -> Result<String, serde_json::Error> {
|
||
serde_json::to_string_pretty(self)
|
||
}
|
||
|
||
/// 从JSON字符串解析
|
||
/// 注意:由于包含 &'static str,实际使用时需要特殊处理
|
||
/// 这个方法仅用于测试目的
|
||
#[cfg(test)]
|
||
pub fn from_json(_json: &str) -> Result<Self, serde_json::Error> {
|
||
// 由于生命周期限制,这里返回一个默认值
|
||
Ok(Self::new("", "", "", "", "", ""))
|
||
}
|
||
}
|
||
|
||
/// 条款元数据
|
||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||
pub struct ClauseMetadata {
|
||
/// 条款ID
|
||
pub id: String,
|
||
/// 条款名称
|
||
pub name: String,
|
||
/// 条款描述
|
||
pub description: String,
|
||
/// 条款版本
|
||
pub version: String,
|
||
/// 条款参数
|
||
pub parameters: HashMap<String, ParameterMetadata>,
|
||
/// 关联函数
|
||
pub functions: Vec<String>,
|
||
/// 创建时间
|
||
pub created_at: String,
|
||
/// 更新时间
|
||
pub updated_at: String,
|
||
}
|
||
|
||
impl ClauseMetadata {
|
||
/// 创建新的条款元数据
|
||
pub fn new(id: impl Into<String>, name: impl Into<String>, description: impl Into<String>) -> Self {
|
||
let now = chrono::Utc::now().to_rfc3339();
|
||
Self {
|
||
id: id.into(),
|
||
name: name.into(),
|
||
description: description.into(),
|
||
version: "1.0.0".to_string(),
|
||
parameters: HashMap::new(),
|
||
functions: Vec::new(),
|
||
created_at: now.clone(),
|
||
updated_at: now,
|
||
}
|
||
}
|
||
|
||
/// 添加参数
|
||
pub fn add_parameter(&mut self, name: impl Into<String>, metadata: ParameterMetadata) {
|
||
self.parameters.insert(name.into(), metadata);
|
||
self.update_timestamp();
|
||
}
|
||
|
||
/// 添加关联函数
|
||
pub fn add_function(&mut self, function_name: impl Into<String>) {
|
||
self.functions.push(function_name.into());
|
||
self.update_timestamp();
|
||
}
|
||
|
||
/// 更新时间戳
|
||
fn update_timestamp(&mut self) {
|
||
self.updated_at = chrono::Utc::now().to_rfc3339();
|
||
}
|
||
|
||
/// 获取参数数量
|
||
pub fn parameter_count(&self) -> usize {
|
||
self.parameters.len()
|
||
}
|
||
|
||
/// 获取函数数量
|
||
pub fn function_count(&self) -> usize {
|
||
self.functions.len()
|
||
}
|
||
|
||
/// 转换为JSON字符串
|
||
pub fn to_json(&self) -> Result<String, serde_json::Error> {
|
||
serde_json::to_string_pretty(self)
|
||
}
|
||
|
||
/// 从JSON字符串解析
|
||
pub fn from_json(json: &str) -> Result<Self, serde_json::Error> {
|
||
serde_json::from_str(json)
|
||
}
|
||
}
|
||
|
||
/// 参数元数据
|
||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||
pub struct ParameterMetadata {
|
||
/// 参数名称
|
||
pub name: String,
|
||
/// 参数类型
|
||
pub param_type: String,
|
||
/// 参数描述
|
||
pub description: String,
|
||
/// 默认值
|
||
pub default_value: Option<String>,
|
||
/// 最小值
|
||
pub min_value: Option<String>,
|
||
/// 最大值
|
||
pub max_value: Option<String>,
|
||
/// 是否必需
|
||
pub required: bool,
|
||
}
|
||
|
||
impl ParameterMetadata {
|
||
/// 创建新的参数元数据
|
||
pub fn new(
|
||
name: impl Into<String>,
|
||
param_type: impl Into<String>,
|
||
description: impl Into<String>,
|
||
) -> Self {
|
||
Self {
|
||
name: name.into(),
|
||
param_type: param_type.into(),
|
||
description: description.into(),
|
||
default_value: None,
|
||
min_value: None,
|
||
max_value: None,
|
||
required: false,
|
||
}
|
||
}
|
||
|
||
/// 设置默认值
|
||
pub fn with_default(mut self, value: impl Into<String>) -> Self {
|
||
self.default_value = Some(value.into());
|
||
self
|
||
}
|
||
|
||
/// 设置最小值
|
||
pub fn with_min(mut self, value: impl Into<String>) -> Self {
|
||
self.min_value = Some(value.into());
|
||
self
|
||
}
|
||
|
||
/// 设置最大值
|
||
pub fn with_max(mut self, value: impl Into<String>) -> Self {
|
||
self.max_value = Some(value.into());
|
||
self
|
||
}
|
||
|
||
/// 设置为必需
|
||
pub fn required(mut self) -> Self {
|
||
self.required = true;
|
||
self
|
||
}
|
||
|
||
/// 检查值是否在范围内
|
||
pub fn is_in_range(&self, value: &str) -> bool {
|
||
// 简化版本,实际应该根据类型进行比较
|
||
if let Some(min) = &self.min_value {
|
||
if value < min {
|
||
return false;
|
||
}
|
||
}
|
||
if let Some(max) = &self.max_value {
|
||
if value > max {
|
||
return false;
|
||
}
|
||
}
|
||
true
|
||
}
|
||
}
|
||
|
||
/// 元数据注册表
|
||
#[derive(Debug, Default)]
|
||
pub struct MetadataRegistry {
|
||
/// 函数元数据映射
|
||
functions: HashMap<String, FunctionMetadata>,
|
||
/// 条款元数据映射
|
||
clauses: HashMap<String, ClauseMetadata>,
|
||
}
|
||
|
||
impl MetadataRegistry {
|
||
/// 创建新的注册表
|
||
pub fn new() -> Self {
|
||
Self::default()
|
||
}
|
||
|
||
/// 注册函数元数据
|
||
pub fn register_function(&mut self, metadata: FunctionMetadata) {
|
||
self.functions.insert(metadata.function_name.to_string(), metadata);
|
||
}
|
||
|
||
/// 注册条款元数据
|
||
pub fn register_clause(&mut self, metadata: ClauseMetadata) {
|
||
self.clauses.insert(metadata.id.clone(), metadata);
|
||
}
|
||
|
||
/// 获取函数元数据
|
||
pub fn get_function(&self, name: &str) -> Option<&FunctionMetadata> {
|
||
self.functions.get(name)
|
||
}
|
||
|
||
/// 获取条款元数据
|
||
pub fn get_clause(&self, id: &str) -> Option<&ClauseMetadata> {
|
||
self.clauses.get(id)
|
||
}
|
||
|
||
/// 获取所有函数元数据
|
||
pub fn all_functions(&self) -> Vec<&FunctionMetadata> {
|
||
self.functions.values().collect()
|
||
}
|
||
|
||
/// 获取所有条款元数据
|
||
pub fn all_clauses(&self) -> Vec<&ClauseMetadata> {
|
||
self.clauses.values().collect()
|
||
}
|
||
|
||
/// 根据条款ID查找关联函数
|
||
pub fn functions_by_clause(&self, clause_id: &str) -> Vec<&FunctionMetadata> {
|
||
self.functions
|
||
.values()
|
||
.filter(|f| f.clause_id == clause_id)
|
||
.collect()
|
||
}
|
||
|
||
/// 导出为JSON
|
||
pub fn export_json(&self) -> Result<String, serde_json::Error> {
|
||
#[derive(Serialize)]
|
||
struct Export<'a> {
|
||
functions: Vec<&'a FunctionMetadata>,
|
||
clauses: Vec<&'a ClauseMetadata>,
|
||
}
|
||
|
||
let export = Export {
|
||
functions: self.all_functions(),
|
||
clauses: self.all_clauses(),
|
||
};
|
||
|
||
serde_json::to_string_pretty(&export)
|
||
}
|
||
}
|
||
|
||
#[cfg(test)]
|
||
mod tests {
|
||
use super::*;
|
||
|
||
#[test]
|
||
fn test_function_metadata() {
|
||
let metadata = FunctionMetadata::new(
|
||
"TEST_CLAUSE",
|
||
"test_function",
|
||
"amount: u64",
|
||
"Result<(), Error>",
|
||
"amount > 0",
|
||
"test_obligation",
|
||
);
|
||
|
||
assert_eq!(metadata.clause_id, "TEST_CLAUSE");
|
||
assert_eq!(metadata.function_name, "test_function");
|
||
assert!(metadata.has_check());
|
||
assert!(metadata.has_obligation());
|
||
}
|
||
|
||
#[test]
|
||
fn test_clause_metadata() {
|
||
let mut metadata = ClauseMetadata::new(
|
||
"TEST_CLAUSE",
|
||
"Test Clause",
|
||
"A test clause for testing",
|
||
);
|
||
|
||
metadata.add_parameter(
|
||
"min_amount",
|
||
ParameterMetadata::new("min_amount", "u64", "Minimum amount")
|
||
.with_min("0")
|
||
.required(),
|
||
);
|
||
|
||
metadata.add_function("test_function");
|
||
|
||
assert_eq!(metadata.parameter_count(), 1);
|
||
assert_eq!(metadata.function_count(), 1);
|
||
}
|
||
|
||
#[test]
|
||
fn test_parameter_metadata() {
|
||
let param = ParameterMetadata::new("amount", "u64", "Transaction amount")
|
||
.with_default("0")
|
||
.with_min("0")
|
||
.with_max("1000000")
|
||
.required();
|
||
|
||
assert_eq!(param.name, "amount");
|
||
assert_eq!(param.param_type, "u64");
|
||
assert!(param.required);
|
||
assert!(param.default_value.is_some());
|
||
}
|
||
|
||
#[test]
|
||
fn test_metadata_registry() {
|
||
let mut registry = MetadataRegistry::new();
|
||
|
||
let func_metadata = FunctionMetadata::new(
|
||
"TEST_CLAUSE",
|
||
"test_function",
|
||
"amount: u64",
|
||
"Result<(), Error>",
|
||
"amount > 0",
|
||
"test_obligation",
|
||
);
|
||
|
||
registry.register_function(func_metadata);
|
||
|
||
assert!(registry.get_function("test_function").is_some());
|
||
assert_eq!(registry.all_functions().len(), 1);
|
||
}
|
||
}
|