baichun-framework-core 0.1.0

Core module for Baichun-Rust framework
Documentation
use async_trait::async_trait;

use crate::error::Result;

/// 数据转换特征
pub trait Convert<T> {
    /// 转换为目标类型
    fn convert(&self) -> T;
}

/// 克隆转换特征
pub trait CloneInto<T: Clone> {
    /// 克隆并转换为目标类型
    fn clone_into(&self) -> T;
}

/// 初始化特征
pub trait Initialize {
    /// 初始化
    fn initialize(&mut self) -> Result<()>;
}

/// 验证特征
pub trait Validate {
    /// 验证
    fn validate(&self) -> Result<()>;
}

/// 生命周期管理特征
pub trait Lifecycle {
    /// 启动
    fn start(&mut self) -> Result<()>;
    /// 停止
    fn stop(&mut self) -> Result<()>;
    /// 重启
    fn restart(&mut self) -> Result<()>;
}

/// 异步生命周期管理特征
#[async_trait]
pub trait AsyncLifecycle {
    /// 启动
    async fn start(&mut self) -> Result<()>;
    /// 停止
    async fn stop(&mut self) -> Result<()>;
    /// 重启
    async fn restart(&mut self) -> Result<()>;
}

/// 序列化特征
pub trait Serializer {
    /// 序列化为字节数组
    fn to_bytes(&self) -> Result<Vec<u8>>;
    /// 从字节数组反序列化
    fn from_bytes(bytes: &[u8]) -> Result<Self>
    where
        Self: Sized;
}

/// 克隆特征
pub trait CloneBox {
    /// 克隆到Box
    fn clone_box(&self) -> Box<dyn CloneBox>;
}

impl<T> CloneBox for T
where
    T: 'static + Clone,
{
    fn clone_box(&self) -> Box<dyn CloneBox> {
        Box::new(self.clone())
    }
}

/// 标识特征
pub trait Identifier {
    /// 获取ID
    fn id(&self) -> String;
}

/// 命名特征
pub trait Named {
    /// 获取名称
    fn name(&self) -> String;
}

/// 描述特征
pub trait Described {
    /// 获取描述
    fn description(&self) -> String;
}

/// 版本特征
pub trait Versioned {
    /// 获取版本
    fn version(&self) -> String;
}

/// 状态特征
pub trait State {
    /// 获取状态
    fn state(&self) -> String;
    /// 是否可用
    fn is_available(&self) -> bool;
}

/// 标记特征
pub trait Tagged {
    /// 获取标签
    fn tags(&self) -> Vec<String>;
    /// 添加标签
    fn add_tag(&mut self, tag: String);
    /// 移除标签
    fn remove_tag(&mut self, tag: &str);
    /// 是否包含标签
    fn has_tag(&self, tag: &str) -> bool;
}