facet_macros/
lib.rs

1#![doc = include_str!("../README.md")]
2
3#[proc_macro_derive(Facet, attributes(facet))]
4pub fn facet_macros(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
5    facet_macros_impl::facet_macros(input.into()).into()
6}
7
8/// Internal proc macro for extension attribute resolution.
9///
10/// This is called by the `Facet` derive macro to forward extension attributes
11/// to their respective crate's dispatcher macro while preserving spans for
12/// better error messages.
13#[doc(hidden)]
14#[proc_macro]
15pub fn __ext(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
16    facet_macros_impl::ext_attr(input.into()).into()
17}
18
19// ============================================================================
20// ATTRIBUTE GRAMMAR PROC-MACROS
21// ============================================================================
22
23/// Internal proc macro for compiling attribute grammars.
24///
25/// This is called by `define_attr_grammar!` to generate type definitions
26/// and dispatcher macros from a grammar DSL.
27#[doc(hidden)]
28#[proc_macro]
29pub fn __make_parse_attr(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
30    facet_macros_impl::attr_grammar::make_parse_attr(input.into()).into()
31}
32
33/// Internal proc macro for unified attribute dispatch.
34///
35/// Routes parsed attribute names to the appropriate variant handlers
36/// (unit, newtype, or struct).
37#[doc(hidden)]
38#[proc_macro]
39pub fn __dispatch_attr(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
40    facet_macros_impl::attr_grammar::dispatch_attr(input.into()).into()
41}
42
43/// Internal proc macro for building struct field values.
44///
45/// Parses struct field assignments with type validation and helpful
46/// error messages.
47#[doc(hidden)]
48#[proc_macro]
49pub fn __build_struct_fields(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
50    facet_macros_impl::attr_grammar::build_struct_fields(input.into()).into()
51}
52
53/// Internal proc macro for attribute error messages.
54///
55/// Generates compile_error! with typo suggestions for unknown attributes.
56#[doc(hidden)]
57#[proc_macro]
58pub fn __attr_error(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
59    facet_macros_impl::attr_grammar::attr_error(input.into()).into()
60}
61
62/// Internal proc macro for field error messages.
63///
64/// Generates compile_error! with typo suggestions for unknown fields.
65#[doc(hidden)]
66#[proc_macro]
67pub fn __field_error(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
68    facet_macros_impl::attr_grammar::field_error(input.into()).into()
69}
70
71/// Internal proc macro for spanned error messages.
72///
73/// A generic helper for emitting errors with precise spans.
74#[doc(hidden)]
75#[proc_macro]
76pub fn __spanned_error(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
77    facet_macros_impl::attr_grammar::spanned_error(input.into()).into()
78}
79
80#[cfg(feature = "function")]
81#[proc_macro_attribute]
82pub fn facet_fn(
83    attr: proc_macro::TokenStream,
84    item: proc_macro::TokenStream,
85) -> proc_macro::TokenStream {
86    facet_macros_impl::function::facet_fn(attr.into(), item.into()).into()
87}
88
89#[cfg(feature = "function")]
90#[proc_macro]
91pub fn fn_shape(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
92    facet_macros_impl::function::fn_shape(input.into()).into()
93}
94
95/// Internal proc macro for unknown extension attribute errors.
96///
97/// This generates a compile_error! with the span pointing to the unknown identifier.
98#[doc(hidden)]
99#[proc_macro]
100pub fn __unknown_attr(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
101    facet_macros_impl::unknown_attr(input.into()).into()
102}
103
104// ============================================================================
105// PLUGIN SYSTEM PROC-MACROS
106// ============================================================================
107
108/// Internal proc macro for plugin chain finalization.
109///
110/// This is called at the end of a plugin chain to:
111/// 1. Parse the type definition ONCE
112/// 2. Generate the base Facet impl
113/// 3. Call each registered plugin's code generator
114///
115/// Input format:
116/// ```ignore
117/// __facet_finalize! {
118///     @tokens { struct Foo { ... } }
119///     @plugins { "Error", }
120///     @facet_crate { ::facet }
121/// }
122/// ```
123#[doc(hidden)]
124#[proc_macro]
125pub fn __facet_finalize(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
126    facet_macros_impl::facet_finalize(input.into()).into()
127}
128
129/// Internal proc macro for "does not accept arguments" errors.
130///
131/// Input: `"ns::attr", token`
132/// Generates compile_error! with span on the token.
133#[doc(hidden)]
134#[proc_macro]
135pub fn __no_args(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
136    facet_macros_impl::no_args(input.into()).into()
137}
138
139/// Attribute macro that runs cleanup code when a method returns an error.
140///
141/// # Usage
142///
143/// ```ignore
144/// #[on_error(self.cleanup())]
145/// pub fn do_something(&mut self) -> Result<(), Error> {
146///     self.inner_work()?;
147///     Ok(())
148/// }
149/// ```
150///
151/// For methods returning `Result<&mut Self, E>`, the macro properly handles
152/// the borrow by discarding the returned reference and returning a fresh `Ok(self)`:
153///
154/// ```ignore
155/// #[on_error(self.poison_and_cleanup())]
156/// pub fn begin_some(&mut self) -> Result<&mut Self, ReflectError> {
157///     self.require_active()?;
158///     // ... implementation
159///     Ok(self)
160/// }
161/// ```
162#[proc_macro_attribute]
163pub fn on_error(
164    attr: proc_macro::TokenStream,
165    item: proc_macro::TokenStream,
166) -> proc_macro::TokenStream {
167    facet_macros_impl::on_error(attr.into(), item.into()).into()
168}