lesbar_macros/
lib.rs

1#![no_std]
2
3extern crate proc_macro;
4
5use lesbar_text::StrExt as _;
6use mitsein::str1::Str1;
7use proc_macro::TokenStream;
8use syn::LitStr;
9
10#[proc_macro]
11pub fn tstr(input: TokenStream) -> TokenStream {
12    let literal = syn::parse_macro_input!(input as LitStr);
13    if literal.value().has_text() {
14        quote::quote! {
15            // SAFETY: The procedural macro that generated this code has established that the
16            //         string literal is non-empty. See `lesbar_text::StrExt::has_text`.
17            ::lesbar::tstr::TStr::from_str1_unchecked(unsafe {
18                ::mitsein::str1::Str1::from_str_unchecked(#literal)
19            })
20        }
21    }
22    else {
23        quote::quote! {
24            ::core::compile_error!("string literal has no legible text")
25        }
26    }
27    .into()
28}
29
30#[proc_macro]
31pub fn str1(input: TokenStream) -> TokenStream {
32    let literal = syn::parse_macro_input!(input as LitStr);
33    if Str1::try_from_str(literal.value().as_ref()).is_ok() {
34        quote::quote! {
35            // SAFETY: The procedural macro that generated this code has established that the
36            //         string literal is non-empty.
37            unsafe {
38                ::mitsein::str1::Str1::from_str_unchecked(#literal)
39            }
40        }
41    }
42    else {
43        quote::quote! {
44            ::core::compile_error!("string literal is empty")
45        }
46    }
47    .into()
48}