1
0
mirror of https://github.com/Sebekerga/native_api_1c.git synced 2025-06-16 23:47:43 +02:00
Files
native_api_1c/native_api_1c_macro/src/utils.rs

52 lines
1.5 KiB
Rust
Raw Normal View History

2023-10-25 11:25:14 +06:00
use proc_macro2::Span;
use proc_macro2::{LexError, TokenStream};
use syn::Ident;
2023-10-25 11:25:14 +06:00
use self::macros::tkn_err_inner;
2023-10-25 11:25:14 +06:00
pub mod macros {
macro_rules! tkn_err_inner {
($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| {
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
});
token
2023-10-25 11:25:14 +06:00
}