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
498 每月下载量
在 3 crates 中使用
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
作为序列化二进制协议的错误,为枚举的任何成员提供以下装饰。
#[from_std_io_error]
自动生成从std::io::Error
到String
的From
实现。#[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