Skip to main content

base_web_content_macros/
lib.rs

1// Released under the MIT License.
2// Copyright, 2026, by Samuel Williams.
3
4//! The derive macros for `base-web-content` declarations. Use them through `base_web_content`, rather than depending on this crate directly.
5
6use proc_macro::TokenStream;
7use syn::{DeriveInput, Error};
8
9mod attributes;
10mod content;
11mod field_value;
12
13#[cfg(test)]
14mod tests;
15
16/// Derive a declaration of the content a struct accepts, from its named fields.
17///
18/// See `base_web_content::Content` for the field types and attributes it accepts.
19#[proc_macro_derive(Content, attributes(content))]
20pub fn derive_content(input: TokenStream) -> TokenStream {
21    // A derive's input always parses as an item, so errors come from the declarations.
22    syn::parse::<DeriveInput>(input)
23        .and_then(|input| content::expand(&input))
24        .unwrap_or_else(Error::into_compile_error)
25        .into()
26}
27
28/// Derive the conversion of an enumeration from a field's text: the exact, case-sensitive spelling of each variant.
29///
30/// See `base_web_content::FieldValue` for the spellings and attributes it accepts.
31#[proc_macro_derive(FieldValue, attributes(content))]
32pub fn derive_field_value(input: TokenStream) -> TokenStream {
33    syn::parse::<DeriveInput>(input)
34        .and_then(|input| field_value::expand(&input))
35        .unwrap_or_else(Error::into_compile_error)
36        .into()
37}