Skip to main content

eure_macros/
lib.rs

1use darling::FromDeriveInput;
2use syn::parse_macro_input;
3
4use crate::{attrs::ContainerAttrs, config::MacroConfig, context::MacroContext};
5
6mod attrs;
7mod build_schema;
8pub(crate) mod config;
9pub(crate) mod context;
10mod from_eure;
11mod into_eure;
12mod must_be_text;
13
14#[proc_macro_derive(IntoEure, attributes(eure))]
15pub fn into_eure_derive(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
16    let input = parse_macro_input!(input as syn::DeriveInput);
17    into_eure::derive(create_context(input)).into()
18}
19
20#[proc_macro_derive(FromEure, attributes(eure))]
21pub fn from_eure_derive(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
22    let input = parse_macro_input!(input as syn::DeriveInput);
23    from_eure::derive(create_context(input)).into()
24}
25
26#[proc_macro_derive(BuildSchema, attributes(eure))]
27pub fn build_schema_derive(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
28    let input = parse_macro_input!(input as syn::DeriveInput);
29    build_schema::derive(create_context(input)).into()
30}
31
32fn create_context(input: syn::DeriveInput) -> MacroContext {
33    let attrs = ContainerAttrs::from_derive_input(&input).expect("Failed to parse eure attributes");
34    MacroContext::new(MacroConfig::from_attrs(attrs), input)
35}
36
37/// Creates a zero-sized type that only parses from a specific Text value.
38///
39/// # Syntax
40///
41/// ```ignore
42/// MustBeText!("content")           // Implicit language: `content`
43/// MustBeText!(plaintext, "content") // Plaintext language: "content"
44/// MustBeText!(rust, "content")      // Other language: rust`content`
45/// ```
46///
47/// # Example
48///
49/// ```ignore
50/// use eure_macros::MustBeText;
51///
52/// // This type only successfully parses from the text value `any`
53/// let marker = MustBeText!("any");
54/// ```
55#[proc_macro]
56#[allow(non_snake_case)]
57pub fn MustBeText(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
58    let input = parse_macro_input!(input as must_be_text::MustBeTextInput);
59    must_be_text::expand(input).into()
60}