local_fmt_macros_internal/util_macro/
arg.rs

1use std::str::FromStr;
2
3use proc_macro2::TokenStream;
4use quote::ToTokens;
5use syn::{parse::Parse, punctuated::Punctuated, spanned::Spanned, LitStr};
6
7use crate::parse::{AllocMessage, StaticMessage};
8
9pub struct Args {
10    pub texts: Punctuated<LitStr, syn::Token![,]>,
11}
12
13impl Parse for Args {
14    fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
15        Ok(Args {
16            texts: Punctuated::parse_terminated(input)?,
17        })
18    }
19}
20
21impl Args {
22    pub fn to_token(&self, is_static: bool) -> syn::Result<TokenStream> {
23        let text = self.texts.iter().fold(String::new(), |mut acc, lit| {
24            acc.push_str(&lit.value());
25            acc
26        });
27
28        if is_static {
29            StaticMessage::from_str(&text)
30                .map_err(|v| syn::Error::new(self.texts.span(), v))
31                .map(|v| v.into_token_stream())
32        } else {
33            AllocMessage::from_str(&text)
34                .map_err(|v| syn::Error::new(self.texts.span(), v))
35                .map(|v| v.into_token_stream())
36        }
37    }
38}