substrate_data_derive/lib.rs
1//! `#[derive(Schema)]` — see `substrate_data::Schema`.
2//!
3//! - A struct with named fields is an object. Field attributes:
4//! `#[data(rename = "name")]`, `#[data(default)]` /
5//! `#[data(default = "path::to_fn")]`, `#[data(skip)]` (never read or
6//! written; `Default`). `Option` fields may be left out.
7//! - A struct with one unnamed field is that field's format.
8//! - An enum of unit variants is a string: the variant's name in
9//! `snake_case`, or `#[data(rename = "...")]`.
10//! - `#[data(tag = "type")]` on an enum: an object whose `type` names the
11//! variant; a variant is a unit, has named fields, or wraps one value —
12//! a struct's fields sit next to the tag, anything else under `value`
13//! (`substrate_data::schema::WRAPPED`).
14//!
15//! - `#[data(view)]` on a struct: `NameView<'a>` too, the struct read out
16//! of a frame without copying (`substrate_data::frame::View`).
17//!
18//! Every type also packs itself into frames (`pack` / `unpack`: fields by
19//! position, unit enums by index) and writes the binary form straight
20//! (`encode` / `decode`).
21//!
22//! Doc comments become the shape's docs (an editor's hover text and the
23//! docs' field tables).
24
25mod attr_name;
26mod attrs;
27mod case;
28mod codegen;
29mod docs;
30mod enums;
31mod expand;
32mod fields;
33mod marker;
34mod struct_schema;
35#[cfg(test)]
36mod tests;
37mod view;
38
39use proc_macro::TokenStream;
40use syn::{DeriveInput, parse_macro_input};
41
42/// `#[derive(Schema)]`: see the crate's docs.
43#[proc_macro_derive(Schema, attributes(data))]
44pub fn derive_schema(input: TokenStream) -> TokenStream {
45 let input = parse_macro_input!(input as DeriveInput);
46 expand::derive(&input)
47 .unwrap_or_else(syn::Error::into_compile_error)
48 .into()
49}
50
51#[cfg(doctest)]
52#[doc = include_str!("../README.md")]
53struct CrateReadmeDoctests;