2023-11-06 00:49:19 +06:00
|
|
|
use proc_macro2::LexError;
|
2023-11-05 21:02:09 +06:00
|
|
|
use syn::Ident;
|
2023-10-25 11:25:14 +06:00
|
|
|
|
|
|
|
pub mod macros {
|
|
|
|
macro_rules! tkn_err_inner {
|
2023-11-05 21:02:09 +06:00
|
|
|
($str:expr, $span:expr) => {{
|
2023-11-06 00:49:19 +06:00
|
|
|
let err_inner: darling::Error = darling::Error::custom($str).with_span($span);
|
2023-11-05 21:02:09 +06:00
|
|
|
err_inner
|
|
|
|
}};
|
2023-10-25 11:25:14 +06:00
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! tkn_err {
|
|
|
|
($str:expr, $span:expr) => {
|
|
|
|
Err(crate::utils::macros::tkn_err_inner!($str, $span))
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) use tkn_err;
|
|
|
|
pub(crate) use tkn_err_inner;
|
|
|
|
}
|
|
|
|
|
2023-11-03 09:29:57 +06:00
|
|
|
const IDENT_OPTION_ERR: &str = "Unable to get ident from option";
|
|
|
|
|
|
|
|
pub fn ident_option_to_darling_err(ident: Option<&Ident>) -> Result<&Ident, darling::Error> {
|
|
|
|
ident.ok_or_else(|| darling::Error::custom(IDENT_OPTION_ERR))
|
|
|
|
}
|
|
|
|
|
2023-10-25 11:25:14 +06:00
|
|
|
pub fn str_literal_token(
|
|
|
|
str_literal: &str,
|
|
|
|
err_ident: &Ident,
|
2023-11-06 00:49:19 +06:00
|
|
|
) -> Result<proc_macro2::TokenStream, darling::Error> {
|
|
|
|
format!(r#""{}""#, str_literal)
|
|
|
|
.parse()
|
|
|
|
.map_err(|e: LexError| {
|
|
|
|
darling::Error::custom(format!("Unable to parse string literal: {}", e))
|
|
|
|
.with_span(err_ident)
|
|
|
|
})
|
2023-10-25 11:25:14 +06:00
|
|
|
}
|