jtd_derive/lib.rs
1//! This crate provides a framework for generating [_JSON Typedef_](https://jsontypedef.com)
2//! schemas based on Rust types and how they'd be serialized by
3//! [`serde_json`](https://docs.rs/serde_json).
4//!
5//! In order to be able to generate a schema for a type, it must implement the
6//! [`JsonTypedef`] trait. Many types from the Rust standard library already do.
7//! To implement [`JsonTypedef`] for your own types, you'll probably
8//! want to derive it.
9//!
10//! Generating a schema is done by creating a [`Generator`](gen::Generator),
11//! calling [`Generator::into_root_schema`](gen::Generator::into_root_schema),
12//! and finally serializing the resulting [`RootSchema`](schema::RootSchema) object.
13//!
14//! # Example
15//!
16//! ```
17//! use jtd_derive::{JsonTypedef, Generator};
18//!
19//! #[derive(JsonTypedef)]
20//! struct Foo {
21//! x: u32,
22//! }
23//!
24//! let root_schema = Generator::default().into_root_schema::<Foo>().unwrap();
25//! let json_schema = serde_json::to_value(&root_schema).unwrap();
26//!
27//! assert_eq!(json_schema, serde_json::json!{ {
28//! "properties": {
29//! "x": { "type": "uint32" }
30//! },
31//! "additionalProperties": true,
32//! } });
33//! ```
34
35mod gen;
36mod names;
37pub mod schema;
38mod r#trait;
39mod type_id;
40
41pub use gen::{GenError, Generator};
42pub use names::Names;
43pub use r#trait::JsonTypedef;