Skip to main content

miden_note_codec_macros/
lib.rs

1//! Procedural macros for author-side note codec components.
2
3#![deny(missing_docs)]
4
5extern crate proc_macro;
6
7mod expand;
8mod registry;
9
10use proc_macro::TokenStream;
11use syn::{ItemImpl, LitStr, parse_macro_input};
12
13/// Generates host-profile note types from the package built by a Miden project.
14///
15/// The project path is relative to `CARGO_MANIFEST_DIR`. Build the note project with
16/// `cargo miden build` before compiling the codec crate.
17#[proc_macro]
18pub fn from_project(input: TokenStream) -> TokenStream {
19    let input = parse_macro_input!(input as LitStr);
20    expand::from_project(&input)
21        .unwrap_or_else(syn::Error::into_compile_error)
22        .into()
23}
24
25/// Generates host-profile note types from one exact Miden package path.
26///
27/// A relative path is resolved against `CARGO_MANIFEST_DIR`.
28#[proc_macro]
29pub fn from_package(input: TokenStream) -> TokenStream {
30    let input = parse_macro_input!(input as LitStr);
31    expand::from_package(&input)
32        .unwrap_or_else(syn::Error::into_compile_error)
33        .into()
34}
35
36/// Generates host-profile note types from WIT text for internal tests.
37#[doc(hidden)]
38#[proc_macro]
39pub fn from_wit_text(input: TokenStream) -> TokenStream {
40    let input = parse_macro_input!(input as LitStr);
41    expand::from_wit_text(&input)
42        .unwrap_or_else(syn::Error::into_compile_error)
43        .into()
44}
45
46/// Marks an [`miden_note_codec::AuthorTypeCodec`] implementation for component export.
47#[proc_macro_attribute]
48pub fn note_codec(args: TokenStream, input: TokenStream) -> TokenStream {
49    let item = parse_macro_input!(input as ItemImpl);
50    expand::note_codec(args.into(), item)
51        .unwrap_or_else(syn::Error::into_compile_error)
52        .into()
53}
54
55/// Exports all codecs marked earlier in the crate through the `miden:note-codec` component world.
56///
57/// Place this macro after the generated schema types and every `#[note_codec]` implementation.
58/// Procedural macros register codecs as they expand; supported types are exported in canonical FQN
59/// order.
60#[proc_macro]
61pub fn export_codecs(input: TokenStream) -> TokenStream {
62    expand::export_codecs(input.into())
63        .unwrap_or_else(syn::Error::into_compile_error)
64        .into()
65}
66
67#[cfg(test)]
68mod tests;