bon_macros/util/
mod.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
mod attrs;
mod expr;
mod fn_arg;
mod generic_param;
mod ident;
mod item;
mod iterator;
mod meta_list;
mod path;
mod punctuated;
mod ty;
mod vec;
mod visibility;

pub(crate) mod ide;

use prelude::*;

pub(crate) mod prelude {
    pub(crate) use proc_macro2::{Span, TokenStream};
    pub(crate) use quote::{format_ident, quote, ToTokens};

    /// The `Error` type in in this crate is supposed to act like `anyhow::Error`
    /// providing a simple way to create and return errors from format strings.
    ///
    /// See [`err!()`] and [`bail!()`] macros for creating errors. Right now this
    /// is just a reexport of [`darling::Error`] because that error already provides
    /// the anyhow-like error handling experience.
    pub(crate) use darling::Error;

    pub(crate) type Result<T = (), E = Error> = std::result::Result<T, E>;

    pub(crate) use super::attrs::AttributeExt;
    pub(crate) use super::expr::ExprExt;
    pub(crate) use super::fn_arg::FnArgExt;
    pub(crate) use super::generic_param::GenericParamExt;
    pub(crate) use super::ident::IdentExt;
    pub(crate) use super::item::ItemExt;
    pub(crate) use super::iterator::{IntoIteratorExt, IteratorExt};
    pub(crate) use super::meta_list::MetaListExt;
    pub(crate) use super::path::PathExt;
    pub(crate) use super::punctuated::PunctuatedExt;
    pub(crate) use super::ty::TypeExt;
    pub(crate) use super::vec::VecExt;
    pub(crate) use super::visibility::VisibilityExt;
    pub(crate) use super::{bail, err};
}

/// Inspired by `anyhow::bail`, but returns a [`Result`] with [`darling::Error`].
/// It accepts the value that implements [`syn::spanned::Spanned`] to attach the
/// span to the error.
#[allow(edition_2024_expr_fragment_specifier)]
macro_rules! bail {
    ($spanned:expr, $($tt:tt)*) => {
        return Err($crate::util::err!($spanned, $($tt)*))
    };
}

/// Inspired by `anyhow::anyhow`, but returns a [`darling::Error`].
/// It accepts the value that implements [`syn::spanned::Spanned`] to attach the
/// span to the error.
#[allow(edition_2024_expr_fragment_specifier)]
macro_rules! err {
    ($spanned:expr, $($tt:tt)*) => {
        ::darling::Error::custom(format_args!($($tt)*)).with_span($spanned)
    };
}

pub(crate) use {bail, err};