Debian Codegraph @

src/schema.rs

raw symbols SCIP
1 /*!
2 JSON Schema types.
3 */
4
5 use craterust-analyzer cargo schemars 1.2.2 crate/::_alloc_preluderust-analyzer cargo schemars 1.2.2 _alloc_prelude/::*;
6 use ref_castrust-analyzer cargo ref-cast 1.0.26 crate/::{ref_cast_customrust-analyzer cargo ref-cast-impl 1.0.26 ref_cast_custom:, RefCastCustomrust-analyzer cargo ref-cast-impl 1.0.26 RefCastCustom:};
7 use serderust-analyzer cargo serde 1.0.229 crate/::{Deserializerust-analyzer cargo serde_core 1.0.229 de/Deserialize#, Serializerust-analyzer cargo serde_core 1.0.229 ser/Serialize#};
8 use serde_jsonrust-analyzer cargo serde_json 1.0.151 crate/::{Maprust-analyzer cargo serde_json 1.0.151 map/Map#, Valuerust-analyzer cargo serde_json 1.0.151 value/Value#};
9
10 /// A JSON Schema.
11 ///
12 /// This wraps a JSON [`Value`] that must be either an [object](Value::Object) or a
13 /// [bool](Value::Bool).
14 ///
15 /// A custom JSON schema can be created using the [`json_schema!`](crate::json_schema) macro:
16 /// ```
17 /// use schemars::{Schema, json_schema};
18 ///
19 /// let my_schema: Schema = json_schema!({
20 /// "type": ["object", "null"]
21 /// });
22 /// ```
23 ///
24 /// Because a `Schema` is a thin wrapper around a `Value`, you can also use
25 /// [`TryFrom::try_from`]/[`TryInto::try_into`] to create a `Schema` from an existing `Value`.
26 /// This operation is fallible, because only [objects](Value::Object) and [bools](Value::Bool) can
27 /// be converted in this way.
28 ///
29 /// ```
30 /// use schemars::{Schema, json_schema};
31 /// use serde_json::json;
32 ///
33 /// let json_object = json!({"type": ["object", "null"]});
34 /// let object_schema: Schema = json_object.try_into().unwrap();
35 ///
36 /// let json_bool = json!(true);
37 /// let bool_schema: Schema = json_bool.try_into().unwrap();
38 ///
39 /// let json_string = json!("This is neither an object nor a bool!");
40 /// assert!(Schema::try_from(json_string).is_err());
41 ///
42 /// // You can also convert a `&Value`/`&mut Value` to a `&Schema`/`&mut Schema` the same way:
43 ///
44 /// let json_object = json!({"type": ["object", "null"]});
45 /// let object_schema_ref: &Schema = (&json_object).try_into().unwrap();
46 ///
47 /// let mut json_object = json!({"type": ["object", "null"]});
48 /// let object_schema_mut: &mut Schema = (&mut json_object).try_into().unwrap();
49 /// ```
50 ///
51 /// Similarly, you can use [`From`]/[`Into`] to (infallibly) create a `Schema` from an existing
52 /// [`Map<String, Value>`] or [`bool`].
53 ///
54 /// ```
55 /// use schemars::{Schema, json_schema};
56 /// use serde_json::{Map, json};
57 ///
58 /// let mut map = Map::new();
59 /// map.insert("type".to_owned(), json!(["object", "null"]));
60 /// let object_schema: Schema = map.into();
61 ///
62 /// let bool_schema: Schema = true.into();
63 /// ```
64 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:, RefCastCustomrust-analyzer cargo ref-cast-impl 1.0.26 RefCastCustom:)]
65 #[repr(transparent)]
66 pub struct Schemarust-analyzer cargo schemars 1.2.2 schema/Schema#(Valuerust-analyzer cargo serde_json 1.0.151 value/Value#);
67
68 impl<'de> Deserializerust-analyzer cargo serde_core 1.0.229 de/Deserialize#<'de> for Schemarust-analyzer cargo schemars 1.2.2 schema/Schema# {
69 fn deserializerust-analyzer cargo schemars 1.2.2 schema/impl#[Schema][`Deserialize<'de>`]deserialize().<D>(deserializer: D) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Selfrust-analyzer cargo schemars 1.2.2 schema/impl#[Schema][`Deserialize<'de>`], D::Errorrust-analyzer cargo serde_core 1.0.229 de/Deserializer#Error#>
70 where
71 D: serderust-analyzer cargo serde 1.0.229 crate/::Deserializerrust-analyzer cargo serde_core 1.0.229 de/Deserializer#<'de>,
72 {
73 let value = Valuerust-analyzer cargo serde_json 1.0.151 value/Value#::deserializerust-analyzer cargo serde_json 1.0.151 value/de/impl#[Value][`Deserialize<'de>`]deserialize().(deserializer)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
74 Schemarust-analyzer cargo schemars 1.2.2 schema/Schema#::validaterust-analyzer cargo schemars 1.2.2 schema/impl#[Schema]validate().(&value)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/try_trait/Try#branch().;
75 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Schemarust-analyzer cargo schemars 1.2.2 schema/Schema#(value))
76 }
77 }
78
79 impl Serializerust-analyzer cargo serde_core 1.0.229 ser/Serialize# for Schemarust-analyzer cargo schemars 1.2.2 schema/Schema# {
80 fn serializerust-analyzer cargo schemars 1.2.2 schema/impl#[Schema][Serialize]serialize().<S>(&self, serializer: S) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<S::Okrust-analyzer cargo serde_core 1.0.229 ser/Serializer#Ok#, S::Errorrust-analyzer cargo serde_core 1.0.229 ser/Serializer#Error#>
81 where
82 S: serderust-analyzer cargo serde 1.0.229 crate/::Serializerrust-analyzer cargo serde_core 1.0.229 ser/Serializer#,
83 {
84 serrust-analyzer cargo schemars 1.2.2 schema/ser/::OrderedKeywordWrapperrust-analyzer cargo schemars 1.2.2 schema/ser/OrderedKeywordWrapper#::fromrust-analyzer cargo schemars 1.2.2 schema/ser/impl#[`OrderedKeywordWrapper<'a>`][`From<&'a Value>`]from().(&self.0rust-analyzer cargo schemars 1.2.2 schema/Schema#0.).serializerust-analyzer cargo schemars 1.2.2 schema/ser/impl#[`OrderedKeywordWrapper<'_>`][Serialize]serialize().(serializer)
85 }
86 }
87
88 impl PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#<bool> for Schemarust-analyzer cargo schemars 1.2.2 schema/Schema# {
89 fn eqrust-analyzer cargo schemars 1.2.2 schema/impl#[Schema][`PartialEq<bool>`]eq().(&self, other: &bool) -> bool {
90 self.as_boolrust-analyzer cargo schemars 1.2.2 schema/impl#[Schema]as_bool().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][`PartialEq<Self>`]eq().Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().other)
91 }
92 }
93
94 impl PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#<Maprust-analyzer cargo serde_json 1.0.151 map/Map#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#, Valuerust-analyzer cargo serde_json 1.0.151 value/Value#>> for Schemarust-analyzer cargo schemars 1.2.2 schema/Schema# {
95 fn eqrust-analyzer cargo schemars 1.2.2 schema/impl#[Schema][`PartialEq<Map<String, Value>>`]eq().(&self, other: &Maprust-analyzer cargo serde_json 1.0.151 map/Map#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#, Valuerust-analyzer cargo serde_json 1.0.151 value/Value#>) -> bool {
96 self.as_objectrust-analyzer cargo schemars 1.2.2 schema/impl#[Schema]as_object().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][`PartialEq<Self>`]eq().Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(other)
97 }
98 }
99
100 impl PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#<Valuerust-analyzer cargo serde_json 1.0.151 value/Value#> for Schemarust-analyzer cargo schemars 1.2.2 schema/Schema# {
101 fn eqrust-analyzer cargo schemars 1.2.2 schema/impl#[Schema][`PartialEq<Value>`]eq().(&self, other: &Valuerust-analyzer cargo serde_json 1.0.151 value/Value#) -> bool {
102 self.as_valuerust-analyzer cargo schemars 1.2.2 schema/impl#[Schema]as_value().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialEq<&B>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialEq<&B>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialEq<&B>`]eq().other
103 }
104 }
105
106 impl PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#<Schemarust-analyzer cargo schemars 1.2.2 schema/Schema#> for bool {
107 fn eqrust-analyzer cargo schemars 1.2.2 schema/impl#[bool][`PartialEq<Schema>`]eq().(&self, other: &Schemarust-analyzer cargo schemars 1.2.2 schema/Schema#) -> bool {
108 other rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialEq<&B>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialEq<&B>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialEq<&B>`]eq().self
109 }
110 }
111
112 impl PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#<Schemarust-analyzer cargo schemars 1.2.2 schema/Schema#> for Maprust-analyzer cargo serde_json 1.0.151 map/Map#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#, Valuerust-analyzer cargo serde_json 1.0.151 value/Value#> {
113 fn eqrust-analyzer cargo schemars 1.2.2 schema/impl#[`Map<String, Value>`][`PartialEq<Schema>`]eq().(&self, other: &Schemarust-analyzer cargo schemars 1.2.2 schema/Schema#) -> bool {
114 other rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialEq<&B>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialEq<&B>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialEq<&B>`]eq().self
115 }
116 }
117
118 impl PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#<Schemarust-analyzer cargo schemars 1.2.2 schema/Schema#> for Valuerust-analyzer cargo serde_json 1.0.151 value/Value# {
119 fn eqrust-analyzer cargo schemars 1.2.2 schema/impl#[Value][`PartialEq<Schema>`]eq().(&self, other: &Schemarust-analyzer cargo schemars 1.2.2 schema/Schema#) -> bool {
120 other rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialEq<&B>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialEq<&B>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialEq<&B>`]eq().self
121 }
122 }
123
124 impl Schemarust-analyzer cargo schemars 1.2.2 schema/Schema# {
125 /// Creates a new schema object with a single string property `"$ref"`.
126 ///
127 /// The given reference string should be a URI reference. This will usually be a JSON Pointer
128 /// in [URI Fragment representation](https://tools.ietf.org/html/rfc6901#section-6).
129 #[must_use]
130 pub fn new_refrust-analyzer cargo schemars 1.2.2 schema/impl#[Schema]new_ref().(reference: Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#) -> Selfrust-analyzer cargo schemars 1.2.2 schema/impl#[Schema] {
131 let mut map = Maprust-analyzer cargo serde_json 1.0.151 map/Map#::newrust-analyzer cargo serde_json 1.0.151 map/impl#[`Map<String, Value>`]new().();
132 map.insertrust-analyzer cargo serde_json 1.0.151 map/impl#[`Map<String, Value>`]insert().("$ref".to_ownedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str][ToOwned]to_owned().(), Valuerust-analyzer cargo serde_json 1.0.151 value/Value#::Stringrust-analyzer cargo serde_json 1.0.151 value/Value#String#(reference));
133 Selfrust-analyzer cargo schemars 1.2.2 schema/impl#[Schema](Valuerust-analyzer cargo serde_json 1.0.151 value/Value#::Objectrust-analyzer cargo serde_json 1.0.151 value/Value#Object#(map))
134 }
135
136 /// Borrows the `Schema`'s underlying JSON value.
137 #[must_use]
138 pub fn as_valuerust-analyzer cargo schemars 1.2.2 schema/impl#[Schema]as_value().(&self) -> &Valuerust-analyzer cargo serde_json 1.0.151 value/Value# {
139 &self.0rust-analyzer cargo schemars 1.2.2 schema/Schema#0.
140 }
141
142 /// If the `Schema`'s underlying JSON value is a bool, returns the bool value.
143 #[must_use]
144 pub fn as_boolrust-analyzer cargo schemars 1.2.2 schema/impl#[Schema]as_bool().(&self) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<bool> {
145 self.0rust-analyzer cargo schemars 1.2.2 schema/Schema#0..as_boolrust-analyzer cargo serde_json 1.0.151 value/impl#[Value]as_bool().()
146 }
147
148 /// If the `Schema`'s underlying JSON value is an object, borrows the object as a `Map` of
149 /// properties.
150 #[must_use]
151 pub fn as_objectrust-analyzer cargo schemars 1.2.2 schema/impl#[Schema]as_object().(&self) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<&Maprust-analyzer cargo serde_json 1.0.151 map/Map#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#, Valuerust-analyzer cargo serde_json 1.0.151 value/Value#>> {
152 self.0rust-analyzer cargo schemars 1.2.2 schema/Schema#0..as_objectrust-analyzer cargo serde_json 1.0.151 value/impl#[Value]as_object().()
153 }
154
155 /// If the `Schema`'s underlying JSON value is an object, mutably borrows the object as a `Map`
156 /// of properties.
157 #[must_use]
158 pub fn as_object_mutrust-analyzer cargo schemars 1.2.2 schema/impl#[Schema]as_object_mut().(&mut self) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<&mut Maprust-analyzer cargo serde_json 1.0.151 map/Map#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#, Valuerust-analyzer cargo serde_json 1.0.151 value/Value#>> {
159 self.0rust-analyzer cargo schemars 1.2.2 schema/Schema#0..as_object_mutrust-analyzer cargo serde_json 1.0.151 value/impl#[Value]as_object_mut().()
160 }
161
162 pub(craterust-analyzer cargo schemars 1.2.2 crate/) fn try_to_objectrust-analyzer cargo schemars 1.2.2 schema/impl#[Schema]try_to_object().(self) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Maprust-analyzer cargo serde_json 1.0.151 map/Map#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#, Valuerust-analyzer cargo serde_json 1.0.151 value/Value#>, bool> {
163 match self.0rust-analyzer cargo schemars 1.2.2 schema/Schema#0. {
164 Valuerust-analyzer cargo serde_json 1.0.151 value/Value#::Objectrust-analyzer cargo serde_json 1.0.151 value/Value#Object#(m) => Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(m),
165 Valuerust-analyzer cargo serde_json 1.0.151 value/Value#::Boolrust-analyzer cargo serde_json 1.0.151 value/Value#Bool#(b) => Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(b),
166 _ => unreachablerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/unreachable!!(),
167 }
168 }
169
170 pub(craterust-analyzer cargo schemars 1.2.2 crate/) fn try_as_object_mutrust-analyzer cargo schemars 1.2.2 schema/impl#[Schema]try_as_object_mut().(&mut self) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<&mut Maprust-analyzer cargo serde_json 1.0.151 map/Map#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#, Valuerust-analyzer cargo serde_json 1.0.151 value/Value#>, bool> {
171 match &mut self.0rust-analyzer cargo schemars 1.2.2 schema/Schema#0. {
172 Valuerust-analyzer cargo serde_json 1.0.151 value/Value#::Objectrust-analyzer cargo serde_json 1.0.151 value/Value#Object#(m) => Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(m),
173 Valuerust-analyzer cargo serde_json 1.0.151 value/Value#::Boolrust-analyzer cargo serde_json 1.0.151 value/Value#Bool#(b) => Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&mut T`][Deref]deref().b),
174 _ => unreachablerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/unreachable!!(),
175 }
176 }
177
178 /// Returns the `Schema`'s underlying JSON value.
179 #[must_use]
180 pub fn to_valuerust-analyzer cargo schemars 1.2.2 schema/impl#[Schema]to_value().(self) -> Valuerust-analyzer cargo serde_json 1.0.151 value/Value# {
181 self.0rust-analyzer cargo schemars 1.2.2 schema/Schema#0.
182 }
183
184 /// Converts the `Schema` (if it wraps a bool value) into an equivalent object schema. Then
185 /// mutably borrows the object as a `Map` of properties.
186 ///
187 /// `true` is transformed into an empty schema `{}`, which successfully validates against all
188 /// possible values. `false` is transformed into the schema `{"not": {}}`, which does not
189 /// successfully validate against any value.
190 #[allow(clippy::missing_panics_doc)]
191 pub fn ensure_objectrust-analyzer cargo schemars 1.2.2 schema/impl#[Schema]ensure_object().(&mut self) -> &mut Maprust-analyzer cargo serde_json 1.0.151 map/Map#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#, Valuerust-analyzer cargo serde_json 1.0.151 value/Value#> {
192 if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(b) = self.as_boolrust-analyzer cargo schemars 1.2.2 schema/impl#[Schema]as_bool().() {
193 let mut map = Maprust-analyzer cargo serde_json 1.0.151 map/Map#::newrust-analyzer cargo serde_json 1.0.151 map/impl#[`Map<String, Value>`]new().();
194 if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().b {
195 map.insertrust-analyzer cargo serde_json 1.0.151 map/impl#[`Map<String, Value>`]insert().("not".intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(), Valuerust-analyzer cargo serde_json 1.0.151 value/Value#::Objectrust-analyzer cargo serde_json 1.0.151 value/Value#Object#(Maprust-analyzer cargo serde_json 1.0.151 map/Map#::newrust-analyzer cargo serde_json 1.0.151 map/impl#[`Map<String, Value>`]new().()));
196 }
197 self.0rust-analyzer cargo schemars 1.2.2 schema/Schema#0. = Valuerust-analyzer cargo serde_json 1.0.151 value/Value#::Objectrust-analyzer cargo serde_json 1.0.151 value/Value#Object#(map);
198 }
199
200 self.0rust-analyzer cargo schemars 1.2.2 schema/Schema#0.
201 .as_object_mutrust-analyzer cargo serde_json 1.0.151 value/impl#[Value]as_object_mut().()
202 .expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]expect().("Schema value should be of type Object.")
203 }
204
205 /// Inserts a property into the schema, replacing any previous value.
206 ///
207 /// If the schema wraps a bool value, it will first be converted into an equivalent object
208 /// schema.
209 ///
210 /// If the schema did not have this key present, `None` is returned.
211 ///
212 /// If the schema did have this key present, the value is updated, and the old value is
213 /// returned.
214 ///
215 /// # Example
216 /// ```
217 /// use schemars::json_schema;
218 /// use serde_json::json;
219 ///
220 /// let mut schema = json_schema!(true);
221 /// assert_eq!(schema.insert("type".to_owned(), "array".into()), None);
222 /// assert_eq!(schema.insert("type".to_owned(), "object".into()), Some(json!("array")));
223 ///
224 /// assert_eq!(schema, json_schema!({"type": "object"}));
225 /// ```
226 pub fn insertrust-analyzer cargo schemars 1.2.2 schema/impl#[Schema]insert().(&mut self, k: Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#, v: Valuerust-analyzer cargo serde_json 1.0.151 value/Value#) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Valuerust-analyzer cargo serde_json 1.0.151 value/Value#> {
227 self.ensure_objectrust-analyzer cargo schemars 1.2.2 schema/impl#[Schema]ensure_object().().insertrust-analyzer cargo serde_json 1.0.151 map/impl#[`Map<String, Value>`]insert().(k, v)
228 }
229
230 /// If the `Schema`'s underlying JSON value is an object, gets a reference to that object's
231 /// value for the given key if it exists.
232 ///
233 /// This always returns `None` for bool schemas.
234 ///
235 /// # Example
236 /// ```
237 /// use schemars::json_schema;
238 /// use serde_json::json;
239 ///
240 /// let obj_schema = json_schema!({"type": "array"});
241 /// assert_eq!(obj_schema.get("type"), Some(&json!("array")));
242 /// assert_eq!(obj_schema.get("format"), None);
243 ///
244 /// let bool_schema = json_schema!(true);
245 /// assert_eq!(bool_schema.get("type"), None);
246 /// ```
247 #[must_use]
248 pub fn getrust-analyzer cargo schemars 1.2.2 schema/impl#[Schema]get().<Q>(&self, key: &Q) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<&Valuerust-analyzer cargo serde_json 1.0.151 value/Value#>
249 where
250 Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#: corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::borrowrust-analyzer cargo core https://github.com/rust-lang/rust/library/core borrow/::Borrowrust-analyzer cargo core https://github.com/rust-lang/rust/library/core borrow/Borrow#<Q>,
251 Q: ?Sizedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Sized# + Ordrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ord# + Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq# + corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::hashrust-analyzer cargo core https://github.com/rust-lang/rust/library/core hash/::Hashrust-analyzer cargo core https://github.com/rust-lang/rust/library/core hash/Hash#,
252 {
253 self.0rust-analyzer cargo schemars 1.2.2 schema/Schema#0..as_objectrust-analyzer cargo serde_json 1.0.151 value/impl#[Value]as_object().().and_thenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]and_then().(|o| o.getrust-analyzer cargo serde_json 1.0.151 map/impl#[`Map<String, Value>`]get().(key))
254 }
255
256 /// If the `Schema`'s underlying JSON value is an object, gets a mutable reference to that
257 /// object's value for the given key if it exists.
258 ///
259 /// This always returns `None` for bool schemas.
260 ///
261 /// # Example
262 /// ```
263 /// use schemars::json_schema;
264 /// use serde_json::{json, Value};
265 ///
266 /// let mut obj_schema = json_schema!({ "properties": {} });
267 /// if let Some(Value::Object(properties)) = obj_schema.get_mut("properties") {
268 /// properties.insert("anything".to_owned(), true.into());
269 /// }
270 /// assert_eq!(obj_schema, json_schema!({ "properties": { "anything": true } }));
271 /// ```
272 #[must_use]
273 pub fn get_mutrust-analyzer cargo schemars 1.2.2 schema/impl#[Schema]get_mut().<Q>(&mut self, key: &Q) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<&mut Valuerust-analyzer cargo serde_json 1.0.151 value/Value#>
274 where
275 Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#: corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::borrowrust-analyzer cargo core https://github.com/rust-lang/rust/library/core borrow/::Borrowrust-analyzer cargo core https://github.com/rust-lang/rust/library/core borrow/Borrow#<Q>,
276 Q: ?Sizedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Sized# + Ordrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ord# + Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq# + corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::hashrust-analyzer cargo core https://github.com/rust-lang/rust/library/core hash/::Hashrust-analyzer cargo core https://github.com/rust-lang/rust/library/core hash/Hash#,
277 {
278 self.0rust-analyzer cargo schemars 1.2.2 schema/Schema#0..as_object_mutrust-analyzer cargo serde_json 1.0.151 value/impl#[Value]as_object_mut().().and_thenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]and_then().(|o| o.get_mutrust-analyzer cargo serde_json 1.0.151 map/impl#[`Map<String, Value>`]get_mut().(key))
279 }
280
281 /// If the `Schema`'s underlying JSON value is an object, looks up a value within the schema
282 /// by a JSON Pointer.
283 ///
284 /// If the given pointer begins with a `#`, then the rest of the value is assumed to be in
285 /// "URI Fragment Identifier Representation", and will be percent-decoded accordingly.
286 ///
287 /// For more information on JSON Pointer, read [RFC6901](https://tools.ietf.org/html/rfc6901).
288 ///
289 /// This always returns `None` for bool schemas.
290 ///
291 /// # Example
292 /// ```
293 /// use schemars::json_schema;
294 /// use serde_json::json;
295 ///
296 /// let schema = json_schema!({
297 /// "properties": {
298 /// "anything": true
299 /// },
300 /// "$defs": {
301 /// "🚀": true
302 /// }
303 /// });
304 ///
305 /// assert_eq!(schema.pointer("/properties/anything").unwrap(), &json!(true));
306 /// assert_eq!(schema.pointer("#/$defs/%F0%9F%9A%80").unwrap(), &json!(true));
307 /// assert_eq!(schema.pointer("/does/not/exist"), None);
308 /// ```
309 #[must_use]
310 pub fn pointerrust-analyzer cargo schemars 1.2.2 schema/impl#[Schema]pointer().(&self, pointer: &str) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<&Valuerust-analyzer cargo serde_json 1.0.151 value/Value#> {
311 if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(percent_encoded) = pointer.strip_prefixrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]strip_prefix().('#') {
312 let decoded = craterust-analyzer cargo schemars 1.2.2 crate/::encodingrust-analyzer cargo schemars 1.2.2 encoding/::percent_decoderust-analyzer cargo schemars 1.2.2 encoding/percent_decode().(percent_encoded)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][Try]branch().;
313 self.0rust-analyzer cargo schemars 1.2.2 schema/Schema#0..pointerrust-analyzer cargo serde_json 1.0.151 value/impl#[Value]pointer().(&decoded)
314 } else {
315 self.0rust-analyzer cargo schemars 1.2.2 schema/Schema#0..pointerrust-analyzer cargo serde_json 1.0.151 value/impl#[Value]pointer().(pointer)
316 }
317 }
318
319 /// If the `Schema`'s underlying JSON value is an object, looks up a value by a JSON Pointer
320 /// and returns a mutable reference to that value.
321 ///
322 /// If the given pointer begins with a `#`, then the rest of the value is assumed to be in
323 /// "URI Fragment Identifier Representation", and will be percent-decoded accordingly.
324 ///
325 /// For more information on JSON Pointer, read [RFC6901](https://tools.ietf.org/html/rfc6901).
326 ///
327 /// This always returns `None` for bool schemas.
328 ///
329 /// # Example
330 /// ```
331 /// use schemars::{json_schema, Schema};
332 /// use serde_json::json;
333 ///
334 /// let mut schema = json_schema!({
335 /// "properties": {
336 /// "anything": true
337 /// },
338 /// "$defs": {
339 /// "🚀": true
340 /// }
341 /// });
342 ///
343 /// assert_eq!(schema.pointer_mut("/properties/anything").unwrap(), &json!(true));
344 /// assert_eq!(schema.pointer_mut("#/$defs/%F0%9F%9A%80").unwrap(), &json!(true));
345 /// assert_eq!(schema.pointer_mut("/does/not/exist"), None);
346 /// ```
347 #[must_use]
348 pub fn pointer_mutrust-analyzer cargo schemars 1.2.2 schema/impl#[Schema]pointer_mut().(&mut self, pointer: &str) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<&mut Valuerust-analyzer cargo serde_json 1.0.151 value/Value#> {
349 if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(percent_encoded) = pointer.strip_prefixrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]strip_prefix().('#') {
350 let decoded = craterust-analyzer cargo schemars 1.2.2 crate/::encodingrust-analyzer cargo schemars 1.2.2 encoding/::percent_decoderust-analyzer cargo schemars 1.2.2 encoding/percent_decode().(percent_encoded)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][Try]branch().;
351 self.0rust-analyzer cargo schemars 1.2.2 schema/Schema#0..pointer_mutrust-analyzer cargo serde_json 1.0.151 value/impl#[Value]pointer_mut().(&decoded)
352 } else {
353 self.0rust-analyzer cargo schemars 1.2.2 schema/Schema#0..pointer_mutrust-analyzer cargo serde_json 1.0.151 value/impl#[Value]pointer_mut().(pointer)
354 }
355 }
356
357 /// If the `Schema`'s underlying JSON value is an object, removes and returns its value for the
358 /// given key.
359 ///
360 /// This always returns `None` for bool schemas, without modifying them.
361 ///
362 /// # Example
363 /// ```
364 /// use schemars::json_schema;
365 /// use serde_json::json;
366 ///
367 /// let mut schema = json_schema!({"type": "array"});
368 /// assert_eq!(schema.remove("type"), Some(json!("array")));
369 /// assert_eq!(schema, json_schema!({}));
370 /// ```
371 pub fn removerust-analyzer cargo schemars 1.2.2 schema/impl#[Schema]remove().<Q>(&mut self, key: &Q) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Valuerust-analyzer cargo serde_json 1.0.151 value/Value#>
372 where
373 Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#: corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::borrowrust-analyzer cargo core https://github.com/rust-lang/rust/library/core borrow/::Borrowrust-analyzer cargo core https://github.com/rust-lang/rust/library/core borrow/Borrow#<Q>,
374 Q: ?Sizedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Sized# + Ordrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ord# + Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq# + corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::hashrust-analyzer cargo core https://github.com/rust-lang/rust/library/core hash/::Hashrust-analyzer cargo core https://github.com/rust-lang/rust/library/core hash/Hash#,
375 {
376 self.0rust-analyzer cargo schemars 1.2.2 schema/Schema#0..as_object_mutrust-analyzer cargo serde_json 1.0.151 value/impl#[Value]as_object_mut().().and_thenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]and_then().(|o| o.removerust-analyzer cargo serde_json 1.0.151 map/impl#[`Map<String, Value>`]remove().(key))
377 }
378
379 pub(craterust-analyzer cargo schemars 1.2.2 crate/) fn has_typerust-analyzer cargo schemars 1.2.2 schema/impl#[Schema]has_type().(&self, ty: &str) -> bool {
380 match self.0rust-analyzer cargo schemars 1.2.2 schema/Schema#0..getrust-analyzer cargo serde_json 1.0.151 value/impl#[Value]get().("type") {
381 Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(Valuerust-analyzer cargo serde_json 1.0.151 value/Value#::Arrayrust-analyzer cargo serde_json 1.0.151 value/Value#Array#(values)) => values.iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().anyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/iter/impl#[`Iter<'a, T>`][Iterator]any().(|v| v.as_strrust-analyzer cargo serde_json 1.0.151 value/impl#[Value]as_str().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][`PartialEq<Self>`]eq().Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(ty)),
382 Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(Valuerust-analyzer cargo serde_json 1.0.151 value/Value#::Stringrust-analyzer cargo serde_json 1.0.151 value/Value#String#(s)) => s rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialEq<&B>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialEq<&B>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialEq<&B>`]eq().ty,
383 _ => false,
384 }
385 }
386
387 fn validaterust-analyzer cargo schemars 1.2.2 schema/impl#[Schema]validate().<E: serderust-analyzer cargo serde 1.0.229 crate/::derust-analyzer cargo serde_core 1.0.229 de/::Errorrust-analyzer cargo serde_core 1.0.229 de/Error#>(value: &Valuerust-analyzer cargo serde_json 1.0.151 value/Value#) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), E> {
388 use serderust-analyzer cargo serde 1.0.229 crate/::derust-analyzer cargo serde_core 1.0.229 de/::Unexpectedrust-analyzer cargo serde_core 1.0.229 de/Unexpected#;
389 let unexpected = match value {
390 Valuerust-analyzer cargo serde_json 1.0.151 value/Value#::Boolrust-analyzer cargo serde_json 1.0.151 value/Value#Bool#(_) | Valuerust-analyzer cargo serde_json 1.0.151 value/Value#::Objectrust-analyzer cargo serde_json 1.0.151 value/Value#Object#(_) => return Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()),
391 Valuerust-analyzer cargo serde_json 1.0.151 value/Value#::Nullrust-analyzer cargo serde_json 1.0.151 value/Value#Null# => Unexpectedrust-analyzer cargo serde_core 1.0.229 de/Unexpected#::Unitrust-analyzer cargo serde_core 1.0.229 de/Unexpected#Unit#,
392 Valuerust-analyzer cargo serde_json 1.0.151 value/Value#::Numberrust-analyzer cargo serde_json 1.0.151 value/Value#Number#(n) => {
393 if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(u) = n.as_u64rust-analyzer cargo serde_json 1.0.151 number/impl#[Number]as_u64().() {
394 Unexpectedrust-analyzer cargo serde_core 1.0.229 de/Unexpected#::Unsignedrust-analyzer cargo serde_core 1.0.229 de/Unexpected#Unsigned#(u)
395 } else if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(i) = n.as_i64rust-analyzer cargo serde_json 1.0.151 number/impl#[Number]as_i64().() {
396 Unexpectedrust-analyzer cargo serde_core 1.0.229 de/Unexpected#::Signedrust-analyzer cargo serde_core 1.0.229 de/Unexpected#Signed#(i)
397 } else if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(f) = n.as_f64rust-analyzer cargo serde_json 1.0.151 number/impl#[Number]as_f64().() {
398 Unexpectedrust-analyzer cargo serde_core 1.0.229 de/Unexpected#::Floatrust-analyzer cargo serde_core 1.0.229 de/Unexpected#Float#(f)
399 } else {
400 unreachablerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/unreachable!!()
401 }
402 }
403 Valuerust-analyzer cargo serde_json 1.0.151 value/Value#::Stringrust-analyzer cargo serde_json 1.0.151 value/Value#String#(s) => Unexpectedrust-analyzer cargo serde_core 1.0.229 de/Unexpected#::Strrust-analyzer cargo serde_core 1.0.229 de/Unexpected#Str#(s),
404 Valuerust-analyzer cargo serde_json 1.0.151 value/Value#::Arrayrust-analyzer cargo serde_json 1.0.151 value/Value#Array#(_) => Unexpectedrust-analyzer cargo serde_core 1.0.229 de/Unexpected#::Seqrust-analyzer cargo serde_core 1.0.229 de/Unexpected#Seq#,
405 };
406
407 Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(E::invalid_typerust-analyzer cargo serde_core 1.0.229 de/Error#invalid_type().(unexpected, &"object or boolean"))
408 }
409
410 #[ref_cast_customrust-analyzer cargo ref-cast-impl 1.0.26 ref_cast_custom:]
411 fn ref_castrust-analyzer cargo schemars 1.2.2 schema/impl#[Schema]ref_cast().(value: &Valuerust-analyzer cargo serde_json 1.0.151 value/Value#) -> &Selfrust-analyzer cargo schemars 1.2.2 schema/impl#[Schema];
412
413 #[ref_cast_customrust-analyzer cargo ref-cast-impl 1.0.26 ref_cast_custom:]
414 fn ref_cast_mutrust-analyzer cargo schemars 1.2.2 schema/impl#[Schema]ref_cast_mut().(value: &mut Valuerust-analyzer cargo serde_json 1.0.151 value/Value#) -> &mut Selfrust-analyzer cargo schemars 1.2.2 schema/impl#[Schema];
415 }
416
417 impl Fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/From#<Schemarust-analyzer cargo schemars 1.2.2 schema/Schema#> for Valuerust-analyzer cargo serde_json 1.0.151 value/Value# {
418 fn fromrust-analyzer cargo schemars 1.2.2 schema/impl#[Value][`From<Schema>`]from().(v: Schemarust-analyzer cargo schemars 1.2.2 schema/Schema#) -> Valuerust-analyzer cargo serde_json 1.0.151 value/Value# {
419 v.0rust-analyzer cargo schemars 1.2.2 schema/Schema#0.
420 }
421 }
422
423 impl corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::convertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/::TryFromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/TryFrom#<Valuerust-analyzer cargo serde_json 1.0.151 value/Value#> for Schemarust-analyzer cargo schemars 1.2.2 schema/Schema# {
424 type Errorrust-analyzer cargo schemars 1.2.2 schema/impl#[Schema][`TryFrom<Value>`]Error# = serde_jsonrust-analyzer cargo serde_json 1.0.151 crate/::Errorrust-analyzer cargo serde_json 1.0.151 error/Error#;
425
426 fn try_fromrust-analyzer cargo schemars 1.2.2 schema/impl#[Schema][`TryFrom<Value>`]try_from().(value: Valuerust-analyzer cargo serde_json 1.0.151 value/Value#) -> serde_jsonrust-analyzer cargo serde_json 1.0.151 crate/::Resultrust-analyzer cargo serde_json 1.0.151 error/Result#<Schemarust-analyzer cargo schemars 1.2.2 schema/Schema#> {
427 Schemarust-analyzer cargo schemars 1.2.2 schema/Schema#::validaterust-analyzer cargo schemars 1.2.2 schema/impl#[Schema]validate().(&value)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/try_trait/Try#branch().;
428 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Schemarust-analyzer cargo schemars 1.2.2 schema/Schema#(value))
429 }
430 }
431
432 impl<'a> corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::convertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/::TryFromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/TryFrom#<&'a Valuerust-analyzer cargo serde_json 1.0.151 value/Value#> for &'a Schemarust-analyzer cargo schemars 1.2.2 schema/Schema# {
433 type Errorrust-analyzer cargo schemars 1.2.2 schema/impl#[`&'a Schema`][`TryFrom<&'a Value>`]Error# = serde_jsonrust-analyzer cargo serde_json 1.0.151 crate/::Errorrust-analyzer cargo serde_json 1.0.151 error/Error#;
434
435 fn try_fromrust-analyzer cargo schemars 1.2.2 schema/impl#[`&'a Schema`][`TryFrom<&'a Value>`]try_from().(value: &Valuerust-analyzer cargo serde_json 1.0.151 value/Value#) -> serde_jsonrust-analyzer cargo serde_json 1.0.151 crate/::Resultrust-analyzer cargo serde_json 1.0.151 error/Result#<&Schemarust-analyzer cargo schemars 1.2.2 schema/Schema#> {
436 Schemarust-analyzer cargo schemars 1.2.2 schema/Schema#::validaterust-analyzer cargo schemars 1.2.2 schema/impl#[Schema]validate().(value)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/try_trait/Try#branch().;
437 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Schemarust-analyzer cargo schemars 1.2.2 schema/Schema#::ref_castrust-analyzer cargo schemars 1.2.2 schema/impl#[Schema]ref_cast().(value))
438 }
439 }
440
441 impl<'a> corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::convertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/::TryFromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/TryFrom#<&'a mut Valuerust-analyzer cargo serde_json 1.0.151 value/Value#> for &'a mut Schemarust-analyzer cargo schemars 1.2.2 schema/Schema# {
442 type Errorrust-analyzer cargo schemars 1.2.2 schema/impl#[`&'a mut Schema`][`TryFrom<&'a mut Value>`]Error# = serde_jsonrust-analyzer cargo serde_json 1.0.151 crate/::Errorrust-analyzer cargo serde_json 1.0.151 error/Error#;
443
444 fn try_fromrust-analyzer cargo schemars 1.2.2 schema/impl#[`&'a mut Schema`][`TryFrom<&'a mut Value>`]try_from().(value: &mut Valuerust-analyzer cargo serde_json 1.0.151 value/Value#) -> serde_jsonrust-analyzer cargo serde_json 1.0.151 crate/::Resultrust-analyzer cargo serde_json 1.0.151 error/Result#<&mut Schemarust-analyzer cargo schemars 1.2.2 schema/Schema#> {
445 Schemarust-analyzer cargo schemars 1.2.2 schema/Schema#::validaterust-analyzer cargo schemars 1.2.2 schema/impl#[Schema]validate().(value)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/try_trait/Try#branch().;
446 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Schemarust-analyzer cargo schemars 1.2.2 schema/Schema#::ref_cast_mutrust-analyzer cargo schemars 1.2.2 schema/impl#[Schema]ref_cast_mut().(value))
447 }
448 }
449
450 impl Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default# for Schemarust-analyzer cargo schemars 1.2.2 schema/Schema# {
451 fn defaultrust-analyzer cargo schemars 1.2.2 schema/impl#[Schema][Default]default().() -> Selfrust-analyzer cargo schemars 1.2.2 schema/impl#[Schema][Default] {
452 Selfrust-analyzer cargo schemars 1.2.2 schema/impl#[Schema][Default](Valuerust-analyzer cargo serde_json 1.0.151 value/Value#::Objectrust-analyzer cargo serde_json 1.0.151 value/Value#Object#(Maprust-analyzer cargo serde_json 1.0.151 map/Map#::newrust-analyzer cargo serde_json 1.0.151 map/impl#[`Map<String, Value>`]new().()))
453 }
454 }
455
456 impl Fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/From#<Maprust-analyzer cargo serde_json 1.0.151 map/Map#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#, Valuerust-analyzer cargo serde_json 1.0.151 value/Value#>> for Schemarust-analyzer cargo schemars 1.2.2 schema/Schema# {
457 fn fromrust-analyzer cargo schemars 1.2.2 schema/impl#[Schema][`From<Map<String, Value>>`]from().(o: Maprust-analyzer cargo serde_json 1.0.151 map/Map#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#, Valuerust-analyzer cargo serde_json 1.0.151 value/Value#>) -> Selfrust-analyzer cargo schemars 1.2.2 schema/impl#[Schema][`From<Map<String, Value>>`] {
458 Schemarust-analyzer cargo schemars 1.2.2 schema/Schema#(Valuerust-analyzer cargo serde_json 1.0.151 value/Value#::Objectrust-analyzer cargo serde_json 1.0.151 value/Value#Object#(o))
459 }
460 }
461
462 impl Fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/From#<bool> for Schemarust-analyzer cargo schemars 1.2.2 schema/Schema# {
463 fn fromrust-analyzer cargo schemars 1.2.2 schema/impl#[Schema][`From<bool>`]from().(b: bool) -> Selfrust-analyzer cargo schemars 1.2.2 schema/impl#[Schema][`From<bool>`] {
464 Schemarust-analyzer cargo schemars 1.2.2 schema/Schema#(Valuerust-analyzer cargo serde_json 1.0.151 value/Value#::Boolrust-analyzer cargo serde_json 1.0.151 value/Value#Bool#(b))
465 }
466 }
467
468 impl craterust-analyzer cargo schemars 1.2.2 crate/::JsonSchemarust-analyzer cargo schemars 1.2.2 JsonSchema# for Schemarust-analyzer cargo schemars 1.2.2 schema/Schema# {
469 fn schema_namerust-analyzer cargo schemars 1.2.2 schema/impl#[Schema][JsonSchema]schema_name().() -> allocrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc crate/::borrowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/::Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#<'static, str> {
470 "Schema".intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().()
471 }
472
473 fn schema_idrust-analyzer cargo schemars 1.2.2 schema/impl#[Schema][JsonSchema]schema_id().() -> allocrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc crate/::borrowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/::Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#<'static, str> {
474 "schemars::Schema".intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().()
475 }
476
477 fn json_schemarust-analyzer cargo schemars 1.2.2 schema/impl#[Schema][JsonSchema]json_schema().(_: &mut craterust-analyzer cargo schemars 1.2.2 crate/::SchemaGeneratorrust-analyzer cargo schemars 1.2.2 generate/SchemaGenerator#) -> Schemarust-analyzer cargo schemars 1.2.2 schema/Schema# {
478 craterust-analyzer cargo schemars 1.2.2 crate/::json_schemarust-analyzer cargo schemars 1.2.2 macros/json_schema!!({
479 "type": ["object", "boolean"]
480 })
481 }
482 }
483
484 mod serrust-analyzer cargo schemars 1.2.2 schema/ser/ {
485 use serderust-analyzer cargo serde 1.0.229 crate/::serrust-analyzer cargo serde_core 1.0.229 ser/::{Serializerust-analyzer cargo serde_core 1.0.229 ser/Serialize#, SerializeMaprust-analyzer cargo serde_core 1.0.229 ser/SerializeMap#, SerializeSeqrust-analyzer cargo serde_core 1.0.229 ser/SerializeSeq#};
486 use serde_jsonrust-analyzer cargo serde_json 1.0.151 crate/::Valuerust-analyzer cargo serde_json 1.0.151 value/Value#;
487
488 // The order of properties in a JSON Schema object is insignificant, but we explicitly order
489 // some of them here to make them easier for a human to read. All other properties are ordered
490 // either lexicographically (by default) or by insertion order (if `preserve_order` is enabled)
491 const ORDERED_KEYWORDS_STARTrust-analyzer cargo schemars 1.2.2 schema/ser/ORDERED_KEYWORDS_START.: [&str; 7] = [
492 "$id",
493 "$schema",
494 "title",
495 "description",
496 "type",
497 "format",
498 "properties",
499 ];
500 const ORDERED_KEYWORDS_ENDrust-analyzer cargo schemars 1.2.2 schema/ser/ORDERED_KEYWORDS_END.: [&str; 2] = ["$defs", "definitions"];
501
502 // `no_reorder` is true when the value is expected to be an object that is NOT a schema,
503 // but the object's property values are expected to be schemas. In this case, we do not
504 // reorder the object's direct properties, but we do reorder nested (subschema) properties.
505 //
506 // When `no_reorder` is false, then the value is expected to be one of:
507 // - a JSON schema object
508 // - an array of JSON schemas
509 // - a JSON primitive value (null/string/number/bool)
510 //
511 // If any of these expectations are not met, then the value should still be serialized in a
512 // valid way, but the property ordering may be unclear.
513 pub(superrust-analyzer cargo schemars 1.2.2 schema/) struct OrderedKeywordWrapperrust-analyzer cargo schemars 1.2.2 schema/ser/OrderedKeywordWrapper#<'a> {
514 valuerust-analyzer cargo schemars 1.2.2 schema/ser/OrderedKeywordWrapper#value.: &'a Valuerust-analyzer cargo serde_json 1.0.151 value/Value#,
515 no_reorderrust-analyzer cargo schemars 1.2.2 schema/ser/OrderedKeywordWrapper#no_reorder.: bool,
516 }
517
518 impl<'a> Fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/From#<&'a Valuerust-analyzer cargo serde_json 1.0.151 value/Value#> for OrderedKeywordWrapperrust-analyzer cargo schemars 1.2.2 schema/ser/OrderedKeywordWrapper#<'a> {
519 fn fromrust-analyzer cargo schemars 1.2.2 schema/ser/impl#[`OrderedKeywordWrapper<'a>`][`From<&'a Value>`]from().(value: &'a Valuerust-analyzer cargo serde_json 1.0.151 value/Value#) -> Selfrust-analyzer cargo schemars 1.2.2 schema/ser/impl#[`OrderedKeywordWrapper<'a>`][`From<&'a Value>`] {
520 Selfrust-analyzer cargo schemars 1.2.2 schema/ser/impl#[`OrderedKeywordWrapper<'a>`][`From<&'a Value>`] {
521 valuerust-analyzer cargo schemars 1.2.2 schema/ser/OrderedKeywordWrapper#value.,
522 no_reorderrust-analyzer cargo schemars 1.2.2 schema/ser/OrderedKeywordWrapper#no_reorder.: false,
523 }
524 }
525 }
526
527 impl Serializerust-analyzer cargo serde_core 1.0.229 ser/Serialize# for OrderedKeywordWrapperrust-analyzer cargo schemars 1.2.2 schema/ser/OrderedKeywordWrapper#<'_> {
528 fn serializerust-analyzer cargo schemars 1.2.2 schema/ser/impl#[`OrderedKeywordWrapper<'_>`][Serialize]serialize().<S>(&self, serializer: S) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<S::Okrust-analyzer cargo serde_core 1.0.229 ser/Serializer#Ok#, S::Errorrust-analyzer cargo serde_core 1.0.229 ser/Serializer#Error#>
529 where
530 S: serderust-analyzer cargo serde 1.0.229 crate/::Serializerrust-analyzer cargo serde_core 1.0.229 ser/Serializer#,
531 {
532 fn serialize_schema_propertyrust-analyzer cargo schemars 1.2.2 schema/ser/serialize_schema_property().<S>(
533 map: &mut S::SerializeMaprust-analyzer cargo serde_core 1.0.229 ser/Serializer#SerializeMap#,
534 key: &str,
535 value: &Valuerust-analyzer cargo serde_json 1.0.151 value/Value#,
536 ) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), S::Errorrust-analyzer cargo serde_core 1.0.229 ser/Serializer#Error#>
537 where
538 S: serderust-analyzer cargo serde 1.0.229 crate/::Serializerrust-analyzer cargo serde_core 1.0.229 ser/Serializer#,
539 {
540 if matchesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/matches!!(key, "examples" | "default") || key.starts_withrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]starts_with().("x-") {
541 // Value(s) of `examples`/`default` are plain values, not schemas.
542 // Also don't reorder values of custom properties.
543 map.serialize_entryrust-analyzer cargo serde_core 1.0.229 ser/SerializeMap#serialize_entry().(key, value)
544 } else {
545 let no_reorder = matchesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/matches!!(
546 key,
547 "properties"
548 | "patternProperties"
549 | "dependentSchemas"
550 | "$defs"
551 | "definitions"
552 );
553 map.serialize_entryrust-analyzer cargo serde_core 1.0.229 ser/SerializeMap#serialize_entry().(key, &OrderedKeywordWrapperrust-analyzer cargo schemars 1.2.2 schema/ser/OrderedKeywordWrapper# { valuerust-analyzer cargo schemars 1.2.2 schema/ser/OrderedKeywordWrapper#value., no_reorderrust-analyzer cargo schemars 1.2.2 schema/ser/OrderedKeywordWrapper#no_reorder. })
554 }
555 }
556
557 match self.valuerust-analyzer cargo schemars 1.2.2 schema/ser/OrderedKeywordWrapper#value. {
558 Valuerust-analyzer cargo serde_json 1.0.151 value/Value#::Arrayrust-analyzer cargo serde_json 1.0.151 value/Value#Array#(array) => {
559 let mut seq = serializer.serialize_seqrust-analyzer cargo serde_core 1.0.229 ser/Serializer#serialize_seq().(Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(array.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().()))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
560 for value in array {
561 seq.serialize_elementrust-analyzer cargo serde_core 1.0.229 ser/SerializeSeq#serialize_element().(&OrderedKeywordWrapperrust-analyzer cargo schemars 1.2.2 schema/ser/OrderedKeywordWrapper#::fromrust-analyzer cargo schemars 1.2.2 schema/ser/impl#[`OrderedKeywordWrapper<'a>`][`From<&'a Value>`]from().(value))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
562 }
563 seq.endrust-analyzer cargo serde_core 1.0.229 ser/SerializeSeq#end().()
564 }
565 Valuerust-analyzer cargo serde_json 1.0.151 value/Value#::Objectrust-analyzer cargo serde_json 1.0.151 value/Value#Object#(object) if self.no_reorderrust-analyzer cargo schemars 1.2.2 schema/ser/OrderedKeywordWrapper#no_reorder. => {
566 let mut map = serializer.serialize_maprust-analyzer cargo serde_core 1.0.229 ser/Serializer#serialize_map().(Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(object.lenrust-analyzer cargo serde_json 1.0.151 map/impl#[`Map<String, Value>`]len().()))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
567
568 for (key, value) in object {
569 // Don't use `serialize_schema_property` because `object` is NOT expected
570 // to be a schema (but `value` is expected to be a schema)
571 map.serialize_entryrust-analyzer cargo serde_core 1.0.229 ser/SerializeMap#serialize_entry().(key, &OrderedKeywordWrapperrust-analyzer cargo schemars 1.2.2 schema/ser/OrderedKeywordWrapper#::fromrust-analyzer cargo schemars 1.2.2 schema/ser/impl#[`OrderedKeywordWrapper<'a>`][`From<&'a Value>`]from().(value))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
572 }
573
574 map.endrust-analyzer cargo serde_core 1.0.229 ser/SerializeMap#end().()
575 }
576 Valuerust-analyzer cargo serde_json 1.0.151 value/Value#::Objectrust-analyzer cargo serde_json 1.0.151 value/Value#Object#(object) => {
577 let mut map = serializer.serialize_maprust-analyzer cargo serde_core 1.0.229 ser/Serializer#serialize_map().(Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(object.lenrust-analyzer cargo serde_json 1.0.151 map/impl#[`Map<String, Value>`]len().()))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
578
579 for key in ORDERED_KEYWORDS_STARTrust-analyzer cargo schemars 1.2.2 schema/ser/ORDERED_KEYWORDS_START. {
580 if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(value) = object.getrust-analyzer cargo serde_json 1.0.151 map/impl#[`Map<String, Value>`]get().(key) {
581 serialize_schema_propertyrust-analyzer cargo schemars 1.2.2 schema/ser/serialize_schema_property().::<S>(&mut map, key, value)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
582 }
583 }
584
585 for (key, value) in object {
586 if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().ORDERED_KEYWORDS_STARTrust-analyzer cargo schemars 1.2.2 schema/ser/ORDERED_KEYWORDS_START..containsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]contains().(&key.as_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]as_str().())
587 && !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().ORDERED_KEYWORDS_ENDrust-analyzer cargo schemars 1.2.2 schema/ser/ORDERED_KEYWORDS_END..containsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]contains().(&key.as_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]as_str().())
588 {
589 serialize_schema_propertyrust-analyzer cargo schemars 1.2.2 schema/ser/serialize_schema_property().::<S>(&mut map, key, value)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
590 }
591 }
592
593 for key in ORDERED_KEYWORDS_ENDrust-analyzer cargo schemars 1.2.2 schema/ser/ORDERED_KEYWORDS_END. {
594 if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(value) = object.getrust-analyzer cargo serde_json 1.0.151 map/impl#[`Map<String, Value>`]get().(key) {
595 serialize_schema_propertyrust-analyzer cargo schemars 1.2.2 schema/ser/serialize_schema_property().::<S>(&mut map, key, value)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
596 }
597 }
598
599 map.endrust-analyzer cargo serde_core 1.0.229 ser/SerializeMap#end().()
600 }
601 Valuerust-analyzer cargo serde_json 1.0.151 value/Value#::Nullrust-analyzer cargo serde_json 1.0.151 value/Value#Null# | Valuerust-analyzer cargo serde_json 1.0.151 value/Value#::Boolrust-analyzer cargo serde_json 1.0.151 value/Value#Bool#(_) | Valuerust-analyzer cargo serde_json 1.0.151 value/Value#::Numberrust-analyzer cargo serde_json 1.0.151 value/Value#Number#(_) | Valuerust-analyzer cargo serde_json 1.0.151 value/Value#::Stringrust-analyzer cargo serde_json 1.0.151 value/Value#String#(_) => {
602 self.valuerust-analyzer cargo schemars 1.2.2 schema/ser/OrderedKeywordWrapper#value..serializerust-analyzer cargo serde_json 1.0.151 value/ser/impl#[Value][Serialize]serialize().(serializer)
603 }
604 }
605 }
606 }
607 }