#协议 #错误 # #二进制 #IO错误 #辅助 #枚举

netgauze-serde-macros

辅助宏,使序列/反序列二进制协议更加容易

5 个版本 (3 个重大更改)

0.4.1 2024年8月8日
0.4.0 2024年8月7日
0.3.0 2024年1月31日
0.2.0 2023年10月9日
0.1.0 2023年3月5日

#49#io-error

Download history 11/week @ 2024-05-03 4/week @ 2024-05-10 19/week @ 2024-05-17 26/week @ 2024-05-24 19/week @ 2024-05-31 19/week @ 2024-06-07 23/week @ 2024-06-14 26/week @ 2024-06-21 7/week @ 2024-06-28 15/week @ 2024-07-05 37/week @ 2024-07-12 77/week @ 2024-07-19 65/week @ 2024-07-26 313/week @ 2024-08-02 74/week @ 2024-08-09 21/week @ 2024-08-16

498 每月下载量
3 crates 中使用

Apache-2.0

22KB
336

辅助宏,使序列/反序列二进制协议更加容易

LocatedError:对于给定的错误枚举 {Name},生成一个名为 Located{Name} 的结构体,该结构体携带错误(输入流中的错误位置)信息。此外,还为 nom 库错误、外部错误和另一个定位错误生成 From

use netgauze_serde_macros::LocatedError;

#[derive(LocatedError, Eq, PartialEq, Clone, Debug)]
pub enum ExtendedCommunityParsingError {
    NomError(#[from_nom] nom::error::ErrorKind),
    CommunityError(#[from_located(module = "self")] CommunityParsingError),
    UndefinedCapabilityCode(#[from_external] UndefinedBgpCapabilityCode),
}

#[derive(LocatedError, Eq, PartialEq, Clone, Debug)]
pub enum CommunityParsingError {
    NomError(#[from_nom] nom::error::ErrorKind),
}

#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub struct UndefinedBgpCapabilityCode(pub u8);

fn test() {
    let span = Span::new(&[1, 2, 3]);
    // LocatedExtendedCommunityParsingError is generated by LocatedError
    let _located = LocatedExtendedCommunityParsingError::new(
        span,
        ExtendedCommunityParsingError::UndefinedCapabilityCode(UndefinedBgpCapabilityCode(1)));
}

WritingError:装饰一个 enum 作为序列化二进制协议的错误,为枚举的任何成员提供以下装饰。

  1. #[from_std_io_error] 自动生成从 std::io::ErrorStringFrom 实现。
  2. #[from],自动为给定类型生成一个 From 实现。

示例

use netgauze_serde_macros::WritingError;

#[derive(WritingError, Eq, PartialEq, Clone, Debug)]
pub enum BgpMessageWritingError {
    /// std::io::Error will be converted to this value
    StdIOError(#[from_std_io_error] String),

    /// BgpOpenMessageWritingError will be converted to this value
    OpenError(#[from] BgpOpenMessageWritingError),
}

依赖项

~255–700KB
~17K SLoC