2023-10-25 11:25:14 +06:00
|
|
|
use proc_macro2::Span;
|
2023-11-05 21:02:09 +06:00
|
|
|
use proc_macro2::{LexError, TokenStream};
|
|
|
|
use syn::Ident;
|
2023-10-25 11:25:14 +06:00
|
|
|
|
2023-11-05 21:02:09 +06:00
|
|
|
use self::macros::tkn_err_inner;
|
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) => {{
|
|
|
|
let err_inner: proc_macro2::TokenStream =
|
|
|
|
syn::Error::new($span, $str).to_compile_error().into();
|
|
|
|
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_token_err(ident: Option<&Ident>) -> Result<&Ident, TokenStream> {
|
|
|
|
ident.ok_or(tkn_err_inner!(IDENT_OPTION_ERR, Span::call_site()))
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
|
|
|
) -> Result<proc_macro2::TokenStream, TokenStream> {
|
|
|
|
let token: Result<TokenStream, TokenStream> =
|
|
|
|
format!(r#""{}""#, str_literal)
|
|
|
|
.parse()
|
|
|
|
.map_err(|e: LexError| {
|
2023-11-05 21:02:09 +06:00
|
|
|
let token2: TokenStream =
|
|
|
|
Err(syn::Error::new(err_ident.span(), format!("LexErr: {}", e))
|
|
|
|
.to_compile_error())
|
|
|
|
.unwrap();
|
2023-10-25 11:25:14 +06:00
|
|
|
token2
|
|
|
|
});
|
2023-11-05 21:02:09 +06:00
|
|
|
token
|
2023-10-25 11:25:14 +06:00
|
|
|
}
|