oam_schema/lib.rs
1extern crate proc_macro;
2
3use proc_macro::TokenStream;
4use quote::quote;
5use syn::{DeriveInput, parse_macro_input};
6
7/// Derive macro to generate LLM-ready JSON schema from any struct
8/// Works with serde::Serialize and schemars::JsonSchema
9///
10/// # Example
11/// ```ignore
12/// use schemars::JsonSchema;
13/// use roam_schema::LlmSchema;
14/// use serde::{Deserialize, Serialize};
15///
16/// #[derive(Serialize, Deserialize, JsonSchema, LlmSchema)]
17/// pub struct Organization {
18/// pub id: String,
19/// pub name: String,
20/// }
21///
22/// let schema = Organization::llm_schema();
23/// ```
24#[proc_macro_derive(LlmSchema)]
25pub fn derive_llm_schema(input: TokenStream) -> TokenStream {
26 let input = parse_macro_input!(input as DeriveInput);
27 let name = &input.ident;
28
29 let expanded = quote! {
30 impl #name {
31 /// Generate LLM-ready JSON schema for tool calling (OpenAI, Anthropic, etc.)
32 ///
33 /// Returns a schemars::schema::RootSchema that represents this struct
34 /// as a JSON Schema, suitable for use as a function parameter schema
35 /// in LLM function calling interfaces.
36 pub fn llm_schema() -> schemars::schema::RootSchema {
37 schemars::schema_for!(#name)
38 }
39 }
40 };
41
42 TokenStream::from(expanded)
43}