ai-json-template-derive
A proc-macro crate that allows you to derive a trait, AiJsonTemplate
, on any plain-old Rust struct to produce a JSON “schema” or “template” describing that struct’s fields, doc comments, and nested structures. This crate enforces certain field types—such as String
, Option<String>
, Vec<String>
, or a recursively nested type also deriving AiJsonTemplate
—making it useful for guiding AI models (like GPT) to output data matching a specific format.
Features
-
AiJsonTemplate
Trait- Automatically implemented via
#[derive(AiJsonTemplate)]
. - Provides a single method,
to_template()
, returning aserde_json::Value
describing each field’s type and any doc comments.
- Automatically implemented via
-
Doc Comments to Instructions
- Rust doc comments (
///
) on structs and fields become embedded in the JSON output, helping you generate AI instructions or clarifications.
- Rust doc comments (
-
Nested Struct Support
- If a field’s type also derives
AiJsonTemplate
, the macro includes a"nested_template"
object in the JSON.
- If a field’s type also derives
-
Controlled Field Types
- By default, only
String
,Option<String>
,Vec<String>
, or nestedAiJsonTemplate
types are allowed. The macro fails if it encounters other field types, ensuring consistent data structures for AI outputs.
- By default, only
-
Seamless with Serde
- You can also derive
Serialize
andDeserialize
on the same struct. The macro doesn’t interfere with normal Rust <-> JSON round-trip usage.
- You can also derive
Example
use AiJsonTemplate;
use ;
/// My top-level config struct
/// A nested struct, also with doc comments
Output might look like:
Installation
In your Cargo.toml
, add:
[]
= "0.1"
= "1.0"
= "1.0"
Note: Because it’s a proc-macro crate, ensure you have Rust 2021 edition or later. You’ll also need
syn
,quote
, andproc-macro2
available internally.
Usage
- Import the macro:
use AiJsonTemplate;
- Annotate structs with
#[derive(AiJsonTemplate, Serialize, Deserialize)]
. - Generate JSON: call
YourStruct::to_template()
to retrieve a structured schema of doc comments and types. - Feed that JSON schema to an AI model to guide output format, or use it as you see fit.
Testing
We provide a robust integration test suite in the tests/
folder, covering:
- Simple usage with required and optional fields.
- Nested structs (multi-level).
- Doc comments verification.
- Round-trip checks ensuring normal Serde usage is unaffected.
You can also add [trybuild] tests to confirm that the macro fails gracefully when encountering unsupported field types or missing Serialize
/Deserialize
derivations.