Skip to main content

ic_wire_derive/
lib.rs

1//! Host metadata for Serde contracts; no serializer generation.
2use proc_macro::TokenStream;
3use quote::format_ident;
4use syn::{DeriveInput, parse_macro_input};
5
6#[cfg(feature = "endpoint")]
7mod endpoint;
8mod native_schema;
9
10/// Connect a typed Wire query to Platform's maintained entrypoint generator.
11#[cfg(feature = "endpoint")]
12#[proc_macro_attribute]
13pub fn query(args: TokenStream, item: TokenStream) -> TokenStream {
14    endpoint::expand(true, args.into(), item.into())
15        .unwrap_or_else(syn::Error::into_compile_error)
16        .into()
17}
18
19/// Connect a typed Wire update to Platform's maintained entrypoint generator.
20#[cfg(feature = "endpoint")]
21#[proc_macro_attribute]
22pub fn update(args: TokenStream, item: TokenStream) -> TokenStream {
23    endpoint::expand(false, args.into(), item.into())
24        .unwrap_or_else(syn::Error::into_compile_error)
25        .into()
26}
27
28/// Describe ordinary Serde layouts for host-side TypeScript generation, without IDs.
29/// Emits metadata implementations on native targets only; Serde owns runtime codecs.
30#[proc_macro_derive(Wire, attributes(serde, wire))]
31pub fn derive_wire(input: TokenStream) -> TokenStream {
32    native_schema::expand(parse_macro_input!(input as DeriveInput))
33        .unwrap_or_else(syn::Error::into_compile_error)
34        .into()
35}
36
37// Generated local types and generics must not shadow names in the contract.
38fn fresh_ident(source: &str, base: &str) -> syn::Ident {
39    let mut name = base.to_owned();
40    while source.contains(&name) {
41        name.push('_');
42    }
43    format_ident!("{name}")
44}