Skip to main content

wire_repr_macros/
lib.rs

1//! Derive macro implementation for `wire-repr`.
2
3#![deny(missing_docs)]
4
5use proc_macro::TokenStream;
6
7mod derive;
8mod validator;
9
10/// Marks a semantic validator so schema derives can infer its error type.
11#[proc_macro_attribute]
12pub fn validator(_attribute: TokenStream, input: TokenStream) -> TokenStream {
13    match syn::parse(input).and_then(validator::expand) {
14        Ok(tokens) => tokens.into(),
15        Err(error) => error.into_compile_error().into(),
16    }
17}
18
19/// Marks a fallible computed callback so derives can retain its concrete error type.
20#[proc_macro_attribute]
21pub fn computed(_attribute: TokenStream, input: TokenStream) -> TokenStream {
22    match syn::parse(input).and_then(validator::expand_computed) {
23        Ok(tokens) => tokens.into(),
24        Err(error) => error.into_compile_error().into(),
25    }
26}
27
28/// Derives the schema-only exact-source read surface.
29#[proc_macro_derive(WireView, attributes(wire))]
30pub fn derive_wire_view(input: TokenStream) -> TokenStream {
31    match syn::parse(input).and_then(derive::expand_view) {
32        Ok(tokens) => tokens.into(),
33        Err(error) => error.into_compile_error().into(),
34    }
35}
36
37/// Derives the schema-only typestate builder surface.
38#[proc_macro_derive(WireBuilder, attributes(wire))]
39pub fn derive_wire_builder(input: TokenStream) -> TokenStream {
40    match syn::parse(input).and_then(derive::expand_builder) {
41        Ok(tokens) => tokens.into(),
42        Err(error) => error.into_compile_error().into(),
43    }
44}