#error #syn #optional #proc #proc-macro #macro

optional-error

Simple crate to more easily work with an optional syn::Error

2 releases

0.1.1 Mar 13, 2023
0.1.0 Mar 13, 2023

#2046 in Procedural macros

Download history 3/week @ 2024-01-25 5/week @ 2024-02-01 1/week @ 2024-02-08 39/week @ 2024-02-15 46/week @ 2024-02-22 38/week @ 2024-02-29 36/week @ 2024-03-07 28/week @ 2024-03-14 26/week @ 2024-03-21 54/week @ 2024-03-28 38/week @ 2024-04-04 20/week @ 2024-04-11 16/week @ 2024-04-18 20/week @ 2024-04-25 18/week @ 2024-05-02 15/week @ 2024-05-09

72 downloads per month
Used in 3 crates (via bevy_proto_derive)

MIT/Apache

9KB
65 lines

optional-error

crates.io docs.rs

This crate provides a simpler way to create and manage an Option<syn::Error>.

fn parse(input: DeriveInput) -> Result<TokenStream, syn::Error> {
    // Create an optional error to contain zero or more errors
    let mut errors = OptionalError::default();

    if !matches!(input.vis, Visibility::Public(_)) {
        // Combine with a new error (or initialize if empty)
        errors.combine(syn::Error::new(Span::call_site(), "input must be marked `pub`"));
    }

    match input.data {
        syn::Data::Struct(_) | syn::Data::Enum(_) => { /* ... */ }
        syn::Data::Union(_) => {
            // Combine some more!
            errors.combine(syn::Error::new(Span::call_site(), "unions not supported"));
        }
    }

    // Easy early return with all errors (if any)
    errors.try_throw()?;

    // ...
}

Dependencies

~1.5MB
~33K SLoC