Struct Generator

Source
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

Source

pub fn builder() -> GeneratorBuilder

Provide a Generator builder, allowing for some customization.

Source

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.

Source

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.

Trait Implementations§

Source§

impl Debug for Generator

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Generator

Source§

fn default() -> Generator

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.