ek-core 0.5.0

Business-agnostic plugin host and actor-over-bus runtime on top of ek-event: capability index with runtime service discovery, per-plugin actor workers, dynamic add/remove with lifecycle broadcasts, one-shot ask/ask_service with contract gates, panic quarantine, five-step close.
Documentation
//! 错误体系:全部公共 API 的错误面。

use std::fmt;

/// 宿主注册/生命周期错误。
///
/// 0.2.0 变更:移除 `HostStarted` 变体——运行期动态 add 合法(新 D1);
/// 仅 `Stopped`(宿主已 close)仍拒绝一切变更。
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum PluginError {
    /// 插件名不合法(规则:首字母字母,其余 alnum/_/-,长度 1..=32)。
    IllegalName(String),
    /// 重名注册。
    Duplicate(String),
    /// deps 指向的目标在 start 时不存在(add 期不再校验——解耦注册顺序)。
    UnknownDep(String),
    /// start 时 deps 构成环(列出环上点名)。
    DepCycle(Vec<String>),
    /// get/remove 等操作目标不存在。
    UnknownName(String),
    /// 宿主已 close():一切变更被拒。
    Stopped,
}

impl fmt::Display for PluginError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::IllegalName(n) => write!(f, "illegal plugin name: {n:?}"),
            Self::Duplicate(n) => write!(f, "duplicate plugin: {n:?}"),
            Self::UnknownDep(n) => write!(f, "unknown dependency at start: {n:?}"),
            Self::DepCycle(c) => write!(f, "dependency cycle: {c:?}"),
            Self::UnknownName(n) => write!(f, "unknown plugin: {n:?}"),
            Self::Stopped => write!(f, "host closed"),
        }
    }
}
impl std::error::Error for PluginError {}

/// 请求应答(ask/ask_service)错误。
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum AskError {
    /// 目标 worker 未能在时限内回包(目标慢/挂/积压)。
    ///
    /// 0.5.0 带 `pending` = **超时那一刻目标邮箱里排了多少帧**(目标已摘 → 0)。
    /// 目的:把"慢"从猜测变成读数 —— 消费侧能显示「积压 N 帧、刷新滞后 ~X 秒」,
    /// 而不是只剩一个干巴巴的超时。
    Timeout { target: String, pending: u64 },
    /// 目标不存在/未 Running/已被检疫或移除(尽力而为的预检 + 断连快失败)。
    TargetGone { target: String },
    /// 请求的服务在全宿主中无人声明(find_service 无提供方)。
    UnknownService { service: String },
    /// 回包/请求契约不匹配(错型信封,或 B 明确回 Err(InvalidPayload))。
    InvalidPayload { target: String },
    /// 总线已停(put 失败)。
    BusStopped,
}

impl fmt::Display for AskError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Timeout { target, pending } => {
                if *pending == 0 {
                    write!(f, "ask timeout: {target}")
                } else {
                    write!(f, "ask timeout: {target}(目标积压 {pending} 帧)")
                }
            }
            Self::TargetGone { target } => write!(f, "ask target gone: {target}"),
            Self::UnknownService { service } => write!(f, "unknown service: {service}"),
            Self::InvalidPayload { target } => write!(f, "ask invalid payload/contract: {target}"),
            Self::BusStopped => write!(f, "bus stopped"),
        }
    }
}
impl std::error::Error for AskError {}