Skip to main content

dynamic_config_macros/
lib.rs

1//! Procedural macro implementation for [`dynamic-config`](https://docs.rs/dynamic-config).
2//!
3//! Do not depend on this crate directly — it is an implementation detail of
4//! `dynamic-config`, which re-exports everything here and provides the runtime
5//! the generated code calls into. The two are released together and pinned to
6//! the same version.
7//!
8//! A procedural macro can only be defined in a crate with `proc-macro = true`,
9//! which is the sole reason this crate exists separately.
10
11#![forbid(unsafe_code)]
12#![deny(missing_docs)]
13
14mod args;
15mod expand;
16
17use proc_macro::TokenStream;
18use syn::{parse_macro_input, ItemStruct};
19
20/// Turns a struct into a hot-reloadable configuration snapshot.
21///
22/// Documented on the re-export in `dynamic_config`, which is where callers
23/// should read it.
24#[proc_macro_attribute]
25pub fn dynamic_config(attr: TokenStream, item: TokenStream) -> TokenStream {
26    let args = parse_macro_input!(attr as args::Args);
27    let input = parse_macro_input!(item as ItemStruct);
28
29    match expand::expand(args, input) {
30        Ok(tokens) => tokens.into(),
31        Err(error) => error.into_compile_error().into(),
32    }
33}