pub struct Generator { /* private fields */ }
Expand description
A configurable schema generator. An instance is meant to produce one
RootSchema
and be consumed in the process.
If you want to just use the sane defaults, try Generator::default()
.
Otherwise, you can configure schema generation using the builder.
§Examples
Using the default settings:
use jtd_derive::{JsonTypedef, Generator};
#[derive(JsonTypedef)]
struct Foo {
x: u32,
}
let root_schema = Generator::default().into_root_schema::<Foo>().unwrap();
let json_schema = serde_json::to_value(&root_schema).unwrap();
assert_eq!(json_schema, serde_json::json!{ {
"properties": {
"x": { "type": "uint32" }
},
"additionalProperties": true,
} });
Using custom settings:
use jtd_derive::{JsonTypedef, Generator};
#[derive(JsonTypedef)]
struct Foo {
x: u32,
}
let root_schema = Generator::builder()
.top_level_ref()
.naming_short()
.build()
.into_root_schema::<Foo>()
.unwrap();
let json_schema = serde_json::to_value(&root_schema).unwrap();
assert_eq!(json_schema, serde_json::json!{ {
"definitions": {
"Foo": {
"properties": {
"x": { "type": "uint32" }
},
"additionalProperties": true,
}
},
"ref": "Foo",
} });
Implementations§
Source§impl Generator
impl Generator
Sourcepub fn builder() -> GeneratorBuilder
pub fn builder() -> GeneratorBuilder
Provide a Generator
builder, allowing for some customization.
Sourcepub fn into_root_schema<T: JsonTypedef>(self) -> Result<RootSchema, GenError>
pub fn into_root_schema<T: JsonTypedef>(self) -> Result<RootSchema, GenError>
Generate the root schema for the given type according to the settings. This consumes the generator.
This will return an error if a naming collision is detected, i.e. two distinct Rust types produce the same identifier.
Sourcepub fn sub_schema<T: JsonTypedef + ?Sized>(&mut self) -> Schema
pub fn sub_schema<T: JsonTypedef + ?Sized>(&mut self) -> Schema
Generate a Schema
for a given type, adding definitions to the
generator as appropriate.
This is meant to only be called when implementing JsonTypedef
for
new types. Most commonly you’ll derive that trait. It’s unlikely you’ll
need to call this method explicitly.