darra-ethercat-master 2.0.5

商业 EtherCAT 主站协议栈 · 实时内核驱动 · 抖动 1µs · Windows + Linux · 多编程语言 · 全协议 · 支持复杂拓扑 + 热插拔 · ethercat.darra.xyz · Commercial EtherCAT Master protocol stack · Real-time kernel driver · 1µs jitter · Multi-platform · Multi-language · Complex topology + hot-plug.
//! `Display` 增强语法糖
//!
//! 主 SDK 已为 `EcState` / `DarraError` / `EcPortType` 等少数枚举实现 `Display`,
//! 但 `LinkState` / `RedundancyState` / `EcALState` / `CiA402State` / `CiA402Mode` /
//! `Slave` / `SlaveIdentity` 仍只能用 `{:?}` 打印, 输出冗长且对最终用户不友好.
//!
//! 本模块统一补齐这些类型的 `Display` 实现, 让 `println!("{}", state)` 输出
//! 简短可读的字符串, 例如 `Connected` / `OP+Error` / `0x88A4 (Profinet)`.
//!
//! # 示例
//!
//! ```no_run
//! use darra_ethercat::sugar::prelude::*;
//! use darra_ethercat::{LinkState, RedundancyState};
//! use darra_ethercat::data::error::{CiA402State, CiA402Mode};
//!
//! println!("链路: {}", LinkState::Connected);          // 链路: Connected
//! println!("冗余: {}", RedundancyState::Both);         // 冗余: Both
//! println!("驱动: {}", CiA402State::OperationEnabled); // 驱动: OperationEnabled
//! println!("模式: {}", CiA402Mode::CSP);                // 模式: CSP
//! ```

use crate::data::error::{LinkState, RedundancyState, CiA402State, CiA402Mode};
use crate::data::types::EcALState;
use crate::utils::ffi::SlaveIdentity;
use std::fmt;

// ============================================================
// LinkState
// ============================================================

impl fmt::Display for LinkState {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let s: &'static str = (*self).into();
        f.write_str(s)
    }
}

// ============================================================
// RedundancyState
// ============================================================

impl fmt::Display for RedundancyState {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let s: &'static str = (*self).into();
        f.write_str(s)
    }
}

// ============================================================
// CiA402State
// ============================================================

impl fmt::Display for CiA402State {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let name = match self {
            CiA402State::NotReady => "NotReady",
            CiA402State::SwitchOnDisabled => "SwitchOnDisabled",
            CiA402State::ReadyToSwitchOn => "ReadyToSwitchOn",
            CiA402State::SwitchedOn => "SwitchedOn",
            CiA402State::OperationEnabled => "OperationEnabled",
            CiA402State::QuickStopActive => "QuickStopActive",
            CiA402State::FaultReaction => "FaultReaction",
            CiA402State::Fault => "Fault",
            CiA402State::Unknown => "Unknown",
        };
        f.write_str(name)
    }
}

// ============================================================
// CiA402Mode
// ============================================================

impl fmt::Display for CiA402Mode {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let name = match self {
            CiA402Mode::PP => "PP",
            CiA402Mode::VL => "VL",
            CiA402Mode::PV => "PV",
            CiA402Mode::PT => "PT",
            CiA402Mode::HM => "HM",
            CiA402Mode::IP => "IP",
            CiA402Mode::CSP => "CSP",
            CiA402Mode::CSV => "CSV",
            CiA402Mode::CST => "CST",
        };
        f.write_str(name)
    }
}

// ============================================================
// EcALState (列出最常见的, 其它走 0xXXXX hex 兜底)
// ============================================================

impl fmt::Display for EcALState {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let name = match self {
            EcALState::NoError => "NoError",
            EcALState::UnspecifiedError => "UnspecifiedError",
            EcALState::NoMemory => "NoMemory",
            EcALState::InvalidDeviceSetup => "InvalidDeviceSetup",
            EcALState::InvalidRevision => "InvalidRevision",
            EcALState::SiiEepromMismatch => "SiiEepromMismatch",
            EcALState::FirmwareUpdateFailed => "FirmwareUpdateFailed",
            EcALState::LicenseError => "LicenseError",
            EcALState::InvalidStateChange => "InvalidStateChange",
            EcALState::UnknownRequestedState => "UnknownRequestedState",
            EcALState::BootstrapNotSupported => "BootstrapNotSupported",
            EcALState::NoValidFirmware => "NoValidFirmware",
            EcALState::InvalidMailboxConfig => "InvalidMailboxConfig",
            EcALState::InvalidMailBoxConfig => "InvalidMailBoxConfig",
            EcALState::InvalidSyncManagerConfig => "InvalidSyncManagerConfig",
            EcALState::NoValidInputs => "NoValidInputs",
            EcALState::NoValidOutputs => "NoValidOutputs",
            EcALState::SyncError => "SyncError",
            EcALState::SyncManagerWatchdog => "SyncManagerWatchdog",
            EcALState::InvalidSyncManagerTypes => "InvalidSyncManagerTypes",
            EcALState::InvalidOutputConfig => "InvalidOutputConfig",
            EcALState::InvalidInputConfig => "InvalidInputConfig",
            EcALState::InvalidWatchdogConfig => "InvalidWatchdogConfig",
            EcALState::NeedsColdStart => "NeedsColdStart",
            EcALState::NeedsInit => "NeedsInit",
            EcALState::NeedsPreOp => "NeedsPreOp",
            EcALState::NeedsSafeOp => "NeedsSafeOp",
            EcALState::Unknown => return write!(f, "Unknown(0xFFFF)"),
            other => return write!(f, "AL(0x{:04X})", *other as u16),
        };
        f.write_str(name)
    }
}

// 注: `Slave` 已在 `slave/core.rs:2024` 实现 `Display`, 此处不重复定义.

// ============================================================
// SlaveIdentity (vendor/product/rev/serial 一行打印)
// ============================================================

impl fmt::Display for SlaveIdentity {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "Vendor=0x{:08X} Product=0x{:08X} Rev=0x{:08X} Serial=0x{:08X}",
            self.vendor_id, self.product_code, self.revision_no, self.serial_no
        )
    }
}