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
2023-11-06 00:49:19 +06:00

39 lines
1.0 KiB
Rust

use proc_macro2::LexError;
use syn::Ident;
pub mod macros {
macro_rules! tkn_err_inner {
($str:expr, $span:expr) => {{
let err_inner: darling::Error = darling::Error::custom($str).with_span($span);
err_inner
}};
}
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;
}
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))
}
pub fn str_literal_token(
str_literal: &str,
err_ident: &Ident,
) -> 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)
})
}