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