Struct Schema

Source
pub struct Schema {
    pub schema: Option<Draft>,
    pub id: Option<String>,
    pub title: String,
    pub description: Option<String>,
    /* private fields */
}
Expand description

This is a structure representing the JSON schema itself.

§Create Schema

See the documentation top for usage.

§To JSON schema string

#[derive(Debug, Schematic)]
struct Example {
    #[rschema(field(
        title = "Dummy",
        description = "Dummy field",
    ))]
    dummy: String,
}
 
fn main() -> rschema::Result<()> {
    let schema_str = Schema::new::<Example>("Example")
        .to_string()?;
 
    assert_eq!(
        schema_str,
        r#"{"title":"Example","type":"object","properties":{"dummy":{"title":"Dummy","description":"Dummy field","type":"string"}},"additionalProperties":false}"#
    );
 
    Ok(())
}

Use to_string_pretty to generate as a pretty-prited string.

Fields§

§schema: Option<Draft>§id: Option<String>§title: String§description: Option<String>

Implementations§

Source§

impl Schema

Source

pub fn new<T: Schematic>(title: &str) -> Self

Create a schema object from the given type T.

Source

pub fn description(&mut self, description: impl Into<String>) -> &mut Self

Add a description about this schema.

Source

pub fn schema(&mut self, schema: Draft) -> &mut Self

Specify $schema.

Source

pub fn id(&mut self, id: impl Into<String>) -> &mut Self

Specify $id.

Source

pub fn to_string(&self) -> Result<String>

Generate a JSON schema string.

§Errors

Internally calls serde_json::to_string, so this can fail if it fails. Read more

Source

pub fn to_string_pretty(&self) -> Result<String>

Generate a pretty-printed JSON schema string.

§Errors

Internally calls serde_json::to_string_pretty, so this can fail if it fails. Read more

Source

pub fn write(&self, path: impl AsRef<Path>) -> Result<()>

Write a JSON schema string to a file.

§Errors

This call can fail if its own to_string call or writing to a file fails.

Source

pub fn write_pretty(&self, path: impl AsRef<Path>) -> Result<()>

Write a pretty-printed JSON schema string to a file.

§Errors

This call can fail if its own to_string_pretty call or writing to a file fails.

Trait Implementations§

Source§

impl Debug for Schema

Source§

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

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

impl Serialize for Schema

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

§

impl Freeze for Schema

§

impl RefUnwindSafe for Schema

§

impl Send for Schema

§

impl Sync for Schema

§

impl Unpin for Schema

§

impl UnwindSafe for Schema

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.