bon_macros/util/
mod.rs

1mod attrs;
2mod expr;
3mod fn_arg;
4mod generic_param;
5mod ident;
6mod item;
7mod iterator;
8mod meta_list;
9mod path;
10mod punctuated;
11mod ty;
12mod vec;
13mod visibility;
14
15pub(crate) mod ide;
16
17pub(crate) mod prelude {
18    pub(crate) use proc_macro2::{Span, TokenStream};
19    pub(crate) use quote::{format_ident, quote, quote_spanned, ToTokens};
20
21    /// The `Error` type in in this crate is supposed to act like `anyhow::Error`
22    /// providing a simple way to create and return errors from format strings.
23    ///
24    /// See [`err!()`] and [`bail!()`] macros for creating errors. Right now this
25    /// is just a reexport of [`darling::Error`] because that error already provides
26    /// the anyhow-like error handling experience.
27    pub(crate) use darling::Error;
28
29    pub(crate) type Result<T = (), E = Error> = std::result::Result<T, E>;
30
31    pub(crate) use super::attrs::AttributeExt;
32    pub(crate) use super::expr::ExprExt;
33    pub(crate) use super::fn_arg::FnArgExt;
34    pub(crate) use super::generic_param::GenericParamExt;
35    pub(crate) use super::ident::IdentExt;
36    pub(crate) use super::item::ItemExt;
37    pub(crate) use super::iterator::{IntoIteratorExt, IteratorExt};
38    pub(crate) use super::meta_list::MetaListExt;
39    pub(crate) use super::path::PathExt;
40    pub(crate) use super::punctuated::PunctuatedExt;
41    pub(crate) use super::ty::TypeExt;
42    pub(crate) use super::vec::VecExt;
43    pub(crate) use super::visibility::VisibilityExt;
44    pub(crate) use super::{bail, err};
45}
46
47/// Inspired by `anyhow::bail`, but returns a [`Result`] with [`darling::Error`].
48/// It accepts the value that implements [`syn::spanned::Spanned`] to attach the
49/// span to the error.
50#[allow(edition_2024_expr_fragment_specifier)]
51macro_rules! bail {
52    ($spanned:expr, $($tt:tt)*) => {
53        return Err($crate::util::err!($spanned, $($tt)*))
54    };
55}
56
57/// Inspired by `anyhow::anyhow`, but returns a [`darling::Error`].
58/// It accepts the value that implements [`syn::spanned::Spanned`] to attach the
59/// span to the error.
60#[allow(edition_2024_expr_fragment_specifier)]
61macro_rules! err {
62    ($spanned:expr, $($tt:tt)*) => {
63        ::darling::Error::custom(format_args!($($tt)*)).with_span($spanned)
64    };
65}
66
67pub(crate) use {bail, err};