mirror of
https://github.com/juspay/hyperswitch.git
synced 2025-11-03 21:37:41 +08:00
initial commit
This commit is contained in:
33
crates/router_derive/src/macros/helpers.rs
Normal file
33
crates/router_derive/src/macros/helpers.rs
Normal file
@ -0,0 +1,33 @@
|
||||
use proc_macro2::Span;
|
||||
use quote::ToTokens;
|
||||
use syn::{parse::Parse, punctuated::Punctuated, spanned::Spanned, Attribute, Token};
|
||||
|
||||
pub fn non_enum_error() -> syn::Error {
|
||||
syn::Error::new(Span::call_site(), "This macro only supports enums.")
|
||||
}
|
||||
|
||||
pub(super) fn occurrence_error<T: ToTokens>(
|
||||
first_keyword: T,
|
||||
second_keyword: T,
|
||||
attr: &str,
|
||||
) -> syn::Error {
|
||||
let mut error = syn::Error::new_spanned(
|
||||
second_keyword,
|
||||
format!("Found multiple occurrences of error({})", attr),
|
||||
);
|
||||
error.combine(syn::Error::new_spanned(first_keyword, "first one here"));
|
||||
error
|
||||
}
|
||||
|
||||
pub(super) fn get_metadata_inner<'a, T: Parse + Spanned>(
|
||||
ident: &str,
|
||||
attrs: impl IntoIterator<Item = &'a Attribute>,
|
||||
) -> syn::Result<Vec<T>> {
|
||||
attrs
|
||||
.into_iter()
|
||||
.filter(|attr| attr.path.is_ident(ident))
|
||||
.try_fold(Vec::new(), |mut vec, attr| {
|
||||
vec.extend(attr.parse_args_with(Punctuated::<T, Token![,]>::parse_terminated)?);
|
||||
Ok(vec)
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user