Skip to main content

wolfram_serialize_macros/
lib.rs

1//! Procedural macros for `wolfram-serializer`.
2//!
3//! Provides `#[derive(ToWXF)]` and `#[derive(FromWXF)]` for structs (named,
4//! tuple, unit) and enums. Field-level type pattern matching emits the correct
5//! WXF representation for `Vec<u8>` (ByteArray), `Vec<numeric>` and rectangular
6//! nested tuples / fixed-size arrays of numerics (NumericArray), while
7//! everything else delegates through the `ToWXF` / `FromWXF` traits.
8//!
9//! See the `wolfram-serializer` crate docs for usage and the wire-format
10//! conventions emitted here.
11
12#![allow(clippy::needless_doctest_main)]
13
14use proc_macro::TokenStream;
15use syn::{parse_macro_input, DeriveInput};
16
17mod deserialize;
18mod failure_derive;
19mod serialize;
20mod shared;
21mod ty_classify;
22
23/// Derive `ToWXF` for a struct or enum.
24#[proc_macro_derive(ToWXF, attributes(wolfram))]
25pub fn derive_to_wxf(input: TokenStream) -> TokenStream {
26    let input = parse_macro_input!(input as DeriveInput);
27    serialize::expand(&input)
28        .unwrap_or_else(|err| err.to_compile_error())
29        .into()
30}
31
32/// Derive `FromWXF` for a struct or enum.
33#[proc_macro_derive(FromWXF, attributes(wolfram))]
34pub fn derive_from_wxf(input: TokenStream) -> TokenStream {
35    let input = parse_macro_input!(input as DeriveInput);
36    deserialize::expand(&input)
37        .unwrap_or_else(|err| err.to_compile_error())
38        .into()
39}
40
41/// Derive `From<Enum> for Expr` for an error enum: each variant becomes its
42/// `Failure["VariantName", <|fields|>]` expression (the `expr!` boilerplate one
43/// would otherwise write by hand, inferred from the enum).
44#[proc_macro_derive(Failure, attributes(wolfram))]
45pub fn derive_failure(input: TokenStream) -> TokenStream {
46    let input = parse_macro_input!(input as DeriveInput);
47    failure_derive::expand(&input)
48        .unwrap_or_else(|err| err.to_compile_error())
49        .into()
50}