schemars/json_schema_impls/
core.rs1use crate::SchemaGenerator;
2use crate::_alloc_prelude::*;
3use crate::_private::allow_null;
4use crate::{json_schema, JsonSchema, Schema};
5use alloc::borrow::Cow;
6use core::ops::{Bound, Range, RangeInclusive};
7
8impl<T: JsonSchema> JsonSchema for Option<T> {
9 inline_schema!();
10
11 fn schema_name() -> Cow<'static, str> {
12 format!("Nullable_{}", T::schema_name()).into()
13 }
14
15 fn schema_id() -> Cow<'static, str> {
16 format!("Option<{}>", T::schema_id()).into()
17 }
18
19 fn json_schema(generator: &mut SchemaGenerator) -> Schema {
20 let mut schema = generator.subschema_for::<T>();
21
22 allow_null(generator, &mut schema);
23
24 schema
25 }
26
27 fn _schemars_private_non_optional_json_schema(generator: &mut SchemaGenerator) -> Schema {
28 T::_schemars_private_non_optional_json_schema(generator)
29 }
30
31 fn _schemars_private_is_option() -> bool {
32 true
33 }
34}
35
36impl<T: JsonSchema, E: JsonSchema> JsonSchema for Result<T, E> {
37 fn schema_name() -> Cow<'static, str> {
38 format!("Result_of_{}_or_{}", T::schema_name(), E::schema_name()).into()
39 }
40
41 fn schema_id() -> Cow<'static, str> {
42 format!("Result<{}, {}>", T::schema_id(), E::schema_id()).into()
43 }
44
45 fn json_schema(generator: &mut SchemaGenerator) -> Schema {
46 json_schema!({
47 "oneOf": [
48 {
49 "type": "object",
50 "properties": {
51 "Ok": generator.subschema_for::<T>()
52 },
53 "required": ["Ok"]
54 },
55 {
56 "type": "object",
57 "properties": {
58 "Err": generator.subschema_for::<E>()
59 },
60 "required": ["Err"]
61 }
62 ]
63 })
64 }
65}
66
67impl<T: JsonSchema> JsonSchema for Bound<T> {
68 fn schema_name() -> Cow<'static, str> {
69 format!("Bound_of_{}", T::schema_name()).into()
70 }
71
72 fn schema_id() -> Cow<'static, str> {
73 format!("Bound<{}>", T::schema_id()).into()
74 }
75
76 fn json_schema(generator: &mut SchemaGenerator) -> Schema {
77 json_schema!({
78 "oneOf": [
79 {
80 "type": "object",
81 "properties": {
82 "Included": generator.subschema_for::<T>()
83 },
84 "required": ["Included"]
85 },
86 {
87 "type": "object",
88 "properties": {
89 "Excluded": generator.subschema_for::<T>()
90 },
91 "required": ["Excluded"]
92 },
93 {
94 "type": "string",
95 "const": "Unbounded"
96 }
97 ]
98 })
99 }
100}
101
102impl<T: JsonSchema> JsonSchema for Range<T> {
103 fn schema_name() -> Cow<'static, str> {
104 format!("Range_of_{}", T::schema_name()).into()
105 }
106
107 fn schema_id() -> Cow<'static, str> {
108 format!("Range<{}>", T::schema_id()).into()
109 }
110
111 fn json_schema(generator: &mut SchemaGenerator) -> Schema {
112 let subschema = generator.subschema_for::<T>();
113 json_schema!({
114 "type": "object",
115 "properties": {
116 "start": subschema,
117 "end": subschema
118 },
119 "required": ["start", "end"]
120 })
121 }
122}
123
124forward_impl!((<T: JsonSchema> JsonSchema for RangeInclusive<T>) => Range<T>);
125
126forward_impl!((<T: ?Sized> JsonSchema for core::marker::PhantomData<T>) => ());
127
128forward_impl!((<'a> JsonSchema for core::fmt::Arguments<'a>) => String);