Start adding doc comments

This commit is contained in:
Graham Esau 2019-10-20 15:46:09 +01:00
parent b40c160ec2
commit 9a85e0b2a1
4 changed files with 49 additions and 2 deletions

View file

@ -1,5 +1,3 @@
#![allow(clippy::large_enum_variant)]
pub type Map<K, V> = std::collections::BTreeMap<K, V>; pub type Map<K, V> = std::collections::BTreeMap<K, V>;
pub type Set<T> = std::collections::BTreeSet<T>; pub type Set<T> = std::collections::BTreeSet<T>;
@ -8,22 +6,55 @@ mod json_schema_impls;
#[macro_use] #[macro_use]
mod macros; mod macros;
/// JSON Schema generator and settings.
pub mod gen; pub mod gen;
/// JSON Schema types.
pub mod schema; pub mod schema;
pub use schemars_derive::*; pub use schemars_derive::*;
use schema::Schema; use schema::Schema;
/// A type which can be described as a JSON Schema document.
///
/// This is implemented for many Rust primitive and standard library types.
///
/// This can also be automatically derived on most custom types with `#[derive(JsonSchema)]`.
///
/// ```
/// use schemars::{schema_for, JsonSchema};
///
/// #[derive(JsonSchema)]
/// struct MyStruct {
/// foo: i32,
/// }
///
/// let my_schema = schema_for!(MyStruct);
/// ```
pub trait JsonSchema { pub trait JsonSchema {
/// Whether JSON Schemas generated for this type should be re-used where possible using the `$ref` keyword.
///
/// For trivial types (such as primitives), this should return `false`. For more complex types, it should return `true`.
/// For recursive types, this *must* return `true` to prevent infinite cycles when generating schemas.
///
/// By default, this returns `true`.
fn is_referenceable() -> bool { fn is_referenceable() -> bool {
true true
} }
/// The name of the generated JSON Schema.
///
/// This is used as the title for root schemas, and the key within the `definitions` property for subschemas.
fn schema_name() -> String; fn schema_name() -> String;
/// Generates a JSON Schema for this type.
///
/// This should not return a `$ref` schema.
fn json_schema(gen: &mut gen::SchemaGenerator) -> Schema; fn json_schema(gen: &mut gen::SchemaGenerator) -> Schema;
/// Helper for generating schemas for flattened `Option` fields.
///
/// This should not need to be called or implemented by code outside of `schemars`.
#[doc(hidden)] #[doc(hidden)]
fn json_schema_optional(gen: &mut gen::SchemaGenerator) -> Schema { fn json_schema_optional(gen: &mut gen::SchemaGenerator) -> Schema {
Self::json_schema(gen) Self::json_schema(gen)

View file

@ -1,3 +1,17 @@
/// Generates a [`Schema`](schema::Schema) for the given type using default settings.
///
/// The type must implement [`JsonSchema`].
///
/// ```
/// use schemars::{schema_for, JsonSchema};
///
/// #[derive(JsonSchema)]
/// struct MyStruct {
/// foo: i32,
/// }
///
/// let my_schema = schema_for!(MyStruct);
/// ```
#[macro_export] #[macro_export]
macro_rules! schema_for { macro_rules! schema_for {
($type:path) => { ($type:path) => {

View file

@ -3,6 +3,7 @@ use crate::{JsonSchema, Map, Set};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use serde_json::Value; use serde_json::Value;
#[allow(clippy::large_enum_variant)]
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema)] #[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema)]
#[serde(untagged)] #[serde(untagged)]
pub enum Schema { pub enum Schema {

View file

@ -21,6 +21,7 @@ struct Deep1 {
v: Vec<i32>, v: Vec<i32>,
} }
#[allow(clippy::option_option)]
#[derive(Debug, JsonSchema)] #[derive(Debug, JsonSchema)]
struct Deep2 { struct Deep2 {
b: bool, b: bool,