Skip to content

Commit

Permalink
Wrap comment lines at 100 chars
Browse files Browse the repository at this point in the history
  • Loading branch information
GREsau committed Aug 30, 2024
1 parent 0672c86 commit 5d58a4d
Show file tree
Hide file tree
Showing 9 changed files with 147 additions and 92 deletions.
81 changes: 49 additions & 32 deletions schemars/src/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ type CowStr = alloc::borrow::Cow<'static, str>;
/// Settings to customize how Schemas are generated.
///
/// The default settings currently conform to [JSON Schema 2020-12](https://json-schema.org/specification-links#2020-12), but this is liable to change in a future version of Schemars if support for other JSON Schema versions is added.
/// If you rely on generated schemas conforming to draft 2020-12, consider using the [`SchemaSettings::draft2020_12()`] method.
/// If you rely on generated schemas conforming to draft 2020-12, consider using the
/// [`SchemaSettings::draft2020_12()`] method.
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct SchemaSettings {
Expand All @@ -35,7 +36,8 @@ pub struct SchemaSettings {
///
/// Defaults to `true`.
pub option_add_null_type: bool,
/// A JSON pointer to the expected location of referenceable subschemas within the resulting root schema.
/// A JSON pointer to the expected location of referenceable subschemas within the resulting
/// root schema.
///
/// A single leading `#` and/or single trailing `/` are ignored.
///
Expand All @@ -57,7 +59,8 @@ pub struct SchemaSettings {

impl Default for SchemaSettings {
/// The default settings currently conform to [JSON Schema 2020-12](https://json-schema.org/specification-links#2020-12), but this is liable to change in a future version of Schemars if support for other JSON Schema versions is added.
/// If you rely on generated schemas conforming to draft 2020-12, consider using the [`SchemaSettings::draft2020_12()`] method.
/// If you rely on generated schemas conforming to draft 2020-12, consider using the
/// [`SchemaSettings::draft2020_12()`] method.
fn default() -> SchemaSettings {
SchemaSettings::draft2020_12()
}
Expand Down Expand Up @@ -145,7 +148,8 @@ impl SchemaSettings {
self
}

/// Appends the given transform to the list of [transforms](SchemaSettings::transforms) for these `SchemaSettings`.
/// Appends the given transform to the list of [transforms](SchemaSettings::transforms) for
/// these `SchemaSettings`.
pub fn with_transform(mut self, transform: impl Transform + Clone + 'static + Send) -> Self {
self.transforms.push(Box::new(transform));
self
Expand Down Expand Up @@ -222,13 +226,15 @@ impl SchemaGenerator {
&self.settings
}

/// Generates a JSON Schema for the type `T`, and returns either the schema itself or a `$ref` schema referencing `T`'s schema.
/// Generates a JSON Schema for the type `T`, and returns either the schema itself or a `$ref`
/// schema referencing `T`'s schema.
///
/// If `T` is not [inlined](JsonSchema::always_inline_schema), this will add `T`'s schema to this generator's definitions, and
/// return a `$ref` schema referencing that schema. Otherwise, this method behaves identically to [`JsonSchema::json_schema`].
/// If `T` is not [inlined](JsonSchema::always_inline_schema), this will add `T`'s schema to
/// this generator's definitions, and return a `$ref` schema referencing that schema.
/// Otherwise, this method behaves identically to [`JsonSchema::json_schema`].
///
/// If `T`'s schema depends on any [non-inlined](JsonSchema::always_inline_schema) schemas, then this method will
/// add them to the `SchemaGenerator`'s schema definitions.
/// If `T`'s schema depends on any [non-inlined](JsonSchema::always_inline_schema) schemas, then
/// this method will add them to the `SchemaGenerator`'s schema definitions.
pub fn subschema_for<T: ?Sized + JsonSchema>(&mut self) -> Schema {
let id = T::schema_id();
let return_ref = !T::always_inline_schema()
Expand Down Expand Up @@ -278,40 +284,44 @@ impl SchemaGenerator {
self.definitions.insert(name.into(), schema.to_value());
}

/// Borrows the collection of all [non-inlined](JsonSchema::always_inline_schema) schemas that have been generated.
/// Borrows the collection of all [non-inlined](JsonSchema::always_inline_schema) schemas that
/// have been generated.
///
/// The keys of the returned `Map` are the [schema names](JsonSchema::schema_name), and the values are the schemas
/// themselves.
/// The keys of the returned `Map` are the [schema names](JsonSchema::schema_name), and the
/// values are the schemas themselves.
pub fn definitions(&self) -> &JsonMap<String, Value> {
&self.definitions
}

/// Mutably borrows the collection of all [non-inlined](JsonSchema::always_inline_schema) schemas that have been generated.
/// Mutably borrows the collection of all [non-inlined](JsonSchema::always_inline_schema)
/// schemas that have been generated.
///
/// The keys of the returned `Map` are the [schema names](JsonSchema::schema_name), and the values are the schemas
/// themselves.
/// The keys of the returned `Map` are the [schema names](JsonSchema::schema_name), and the
/// values are the schemas themselves.
pub fn definitions_mut(&mut self) -> &mut JsonMap<String, Value> {
&mut self.definitions
}

/// Returns the collection of all [non-inlined](JsonSchema::always_inline_schema) schemas that have been generated,
/// leaving an empty `Map` in its place.
/// Returns the collection of all [non-inlined](JsonSchema::always_inline_schema) schemas that
/// have been generated, leaving an empty `Map` in its place.
///
/// The keys of the returned `Map` are the [schema names](JsonSchema::schema_name), and the values are the schemas
/// themselves.
/// The keys of the returned `Map` are the [schema names](JsonSchema::schema_name), and the
/// values are the schemas themselves.
pub fn take_definitions(&mut self) -> JsonMap<String, Value> {
core::mem::take(&mut self.definitions)
}

/// Returns an iterator over the [transforms](SchemaSettings::transforms) being used by this `SchemaGenerator`.
/// Returns an iterator over the [transforms](SchemaSettings::transforms) being used by this
/// `SchemaGenerator`.
pub fn transforms_mut(&mut self) -> impl Iterator<Item = &mut dyn GenTransform> {
self.settings.transforms.iter_mut().map(Box::as_mut)
}

/// Generates a JSON Schema for the type `T`.
///
/// If `T`'s schema depends on any [non-inlined](JsonSchema::always_inline_schema) schemas, then this method will
/// include them in the returned `Schema` at the [definitions path](SchemaSettings::definitions_path) (by default `"$defs"`).
/// If `T`'s schema depends on any [non-inlined](JsonSchema::always_inline_schema) schemas, then
/// this method will include them in the returned `Schema` at the [definitions
/// path](SchemaSettings::definitions_path) (by default `"$defs"`).
pub fn root_schema_for<T: ?Sized + JsonSchema>(&mut self) -> Schema {
let mut schema = self.json_schema_internal::<T>(T::schema_id());

Expand All @@ -333,8 +343,9 @@ impl SchemaGenerator {

/// Consumes `self` and generates a JSON Schema for the type `T`.
///
/// If `T`'s schema depends on any [non-inlined](JsonSchema::always_inline_schema) schemas, then this method will
/// include them in the returned `Schema` at the [definitions path](SchemaSettings::definitions_path) (by default `"$defs"`).
/// If `T`'s schema depends on any [non-inlined](JsonSchema::always_inline_schema) schemas, then
/// this method will include them in the returned `Schema` at the [definitions
/// path](SchemaSettings::definitions_path) (by default `"$defs"`).
pub fn into_root_schema_for<T: ?Sized + JsonSchema>(mut self) -> Schema {
let mut schema = self.json_schema_internal::<T>(T::schema_id());

Expand All @@ -357,8 +368,9 @@ impl SchemaGenerator {

/// Generates a JSON Schema for the given example value.
///
/// If the value implements [`JsonSchema`], then prefer using the [`root_schema_for()`](Self::root_schema_for())
/// function which will generally produce a more precise schema, particularly when the value contains any enums.
/// If the value implements [`JsonSchema`], then prefer using the
/// [`root_schema_for()`](Self::root_schema_for()) function which will generally produce a
/// more precise schema, particularly when the value contains any enums.
///
/// If the `Serialize` implementation of the value decides to fail, this will return an [`Err`].
pub fn root_schema_for_value<T: ?Sized + Serialize>(
Expand Down Expand Up @@ -388,8 +400,9 @@ impl SchemaGenerator {

/// Consumes `self` and generates a JSON Schema for the given example value.
///
/// If the value implements [`JsonSchema`], then prefer using the [`into_root_schema_for()!`](Self::into_root_schema_for())
/// function which will generally produce a more precise schema, particularly when the value contains any enums.
/// If the value implements [`JsonSchema`], then prefer using the
/// [`into_root_schema_for()!`](Self::into_root_schema_for()) function which will generally
/// produce a more precise schema, particularly when the value contains any enums.
///
/// If the `Serialize` implementation of the value decides to fail, this will return an [`Err`].
pub fn into_root_schema_for_value<T: ?Sized + Serialize>(
Expand Down Expand Up @@ -511,9 +524,11 @@ fn json_pointer_mut<'a>(
Some(object)
}

/// A [`Transform`] which implements additional traits required to be included in a [`SchemaSettings`].
/// A [`Transform`] which implements additional traits required to be included in a
/// [`SchemaSettings`].
///
/// You will rarely need to use this trait directly as it is automatically implemented for any type which implements all of:
/// You will rarely need to use this trait directly as it is automatically implemented for any type
/// which implements all of:
/// - [`Transform`]
/// - [`std::any::Any`] (implemented for all `'static` types)
/// - [`std::clone::Clone`]
Expand All @@ -537,7 +552,8 @@ fn json_pointer_mut<'a>(
/// assert!(v.as_any().is::<MyTransform>());
/// ```
pub trait GenTransform: Transform + DynClone + Any + Send {
/// Upcasts this transform into an [`Any`], which can be used to inspect and manipulate it as its concrete type.
/// Upcasts this transform into an [`Any`], which can be used to inspect and manipulate it as
/// its concrete type.
///
/// # Example
/// To remove a specific transform from an instance of `SchemaSettings`:
Expand All @@ -556,7 +572,8 @@ pub trait GenTransform: Transform + DynClone + Any + Send {
/// ```
fn as_any(&self) -> &dyn Any;

/// Mutably upcasts this transform into an [`Any`], which can be used to inspect and manipulate it as its concrete type.
/// Mutably upcasts this transform into an [`Any`], which can be used to inspect and manipulate
/// it as its concrete type.
///
/// # Example
/// To modify a specific transform in an instance of `SchemaSettings`:
Expand Down
30 changes: 17 additions & 13 deletions schemars/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@ pub mod r#gen {
/// assert_eq!(NonGenericType::schema_id(), <&mut NonGenericType>::schema_id());
/// ```
///
/// But generic type parameters which may affect the generated schema should typically be included in the name/ID:
/// But generic type parameters which may affect the generated schema should typically be included
/// in the name/ID:
/// ```
/// use schemars::{SchemaGenerator, Schema, JsonSchema, json_schema};
/// use std::{borrow::Cow, marker::PhantomData};
Expand Down Expand Up @@ -138,14 +139,14 @@ pub mod r#gen {
///
/// assert_eq!(<GenericType<i32>>::schema_id(), <&mut GenericType<&i32>>::schema_id());
/// ```
///

pub trait JsonSchema {
/// Whether JSON Schemas generated for this type should be included directly in parent schemas, rather than being
/// re-used where possible using the `$ref` keyword.
/// Whether JSON Schemas generated for this type should be included directly in parent schemas,
/// rather than being re-used where possible using the `$ref` keyword.
///
/// For trivial types (such as primitives), this should return `true`. For more complex types, it should return `false`.
/// For recursive types, this **must** return `false` to prevent infinite cycles when generating schemas.
/// For trivial types (such as primitives), this should return `true`. For more complex types,
/// it should return `false`. For recursive types, this **must** return `false` to prevent
/// infinite cycles when generating schemas.
///
/// By default, this returns `false`.
fn always_inline_schema() -> bool {
Expand All @@ -154,24 +155,27 @@ pub trait JsonSchema {

/// The name of the generated JSON Schema.
///
/// This is used as the title for root schemas, and the key within the root's `definitions` property for subschemas.
/// This is used as the title for root schemas, and the key within the root's `definitions`
/// property for subschemas.
fn schema_name() -> Cow<'static, str>;

/// Returns a string that uniquely identifies the schema produced by this type.
///
/// This does not have to be a human-readable string, and the value will not itself be included in generated schemas.
/// If two types produce different schemas, then they **must** have different `schema_id()`s,
/// but two types that produce identical schemas should *ideally* have the same `schema_id()`.
/// This does not have to be a human-readable string, and the value will not itself be included
/// in generated schemas. If two types produce different schemas, then they **must** have
/// different `schema_id()`s, but two types that produce identical schemas should *ideally*
/// have the same `schema_id()`.
///
/// The default implementation returns the same value as [`schema_name()`](JsonSchema::schema_name).
/// The default implementation returns the same value as
/// [`schema_name()`](JsonSchema::schema_name).
fn schema_id() -> Cow<'static, str> {
Self::schema_name()
}

/// Generates a JSON Schema for this type.
///
/// If the returned schema depends on any [non-inlined](JsonSchema::always_inline_schema) schemas, then this method will
/// add them to the [`SchemaGenerator`]'s schema definitions.
/// If the returned schema depends on any [non-inlined](JsonSchema::always_inline_schema)
/// schemas, then this method will add them to the [`SchemaGenerator`]'s schema definitions.
///
/// This should not return a `$ref` schema.
fn json_schema(generator: &mut SchemaGenerator) -> Schema;
Expand Down
13 changes: 8 additions & 5 deletions schemars/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,10 @@ macro_rules! schema_for {
/// Generates a [`Schema`](crate::Schema) for the given example value using default settings.
/// The default settings currently conform to [JSON Schema 2020-12](https://json-schema.org/specification-links#2020-12), but this is liable to change in a future version of Schemars if support for other JSON Schema versions is added.
///
/// The value must implement [`Serialize`](serde::Serialize). If the value also implements [`JsonSchema`](crate::JsonSchema),
/// then prefer using the [`schema_for!(Type)`](schema_for) macro which will generally produce a more precise schema,
/// particularly when the value contains any enums.
/// The value must implement [`Serialize`](serde::Serialize). If the value also implements
/// [`JsonSchema`](crate::JsonSchema), then prefer using the [`schema_for!(Type)`](schema_for) macro
/// which will generally produce a more precise schema, particularly when the value contains any
/// enums.
///
/// If the `Serialize` implementation of the value decides to fail, this macro will panic.
/// For a non-panicking alternative, create a [`SchemaGenerator`](crate::SchemaGenerator) and use
Expand All @@ -80,9 +81,11 @@ macro_rules! schema_for_value {
};
}

/// Construct a [`Schema`](crate::Schema) from a JSON literal. This can either be a JSON object, or a boolean (`true` or `false`).
/// Construct a [`Schema`](crate::Schema) from a JSON literal. This can either be a JSON object, or
/// a boolean (`true` or `false`).
///
/// You can interpolate variables or expressions into a JSON object using the same rules as the [`serde_json::json`] macro.
/// You can interpolate variables or expressions into a JSON object using the same rules as the
/// [`serde_json::json`] macro.
///
/// # Example
/// ```
Expand Down
Loading

0 comments on commit 5d58a4d

Please sign in to comment.