Struct Hints

Source
pub struct Hints<'a> { /* private fields */ }
Expand description

Hints for Inferrer.

By default, Inferrer will never produce enum, values, or discriminator forms. Hints tell Inferrer to use these forms. See HintSet for details on how you can specify the “paths” to the pieces of the input that should use these forms.

default_num_type tells Inferrer what numeric type to attempt to use by default when it encounters a JSON number. This default will be ignored if it doesn’t contain the example data. When the default is ignored, the inferrer will infer the narrowest numerical type possible for input data, preferring unsigned integers over signed integers.

To adapt the example used at the crate-level docs, here’s how you could change Inferrer behavior using hints:

use serde_json::json;
use jtd_infer::{Inferrer, Hints, HintSet, NumType};

let enum_path = vec!["bar".to_string()];
let mut inferrer = Inferrer::new(Hints::new(
    NumType::Float32,
    HintSet::new(vec![&enum_path]),
    HintSet::new(vec![]),
    HintSet::new(vec![]),
));

inferrer = inferrer.infer(json!({ "foo": true, "bar": "xxx" }));
inferrer = inferrer.infer(json!({ "foo": false, "bar": null, "baz": 5 }));

let inference = inferrer.into_schema();

assert_eq!(
    json!({
        "properties": {
            "foo": { "type": "boolean" },
            "bar": { "enum": ["xxx"], "nullable": true }, // now an enum
        },
        "optionalProperties": {
            "baz": { "type": "float32" }, // instead of uint8
        },
    }),
    serde_json::to_value(inference.into_serde_schema()).unwrap(),
)

Implementations§

Source§

impl<'a> Hints<'a>

Source

pub fn new( default_num_type: NumType, enums: HintSet<'a>, values: HintSet<'a>, discriminator: HintSet<'a>, ) -> Self

Constructs a new set of Hints.

Auto Trait Implementations§

§

impl<'a> Freeze for Hints<'a>

§

impl<'a> RefUnwindSafe for Hints<'a>

§

impl<'a> Send for Hints<'a>

§

impl<'a> Sync for Hints<'a>

§

impl<'a> Unpin for Hints<'a>

§

impl<'a> UnwindSafe for Hints<'a>

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.