#protocols #error #macro #binary #serde #io-error #enums

macro netgauze-serde-macros

Helper macros to make ser/deser binary protocols easier

6 releases (3 breaking)

0.4.1 Aug 8, 2024
0.4.0 Aug 7, 2024
0.3.1 Oct 10, 2024
0.3.0 Jan 31, 2024
0.1.0 Mar 5, 2023

#48 in #io-error

Download history 24/week @ 2024-06-24 23/week @ 2024-07-08 42/week @ 2024-07-15 76/week @ 2024-07-22 71/week @ 2024-07-29 352/week @ 2024-08-05 23/week @ 2024-08-12 26/week @ 2024-08-19 45/week @ 2024-08-26 11/week @ 2024-09-09 62/week @ 2024-09-16 85/week @ 2024-09-23 101/week @ 2024-09-30 200/week @ 2024-10-07

448 downloads per month
Used in 3 crates

Apache-2.0

22KB
336 lines

Helper macros to make ser/deser binary protocols easier

LocatedError: For a given error enum {Name} generate a struct called Located{Name} that carries the Span (the error location in the input stream) info along the error. Additionally, generates From for nom library errors, external, and another located errors.

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: Decorate an enum as an error for serializing binary protocol provides the following decorations for any members of the enum.

  1. #[from_std_io_error] automatically generate From implementation from std::io::Error to a String.
  2. #[from], automatically generates a From implementation for a given type.

Example:

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),
}

Dependencies

~230–670KB
~16K SLoC