llmkit_macros/lib.rs
1//! Procedural macros for `llmkit-rs`.
2//!
3//! Provides `#[derive(ToolSchema)]`, which implements `llmkit_core::ToolSchema`
4//! for a tool input struct, emitting a JSON Schema via `schemars`.
5//!
6//! ```ignore
7//! use llmkit_macros::ToolSchema;
8//! use serde::{Serialize, Deserialize};
9//!
10//! #[derive(Serialize, Deserialize, schemars::JsonSchema, ToolSchema)]
11//! #[tool(name = "get_weather", description = "Get current weather for a city")]
12//! struct GetWeatherInput {
13//! /// City name to look up.
14//! city: String,
15//! #[serde(default)]
16//! units: Option<String>,
17//! }
18//! ```
19
20#![forbid(unsafe_code)]
21
22use proc_macro::TokenStream;
23use syn::{parse_macro_input, DeriveInput};
24
25mod tool_schema;
26
27/// Derive `llmkit_core::ToolSchema` for a struct.
28///
29/// Reads `#[tool(name = "...", description = "...")]`; both are optional (`name`
30/// defaults to the lowercased type name). The type must also derive
31/// `schemars::JsonSchema`.
32#[proc_macro_derive(ToolSchema, attributes(tool))]
33pub fn derive_tool_schema(input: TokenStream) -> TokenStream {
34 let input = parse_macro_input!(input as DeriveInput);
35 tool_schema::expand(input)
36 .unwrap_or_else(syn::Error::into_compile_error)
37 .into()
38}