| 1 | use craterust-analyzer cargo schemars 1.2.2 crate/::_alloc_preluderust-analyzer cargo schemars 1.2.2 _alloc_prelude/::*; |
| 2 | use craterust-analyzer cargo schemars 1.2.2 crate/::transformrust-analyzer cargo schemars 1.2.2 transform/::{transform_immediate_subschemasrust-analyzer cargo schemars 1.2.2 transform/transform_immediate_subschemas()., Transformrust-analyzer cargo schemars 1.2.2 transform/Transform#}; |
| 3 | use craterust-analyzer cargo schemars 1.2.2 crate/::{JsonSchemarust-analyzer cargo schemars 1.2.2 JsonSchema#, Schemarust-analyzer cargo schemars 1.2.2 schema/Schema#, SchemaGeneratorrust-analyzer cargo schemars 1.2.2 generate/SchemaGenerator#}; |
| 4 | use 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#; |
| 5 | use serderust-analyzer cargo serde 1.0.229 crate/::Serializerust-analyzer cargo serde_core 1.0.229 ser/Serialize#; |
| 6 | use serde_jsonrust-analyzer cargo serde_json 1.0.151 crate/::{jsonrust-analyzer cargo serde_json 1.0.151 macros/json!, maprust-analyzer cargo serde_json 1.0.151 map/::Entryrust-analyzer cargo serde_json 1.0.151 map/Entry#, Maprust-analyzer cargo serde_json 1.0.151 map/Map#, Valuerust-analyzer cargo serde_json 1.0.151 value/Value#}; |
| 7 | |
| 8 | mod regex_syntaxrust-analyzer cargo schemars 1.2.2 _private/regex_syntax/; |
| 9 | mod rustdocrust-analyzer cargo schemars 1.2.2 _private/rustdoc/; |
| 10 | |
| 11 | pub extern crate allocrust-analyzer cargo schemars 1.2.2 _private/alloc/; |
| 12 | pub extern crate serde_jsonrust-analyzer cargo schemars 1.2.2 _private/serde_json/; |
| 13 | |
| 14 | pub use rustdocrust-analyzer cargo schemars 1.2.2 _private/rustdoc/::get_title_and_descriptionrust-analyzer cargo schemars 1.2.2 _private/rustdoc/get_title_and_description().; |
| 15 | |
| 16 | pub fn json_schema_for_internally_tagged_enum_newtype_variantrust-analyzer cargo schemars 1.2.2 _private/json_schema_for_internally_tagged_enum_newtype_variant().<T: ?Sizedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Sized# + JsonSchemarust-analyzer cargo schemars 1.2.2 JsonSchema#>( |
| 17 | generator: &mut SchemaGeneratorrust-analyzer cargo schemars 1.2.2 generate/SchemaGenerator#, |
| 18 | ) -> Schemarust-analyzer cargo schemars 1.2.2 schema/Schema# { |
| 19 | let mut schema = T::json_schemarust-analyzer cargo schemars 1.2.2 JsonSchema#json_schema().(generator); |
| 20 | |
| 21 | // Inline the newtype's inner schema if any of: |
| 22 | // - The type specifies that its schema should always be inlined |
| 23 | // - The generator settings specify that all schemas should be inlined |
| 24 | // - The inner type is a unit struct, which would cause an unsatisfiable schema due to mismatched `type`. |
| 25 | // In this case, we replace its type with "object" in `apply_internal_enum_variant_tag` |
| 26 | // - The inner schema specified `"additionalProperties": false` or `"unevaluatedProperties": false`, |
| 27 | // since that would disallow the variant tag. If additional/unevaluatedProperties is in the top-level |
| 28 | // schema, then we can leave it there, because it will "see" the variant tag property. But if it is |
| 29 | // nested e.g. in an `allOf`, then it must be removed, which is why we run `AllowUnknownProperties` |
| 30 | // but only on immediate subschemas. |
| 31 | |
| 32 | let mut transform = AllowUnknownPropertiesrust-analyzer cargo schemars 1.2.2 _private/AllowUnknownProperties#::defaultrust-analyzer cargo schemars 1.2.2 _private/impl#[AllowUnknownProperties][Default]default().(); |
| 33 | transform_immediate_subschemasrust-analyzer cargo schemars 1.2.2 transform/transform_immediate_subschemas().(&mut transform, &mut schema); |
| 34 | |
| 35 | if T::inline_schemarust-analyzer cargo schemars 1.2.2 JsonSchema#inline_schema().() |
| 36 | || generator.settingsrust-analyzer cargo schemars 1.2.2 generate/impl#[SchemaGenerator]settings().().inline_subschemasrust-analyzer cargo schemars 1.2.2 generate/SchemaSettings#inline_subschemas. |
| 37 | || schema.getrust-analyzer cargo schemars 1.2.2 schema/impl#[Schema]get().("type").and_thenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]and_then().(Valuerust-analyzer cargo serde_json 1.0.151 value/Value#::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#("null") |
| 38 | || schema.getrust-analyzer cargo schemars 1.2.2 schema/impl#[Schema]get().("additionalProperties").and_thenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]and_then().(Valuerust-analyzer cargo serde_json 1.0.151 value/Value#::as_boolrust-analyzer cargo serde_json 1.0.151 value/impl#[Value]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#(false) |
| 39 | || schema.getrust-analyzer cargo schemars 1.2.2 schema/impl#[Schema]get().("unevaluatedProperties").and_thenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]and_then().(Valuerust-analyzer cargo serde_json 1.0.151 value/Value#::as_boolrust-analyzer cargo serde_json 1.0.151 value/impl#[Value]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#(false) |
| 40 | || transform.did_modifyrust-analyzer cargo schemars 1.2.2 _private/AllowUnknownProperties#did_modify. |
| 41 | { |
| 42 | return schema; |
| 43 | } |
| 44 | |
| 45 | // ...otherwise, we can freely refer to the schema via a `$ref` |
| 46 | generator.subschema_forrust-analyzer cargo schemars 1.2.2 generate/impl#[SchemaGenerator]subschema_for().::<T>() |
| 47 | } |
| 48 | |
| 49 | // Helper for generating schemas for flattened enums and `Option` fields. |
| 50 | pub fn json_schema_for_flattenrust-analyzer cargo schemars 1.2.2 _private/json_schema_for_flatten().<T: ?Sizedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Sized# + JsonSchemarust-analyzer cargo schemars 1.2.2 JsonSchema#>( |
| 51 | generator: &mut SchemaGeneratorrust-analyzer cargo schemars 1.2.2 generate/SchemaGenerator#, |
| 52 | required: bool, |
| 53 | ) -> Schemarust-analyzer cargo schemars 1.2.2 schema/Schema# { |
| 54 | /// Non-generic inner function to reduce monomorphization overhead |
| 55 | fn innerrust-analyzer cargo schemars 1.2.2 _private/inner().(mut schema: Schemarust-analyzer cargo schemars 1.2.2 schema/Schema#, is_optional: bool) -> Schemarust-analyzer cargo schemars 1.2.2 schema/Schema# { |
| 56 | // Special handling for externally-tagged enums with unit variants. |
| 57 | // Unit variants are normally serialized as strings, but when flattened, are serialized |
| 58 | // as objects like `{ "VariantName": null }` |
| 59 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(unit_variants) = remove_unit_variantsrust-analyzer cargo schemars 1.2.2 _private/remove_unit_variants().(&mut schema) { |
| 60 | if let Valuerust-analyzer cargo serde_json 1.0.151 value/Value#::Arrayrust-analyzer cargo serde_json 1.0.151 value/Value#Array#(one_of) = schema |
| 61 | .ensure_objectrust-analyzer cargo schemars 1.2.2 schema/impl#[Schema]ensure_object().() |
| 62 | .entryrust-analyzer cargo serde_json 1.0.151 map/impl#[`Map<String, Value>`]entry().("oneOf") |
| 63 | .or_insertrust-analyzer cargo serde_json 1.0.151 map/impl#[`Entry<'a>`]or_insert().(Valuerust-analyzer cargo serde_json 1.0.151 value/Value#::Arrayrust-analyzer cargo serde_json 1.0.151 value/Value#Array#(Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]new().())) |
| 64 | { |
| 65 | one_of.extendrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Extend<T>`]extend().( |
| 66 | unit_variants |
| 67 | .iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().() |
| 68 | .filter_maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#filter_map().(Valuerust-analyzer cargo serde_json 1.0.151 value/Value#::as_strrust-analyzer cargo serde_json 1.0.151 value/impl#[Value]as_str().) |
| 69 | .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|variant| { |
| 70 | jsonrust-analyzer cargo serde_json 1.0.151 macros/json!!({ |
| 71 | "type": "object", |
| 72 | "properties": { |
| 73 | variant: { |
| 74 | "type": "null" |
| 75 | } |
| 76 | }, |
| 77 | "required": [variant], |
| 78 | }) |
| 79 | }), |
| 80 | ); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | if is_optional { |
| 85 | schema.removerust-analyzer cargo schemars 1.2.2 schema/impl#[Schema]remove().("required"); |
| 86 | |
| 87 | // Handle `Option<>` of externally/internally/adjacently-tagged enums |
| 88 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(one_of) = schema.removerust-analyzer cargo schemars 1.2.2 schema/impl#[Schema]remove().("oneOf") { |
| 89 | // We can't just add `{}` to the existing `oneOf`, because its items must be |
| 90 | // mutually-exclusive, and `{}` matches everything. |
| 91 | flattenrust-analyzer cargo schemars 1.2.2 _private/flatten().( |
| 92 | &mut schema, |
| 93 | json_schemarust-analyzer cargo schemars 1.2.2 macros/json_schema!!({ |
| 94 | "anyOf": [ |
| 95 | { "oneOf": one_of }, |
| 96 | {} |
| 97 | ] |
| 98 | }), |
| 99 | ); |
| 100 | } |
| 101 | |
| 102 | // Handle `Option<>` of untagged enums |
| 103 | if let 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#(any_of)) = schema.get_mutrust-analyzer cargo schemars 1.2.2 schema/impl#[Schema]get_mut().("anyOf") { |
| 104 | let empty_object = 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().()); |
| 105 | if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().any_of.containsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]contains().(&empty_object) { |
| 106 | any_of.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(empty_object); |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | // Always allow aditional/unevaluated properties, because the outer struct determines |
| 112 | // whether it denies unknown fields. |
| 113 | AllowUnknownPropertiesrust-analyzer cargo schemars 1.2.2 _private/AllowUnknownProperties#::defaultrust-analyzer cargo schemars 1.2.2 _private/impl#[AllowUnknownProperties][Default]default().().transformrust-analyzer cargo schemars 1.2.2 _private/impl#[AllowUnknownProperties][Transform]transform().(&mut schema); |
| 114 | |
| 115 | schema |
| 116 | } |
| 117 | |
| 118 | fn remove_unit_variantsrust-analyzer cargo schemars 1.2.2 _private/remove_unit_variants().(schema: &mut Schemarust-analyzer cargo schemars 1.2.2 schema/Schema#) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<Valuerust-analyzer cargo serde_json 1.0.151 value/Value#>> { |
| 119 | // For enums that only have unit variants, all variants are in `enum` |
| 120 | if schema.getrust-analyzer cargo schemars 1.2.2 schema/impl#[Schema]get().("type").and_thenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]and_then().(Valuerust-analyzer cargo serde_json 1.0.151 value/Value#::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#("string") { |
| 121 | // Remove both `"enum": [...]`... |
| 122 | if let 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#(a)) = schema.removerust-analyzer cargo schemars 1.2.2 schema/impl#[Schema]remove().("enum") { |
| 123 | // ...and `"type": "string"`, since the variants are not serialized as strings |
| 124 | schema.removerust-analyzer cargo schemars 1.2.2 schema/impl#[Schema]remove().("type"); |
| 125 | return Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(a); |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | // For enums that have unit and other variants, unit variants are in the first `oneOf` item |
| 130 | let one_of = schema.get_mutrust-analyzer cargo schemars 1.2.2 schema/impl#[Schema]get_mut().("oneOf")?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][Try]branch()..as_array_mutrust-analyzer cargo serde_json 1.0.151 value/impl#[Value]as_array_mut().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][Try]branch().; |
| 131 | let first = <&mut Schemarust-analyzer cargo schemars 1.2.2 schema/Schema#>::try_fromrust-analyzer cargo schemars 1.2.2 schema/impl#[`&'a mut Schema`][`TryFrom<&'a mut Value>`]try_from().(one_of.get_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]get_mut().(0)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][Try]branch().).okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]ok().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][Try]branch().; |
| 132 | if first.getrust-analyzer cargo schemars 1.2.2 schema/impl#[Schema]get().("type").and_thenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]and_then().(Valuerust-analyzer cargo serde_json 1.0.151 value/Value#::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#("string") { |
| 133 | if let 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#(a)) = first.removerust-analyzer cargo schemars 1.2.2 schema/impl#[Schema]remove().("enum") { |
| 134 | one_of.removerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]remove().(0); |
| 135 | return Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(a); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# |
| 140 | } |
| 141 | |
| 142 | innerrust-analyzer cargo schemars 1.2.2 _private/inner().( |
| 143 | T::_schemars_private_non_optional_json_schemarust-analyzer cargo schemars 1.2.2 JsonSchema#_schemars_private_non_optional_json_schema().(generator), |
| 144 | T::_schemars_private_is_optionrust-analyzer cargo schemars 1.2.2 JsonSchema#_schemars_private_is_option().() && !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().required, |
| 145 | ) |
| 146 | } |
| 147 | |
| 148 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default:)] |
| 149 | struct AllowUnknownPropertiesrust-analyzer cargo schemars 1.2.2 _private/AllowUnknownProperties# { |
| 150 | did_modifyrust-analyzer cargo schemars 1.2.2 _private/AllowUnknownProperties#did_modify.: bool, |
| 151 | } |
| 152 | |
| 153 | impl Transformrust-analyzer cargo schemars 1.2.2 transform/Transform# for AllowUnknownPropertiesrust-analyzer cargo schemars 1.2.2 _private/AllowUnknownProperties# { |
| 154 | fn transformrust-analyzer cargo schemars 1.2.2 _private/impl#[AllowUnknownProperties][Transform]transform().(&mut self, schema: &mut Schemarust-analyzer cargo schemars 1.2.2 schema/Schema#) { |
| 155 | if schema.getrust-analyzer cargo schemars 1.2.2 schema/impl#[Schema]get().("additionalProperties").and_thenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]and_then().(Valuerust-analyzer cargo serde_json 1.0.151 value/Value#::as_boolrust-analyzer cargo serde_json 1.0.151 value/impl#[Value]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#(false) { |
| 156 | schema.removerust-analyzer cargo schemars 1.2.2 schema/impl#[Schema]remove().("additionalProperties"); |
| 157 | self.did_modifyrust-analyzer cargo schemars 1.2.2 _private/AllowUnknownProperties#did_modify. = true; |
| 158 | } |
| 159 | if schema.getrust-analyzer cargo schemars 1.2.2 schema/impl#[Schema]get().("unevaluatedProperties").and_thenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]and_then().(Valuerust-analyzer cargo serde_json 1.0.151 value/Value#::as_boolrust-analyzer cargo serde_json 1.0.151 value/impl#[Value]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#(false) { |
| 160 | schema.removerust-analyzer cargo schemars 1.2.2 schema/impl#[Schema]remove().("unevaluatedProperties"); |
| 161 | self.did_modifyrust-analyzer cargo schemars 1.2.2 _private/AllowUnknownProperties#did_modify. = true; |
| 162 | } |
| 163 | |
| 164 | transform_immediate_subschemasrust-analyzer cargo schemars 1.2.2 transform/transform_immediate_subschemas().(self, schema); |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | /// Hack to simulate specialization: |
| 169 | /// `MaybeSerializeWrapper(x).maybe_to_value()` will resolve to either |
| 170 | /// - The inherent method `MaybeSerializeWrapper::maybe_to_value(...)` if x is `Serialize` |
| 171 | /// - The trait method `NoSerialize::maybe_to_value(...)` from the blanket impl otherwise |
| 172 | #[doc(hidden)] |
| 173 | #[macro_export] |
| 174 | macro_rules! _schemars_maybe_to_valuerust-analyzer cargo schemars 1.2.2 _private/_schemars_maybe_to_value! { |
| 175 | ($expression:expr) => {{ |
| 176 | #[allow(unused_imports)] |
| 177 | use $crate::_private::{MaybeSerializeWrapper, NoSerialize as _}; |
| 178 | |
| 179 | MaybeSerializeWrapper($expression).maybe_to_value() |
| 180 | }}; |
| 181 | } |
| 182 | |
| 183 | pub struct MaybeSerializeWrapperrust-analyzer cargo schemars 1.2.2 _private/MaybeSerializeWrapper#<T>(pub T); |
| 184 | |
| 185 | pub trait NoSerializerust-analyzer cargo schemars 1.2.2 _private/NoSerialize#: Sizedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Sized# { |
| 186 | fn maybe_to_valuerust-analyzer cargo schemars 1.2.2 _private/NoSerialize#maybe_to_value().(self) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Valuerust-analyzer cargo serde_json 1.0.151 value/Value#> { |
| 187 | Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | impl<T> NoSerializerust-analyzer cargo schemars 1.2.2 _private/NoSerialize# for T {} |
| 192 | |
| 193 | impl<T: Serializerust-analyzer cargo serde_core 1.0.229 ser/Serialize#> MaybeSerializeWrapperrust-analyzer cargo schemars 1.2.2 _private/MaybeSerializeWrapper#<T> { |
| 194 | pub fn maybe_to_valuerust-analyzer cargo schemars 1.2.2 _private/impl#[`MaybeSerializeWrapper<T>`]maybe_to_value().(self) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Valuerust-analyzer cargo serde_json 1.0.151 value/Value#> { |
| 195 | serde_jsonrust-analyzer cargo serde_json 1.0.151 crate/::valuerust-analyzer cargo serde_json 1.0.151 value/::to_valuerust-analyzer cargo serde_json 1.0.151 value/to_value().(self.0rust-analyzer cargo schemars 1.2.2 _private/MaybeSerializeWrapper#0.).okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]ok().() |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | /// Create a schema for a unit enum variant |
| 200 | #[must_use] |
| 201 | pub fn new_unit_enum_variantrust-analyzer cargo schemars 1.2.2 _private/new_unit_enum_variant().(variant: &str) -> Schemarust-analyzer cargo schemars 1.2.2 schema/Schema# { |
| 202 | json_schemarust-analyzer cargo schemars 1.2.2 macros/json_schema!!({ |
| 203 | "type": "string", |
| 204 | "const": variant, |
| 205 | }) |
| 206 | } |
| 207 | |
| 208 | /// Hack to simulate specialization: |
| 209 | /// `<MaybeJsonSchemaWrapper<T>>::maybe_schema_id()` will resolve to either |
| 210 | /// - The inherent method `MaybeJsonSchemaWrapper::maybe_schema_id()` if T impls `JsonSchema` |
| 211 | /// - this returns `T::schema_id()` |
| 212 | /// - The trait method `NoJsonSchema::maybe_schema_id()` from the blanket impl otherwise |
| 213 | /// - this returns `core::any::type_name::<T>()`` |
| 214 | #[doc(hidden)] |
| 215 | #[macro_export] |
| 216 | macro_rules! _schemars_maybe_schema_idrust-analyzer cargo schemars 1.2.2 _private/_schemars_maybe_schema_id! { |
| 217 | ($ty:ty) => {{ |
| 218 | #[allow(unused_imports)] |
| 219 | use $crate::_private::{MaybeJsonSchemaWrapper, NoJsonSchema as _}; |
| 220 | |
| 221 | <MaybeJsonSchemaWrapper<$ty>>::maybe_schema_id() |
| 222 | }}; |
| 223 | } |
| 224 | |
| 225 | pub struct MaybeJsonSchemaWrapperrust-analyzer cargo schemars 1.2.2 _private/MaybeJsonSchemaWrapper#<T: ?Sizedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Sized#>(corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::markerrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/::PhantomDatarust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/PhantomData#<T>); |
| 226 | |
| 227 | pub trait NoJsonSchemarust-analyzer cargo schemars 1.2.2 _private/NoJsonSchema# { |
| 228 | #[must_use] |
| 229 | fn maybe_schema_idrust-analyzer cargo schemars 1.2.2 _private/NoJsonSchema#maybe_schema_id().() -> Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#<'static, str> { |
| 230 | Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#::Borrowedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#Borrowed#(corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::anyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core any/::type_namerust-analyzer cargo core https://github.com/rust-lang/rust/library/core any/type_name().::<Self>()) |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | impl<T: ?Sizedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Sized#> NoJsonSchemarust-analyzer cargo schemars 1.2.2 _private/NoJsonSchema# for T {} |
| 235 | |
| 236 | impl<T: JsonSchemarust-analyzer cargo schemars 1.2.2 JsonSchema# + ?Sizedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Sized#> MaybeJsonSchemaWrapperrust-analyzer cargo schemars 1.2.2 _private/MaybeJsonSchemaWrapper#<T> { |
| 237 | #[must_use] |
| 238 | pub fn maybe_schema_idrust-analyzer cargo schemars 1.2.2 _private/impl#[`MaybeJsonSchemaWrapper<T>`]maybe_schema_id().() -> Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#<'static, str> { |
| 239 | T::schema_idrust-analyzer cargo schemars 1.2.2 JsonSchema#schema_id().() |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | /// Create a schema for an externally tagged enum variant |
| 244 | #[allow(clippy::needless_pass_by_value)] |
| 245 | #[must_use] |
| 246 | pub fn new_externally_tagged_enum_variantrust-analyzer cargo schemars 1.2.2 _private/new_externally_tagged_enum_variant().(variant: &str, sub_schema: Schemarust-analyzer cargo schemars 1.2.2 schema/Schema#) -> Schemarust-analyzer cargo schemars 1.2.2 schema/Schema# { |
| 247 | // TODO: this can be optimised by inserting the `sub_schema` as a `Value` rather than |
| 248 | // using the `json_schema!` macro which borrows and serializes the sub_schema |
| 249 | json_schemarust-analyzer cargo schemars 1.2.2 macros/json_schema!!({ |
| 250 | "type": "object", |
| 251 | "properties": { |
| 252 | variant: sub_schema |
| 253 | }, |
| 254 | "required": [variant], |
| 255 | "additionalProperties": false, |
| 256 | }) |
| 257 | } |
| 258 | |
| 259 | /// Update a schema for an internally tagged enum variant |
| 260 | pub fn apply_internal_enum_variant_tagrust-analyzer cargo schemars 1.2.2 _private/apply_internal_enum_variant_tag().( |
| 261 | schema: &mut Schemarust-analyzer cargo schemars 1.2.2 schema/Schema#, |
| 262 | tag_name: &str, |
| 263 | variant: &str, |
| 264 | deny_unknown_fields: bool, |
| 265 | ) { |
| 266 | let obj = schema.ensure_objectrust-analyzer cargo schemars 1.2.2 schema/impl#[Schema]ensure_object().(); |
| 267 | let is_unit = obj.getrust-analyzer cargo serde_json 1.0.151 map/impl#[`Map<String, Value>`]get().("type").and_thenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]and_then().(Valuerust-analyzer cargo serde_json 1.0.151 value/Value#::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#("null"); |
| 268 | |
| 269 | obj.insertrust-analyzer cargo serde_json 1.0.151 map/impl#[`Map<String, Value>`]insert().("type".to_ownedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str][ToOwned]to_owned().(), "object".intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().()); |
| 270 | |
| 271 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(properties) = obj |
| 272 | .entryrust-analyzer cargo serde_json 1.0.151 map/impl#[`Map<String, Value>`]entry().("properties") |
| 273 | .or_insertrust-analyzer cargo serde_json 1.0.151 map/impl#[`Entry<'a>`]or_insert().(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().())) |
| 274 | .as_object_mutrust-analyzer cargo serde_json 1.0.151 value/impl#[Value]as_object_mut().() |
| 275 | { |
| 276 | properties.insertrust-analyzer cargo serde_json 1.0.151 map/impl#[`Map<String, Value>`]insert().( |
| 277 | tag_name.to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(), |
| 278 | jsonrust-analyzer cargo serde_json 1.0.151 macros/json!!({ |
| 279 | "type": "string", |
| 280 | "const": variant |
| 281 | }), |
| 282 | ); |
| 283 | } |
| 284 | |
| 285 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(required) = obj |
| 286 | .entryrust-analyzer cargo serde_json 1.0.151 map/impl#[`Map<String, Value>`]entry().("required") |
| 287 | .or_insertrust-analyzer cargo serde_json 1.0.151 map/impl#[`Entry<'a>`]or_insert().(Valuerust-analyzer cargo serde_json 1.0.151 value/Value#::Arrayrust-analyzer cargo serde_json 1.0.151 value/Value#Array#(Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]new().())) |
| 288 | .as_array_mutrust-analyzer cargo serde_json 1.0.151 value/impl#[Value]as_array_mut().() |
| 289 | { |
| 290 | required.insertrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]insert().(0, tag_name.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().()); |
| 291 | } |
| 292 | |
| 293 | if deny_unknown_fields && is_unit { |
| 294 | obj.entryrust-analyzer cargo serde_json 1.0.151 map/impl#[`Map<String, Value>`]entry().("additionalProperties").or_insertrust-analyzer cargo serde_json 1.0.151 map/impl#[`Entry<'a>`]or_insert().(false.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().()); |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | pub fn insert_object_propertyrust-analyzer cargo schemars 1.2.2 _private/insert_object_property().( |
| 299 | schema: &mut Schemarust-analyzer cargo schemars 1.2.2 schema/Schema#, |
| 300 | key: &str, |
| 301 | is_optional: bool, |
| 302 | sub_schema: Schemarust-analyzer cargo schemars 1.2.2 schema/Schema#, |
| 303 | ) { |
| 304 | let obj = schema.ensure_objectrust-analyzer cargo schemars 1.2.2 schema/impl#[Schema]ensure_object().(); |
| 305 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(properties) = obj |
| 306 | .entryrust-analyzer cargo serde_json 1.0.151 map/impl#[`Map<String, Value>`]entry().("properties") |
| 307 | .or_insertrust-analyzer cargo serde_json 1.0.151 map/impl#[`Entry<'a>`]or_insert().(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().())) |
| 308 | .as_object_mutrust-analyzer cargo serde_json 1.0.151 value/impl#[Value]as_object_mut().() |
| 309 | { |
| 310 | properties.insertrust-analyzer cargo serde_json 1.0.151 map/impl#[`Map<String, Value>`]insert().(key.to_ownedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str][ToOwned]to_owned().(), sub_schema.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().()); |
| 311 | } |
| 312 | |
| 313 | if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().is_optional { |
| 314 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(req) = obj |
| 315 | .entryrust-analyzer cargo serde_json 1.0.151 map/impl#[`Map<String, Value>`]entry().("required") |
| 316 | .or_insertrust-analyzer cargo serde_json 1.0.151 map/impl#[`Entry<'a>`]or_insert().(Valuerust-analyzer cargo serde_json 1.0.151 value/Value#::Arrayrust-analyzer cargo serde_json 1.0.151 value/Value#Array#(Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]new().())) |
| 317 | .as_array_mutrust-analyzer cargo serde_json 1.0.151 value/impl#[Value]as_array_mut().() |
| 318 | { |
| 319 | req.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(key.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().()); |
| 320 | } |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | pub fn insert_metadata_property_if_nonemptyrust-analyzer cargo schemars 1.2.2 _private/insert_metadata_property_if_nonempty().( |
| 325 | schema: &mut Schemarust-analyzer cargo schemars 1.2.2 schema/Schema#, |
| 326 | key: &str, |
| 327 | value: impl Intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Into#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#>, |
| 328 | ) { |
| 329 | let value: Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String# = value.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Into#into().(); |
| 330 | if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().value.is_emptyrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]is_empty().() { |
| 331 | schema.insertrust-analyzer cargo schemars 1.2.2 schema/impl#[Schema]insert().(key.to_ownedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str][ToOwned]to_owned().(), value.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().()); |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | pub fn insert_validation_propertyrust-analyzer cargo schemars 1.2.2 _private/insert_validation_property().( |
| 336 | schema: &mut Schemarust-analyzer cargo schemars 1.2.2 schema/Schema#, |
| 337 | required_type: &str, |
| 338 | key: &str, |
| 339 | value: impl Intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Into#<Valuerust-analyzer cargo serde_json 1.0.151 value/Value#>, |
| 340 | ) { |
| 341 | if schema.has_typerust-analyzer cargo schemars 1.2.2 schema/impl#[Schema]has_type().(required_type) || (required_type 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()."number" && schema.has_typerust-analyzer cargo schemars 1.2.2 schema/impl#[Schema]has_type().("integer")) { |
| 342 | schema.insertrust-analyzer cargo schemars 1.2.2 schema/impl#[Schema]insert().(key.to_ownedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str][ToOwned]to_owned().(), value.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Into#into().()); |
| 343 | } |
| 344 | } |
| 345 | |
| 346 | pub fn must_containrust-analyzer cargo schemars 1.2.2 _private/must_contain().(schema: &mut Schemarust-analyzer cargo schemars 1.2.2 schema/Schema#, substring: &str) { |
| 347 | let escaped = regex_syntaxrust-analyzer cargo schemars 1.2.2 _private/regex_syntax/::escaperust-analyzer cargo schemars 1.2.2 _private/regex_syntax/escape().(substring); |
| 348 | insert_validation_propertyrust-analyzer cargo schemars 1.2.2 _private/insert_validation_property().(schema, "string", "pattern", escaped); |
| 349 | } |
| 350 | |
| 351 | pub fn apply_inner_validationrust-analyzer cargo schemars 1.2.2 _private/apply_inner_validation().(schema: &mut Schemarust-analyzer cargo schemars 1.2.2 schema/Schema#, f: fn(&mut Schemarust-analyzer cargo schemars 1.2.2 schema/Schema#) -> ()) { |
| 352 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(inner_schema) = schema.get_mutrust-analyzer cargo schemars 1.2.2 schema/impl#[Schema]get_mut().("items").and_thenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]and_then().(|i| i.try_intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`TryInto<U>`]try_into().().okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]ok().()) { |
| 353 | f(inner_schema); |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | pub fn flattenrust-analyzer cargo schemars 1.2.2 _private/flatten().(schema: &mut Schemarust-analyzer cargo schemars 1.2.2 schema/Schema#, other: Schemarust-analyzer cargo schemars 1.2.2 schema/Schema#) { |
| 358 | fn flatten_propertyrust-analyzer cargo schemars 1.2.2 _private/flatten_property().(obj1: &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#>, key: Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#, value2: Valuerust-analyzer cargo serde_json 1.0.151 value/Value#) { |
| 359 | match obj1.entryrust-analyzer cargo serde_json 1.0.151 map/impl#[`Map<String, Value>`]entry().(key) { |
| 360 | Entryrust-analyzer cargo serde_json 1.0.151 map/Entry#::Vacantrust-analyzer cargo serde_json 1.0.151 map/Entry#Vacant#(vacant) => { |
| 361 | vacant.insertrust-analyzer cargo serde_json 1.0.151 map/impl#[`VacantEntry<'a>`]insert().(value2); |
| 362 | } |
| 363 | Entryrust-analyzer cargo serde_json 1.0.151 map/Entry#::Occupiedrust-analyzer cargo serde_json 1.0.151 map/Entry#Occupied#(occupied) => { |
| 364 | match occupied.keyrust-analyzer cargo serde_json 1.0.151 map/impl#[`OccupiedEntry<'a>`]key().().as_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]as_str().() { |
| 365 | "required" | "allOf" => { |
| 366 | if let Valuerust-analyzer cargo serde_json 1.0.151 value/Value#::Arrayrust-analyzer cargo serde_json 1.0.151 value/Value#Array#(a1) = occupied.into_mutrust-analyzer cargo serde_json 1.0.151 map/impl#[`OccupiedEntry<'a>`]into_mut().() { |
| 367 | if let Valuerust-analyzer cargo serde_json 1.0.151 value/Value#::Arrayrust-analyzer cargo serde_json 1.0.151 value/Value#Array#(a2) = value2 { |
| 368 | a1.extendrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Extend<T>`]extend().(a2); |
| 369 | } |
| 370 | } |
| 371 | } |
| 372 | "properties" | "patternProperties" => { |
| 373 | if let Valuerust-analyzer cargo serde_json 1.0.151 value/Value#::Objectrust-analyzer cargo serde_json 1.0.151 value/Value#Object#(o1) = occupied.into_mutrust-analyzer cargo serde_json 1.0.151 map/impl#[`OccupiedEntry<'a>`]into_mut().() { |
| 374 | if let Valuerust-analyzer cargo serde_json 1.0.151 value/Value#::Objectrust-analyzer cargo serde_json 1.0.151 value/Value#Object#(o2) = value2 { |
| 375 | o1.extendrust-analyzer cargo serde_json 1.0.151 map/impl#[`Map<String, Value>`][`Extend<(String, Value)>`]extend().(o2); |
| 376 | } |
| 377 | } |
| 378 | } |
| 379 | "oneOf" | "anyOf" => { |
| 380 | let (key, current) = occupied.remove_entryrust-analyzer cargo serde_json 1.0.151 map/impl#[`OccupiedEntry<'a>`]remove_entry().(); |
| 381 | flatten_propertyrust-analyzer cargo schemars 1.2.2 _private/flatten_property().( |
| 382 | obj1, |
| 383 | "allOf".to_ownedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str][ToOwned]to_owned().(), |
| 384 | jsonrust-analyzer cargo serde_json 1.0.151 macros/json!!([ |
| 385 | { &key: current }, |
| 386 | { key: value2 } |
| 387 | ]), |
| 388 | ); |
| 389 | } |
| 390 | _ => { |
| 391 | // leave the original value as it is (don't modify `schema`) |
| 392 | } |
| 393 | } |
| 394 | } |
| 395 | } |
| 396 | } |
| 397 | |
| 398 | match other.try_to_objectrust-analyzer cargo schemars 1.2.2 schema/impl#[Schema]try_to_object().() { |
| 399 | Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(false) => {} |
| 400 | Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(true) => { |
| 401 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(obj) = schema.as_object_mutrust-analyzer cargo schemars 1.2.2 schema/impl#[Schema]as_object_mut().() { |
| 402 | if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().obj.contains_keyrust-analyzer cargo serde_json 1.0.151 map/impl#[`Map<String, Value>`]contains_key().("additionalProperties") |
| 403 | && !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().obj.contains_keyrust-analyzer cargo serde_json 1.0.151 map/impl#[`Map<String, Value>`]contains_key().("unevaluatedProperties") |
| 404 | { |
| 405 | let key = if contains_immediate_subschemarust-analyzer cargo schemars 1.2.2 _private/contains_immediate_subschema().(obj) { |
| 406 | "unevaluatedProperties" |
| 407 | } else { |
| 408 | "additionalProperties" |
| 409 | }; |
| 410 | obj.insertrust-analyzer cargo serde_json 1.0.151 map/impl#[`Map<String, Value>`]insert().(key.to_ownedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str][ToOwned]to_owned().(), true.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().()); |
| 411 | } |
| 412 | } |
| 413 | } |
| 414 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(mut obj2) => { |
| 415 | let obj1 = schema.ensure_objectrust-analyzer cargo schemars 1.2.2 schema/impl#[Schema]ensure_object().(); |
| 416 | |
| 417 | // For complex merges, replace `additionalProperties` with `unevaluatedProperties` |
| 418 | // which usually "works out better". |
| 419 | normalise_additional_unevaluated_propertiesrust-analyzer cargo schemars 1.2.2 _private/normalise_additional_unevaluated_properties().(obj1, &obj2); |
| 420 | normalise_additional_unevaluated_propertiesrust-analyzer cargo schemars 1.2.2 _private/normalise_additional_unevaluated_properties().(&mut obj2, obj1); |
| 421 | |
| 422 | for (key, value2) in obj2 { |
| 423 | flatten_propertyrust-analyzer cargo schemars 1.2.2 _private/flatten_property().(obj1, key, value2); |
| 424 | } |
| 425 | } |
| 426 | } |
| 427 | } |
| 428 | |
| 429 | fn normalise_additional_unevaluated_propertiesrust-analyzer cargo schemars 1.2.2 _private/normalise_additional_unevaluated_properties().( |
| 430 | schema_obj1: &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#>, |
| 431 | schema_obj2: &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#>, |
| 432 | ) { |
| 433 | if schema_obj1.contains_keyrust-analyzer cargo serde_json 1.0.151 map/impl#[`Map<String, Value>`]contains_key().("additionalProperties") |
| 434 | && (schema_obj2.contains_keyrust-analyzer cargo serde_json 1.0.151 map/impl#[`Map<String, Value>`]contains_key().("unevaluatedProperties") |
| 435 | || contains_immediate_subschemarust-analyzer cargo schemars 1.2.2 _private/contains_immediate_subschema().(schema_obj2)) |
| 436 | { |
| 437 | let ap = schema_obj1.removerust-analyzer cargo serde_json 1.0.151 map/impl#[`Map<String, Value>`]remove().("additionalProperties"); |
| 438 | schema_obj1.insertrust-analyzer cargo serde_json 1.0.151 map/impl#[`Map<String, Value>`]insert().("unevaluatedProperties".to_ownedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str][ToOwned]to_owned().(), ap.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().()); |
| 439 | } |
| 440 | } |
| 441 | |
| 442 | fn contains_immediate_subschemarust-analyzer cargo schemars 1.2.2 _private/contains_immediate_subschema().(schema_obj: &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 { |
| 443 | ["if", "allOf", "anyOf", "oneOf", "$ref"] |
| 444 | .into_iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/iter/impl#[`[T; N]`][IntoIterator]into_iter().() |
| 445 | .anyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#any().(|k| schema_obj.contains_keyrust-analyzer cargo serde_json 1.0.151 map/impl#[`Map<String, Value>`]contains_key().(k)) |
| 446 | } |
| 447 | |
| 448 | pub(craterust-analyzer cargo schemars 1.2.2 crate/) fn allow_nullrust-analyzer cargo schemars 1.2.2 _private/allow_null().(generator: &mut SchemaGeneratorrust-analyzer cargo schemars 1.2.2 generate/SchemaGenerator#, schema: &mut Schemarust-analyzer cargo schemars 1.2.2 schema/Schema#) { |
| 449 | fn is_null_schemarust-analyzer cargo schemars 1.2.2 _private/is_null_schema().(value: &Valuerust-analyzer cargo serde_json 1.0.151 value/Value#) -> bool { |
| 450 | <&Schemarust-analyzer cargo schemars 1.2.2 schema/Schema#>::try_fromrust-analyzer cargo schemars 1.2.2 schema/impl#[`&'a Schema`][`TryFrom<&'a Value>`]try_from().(value).is_ok_andrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_ok_and().(|s| s.has_typerust-analyzer cargo schemars 1.2.2 schema/impl#[Schema]has_type().("null")) |
| 451 | } |
| 452 | |
| 453 | match schema.try_as_object_mutrust-analyzer cargo schemars 1.2.2 schema/impl#[Schema]try_as_object_mut().() { |
| 454 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(obj) => { |
| 455 | if obj.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 cmp/impls/impl#[usize][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq().1 |
| 456 | && obj |
| 457 | .getrust-analyzer cargo serde_json 1.0.151 map/impl#[`Map<String, Value>`]get().("anyOf") |
| 458 | .and_thenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]and_then().(Valuerust-analyzer cargo serde_json 1.0.151 value/Value#::as_arrayrust-analyzer cargo serde_json 1.0.151 value/impl#[Value]as_array().) |
| 459 | .is_some_andrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]is_some_and().(|a| a.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().(is_null_schemarust-analyzer cargo schemars 1.2.2 _private/is_null_schema().)) |
| 460 | { |
| 461 | return; |
| 462 | } |
| 463 | |
| 464 | if contains_immediate_subschemarust-analyzer cargo schemars 1.2.2 _private/contains_immediate_subschema().(obj) { |
| 465 | *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&mut T`][Deref]deref().schema = json_schemarust-analyzer cargo schemars 1.2.2 macros/json_schema!!({ |
| 466 | "anyOf": [ |
| 467 | obj, |
| 468 | <()>::json_schemarust-analyzer cargo schemars 1.2.2 json_schema_impls/primitives/impl#[`()`][JsonSchema]json_schema().(generator) |
| 469 | ] |
| 470 | }); |
| 471 | // No need to check `type`/`const`/`enum` because they're trivially not present |
| 472 | return; |
| 473 | } |
| 474 | |
| 475 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(instance_type) = obj.get_mutrust-analyzer cargo serde_json 1.0.151 map/impl#[`Map<String, Value>`]get_mut().("type") { |
| 476 | match instance_type { |
| 477 | Valuerust-analyzer cargo serde_json 1.0.151 value/Value#::Arrayrust-analyzer cargo serde_json 1.0.151 value/Value#Array#(array) => { |
| 478 | let null = Valuerust-analyzer cargo serde_json 1.0.151 value/Value#::fromrust-analyzer cargo serde_json 1.0.151 value/from/impl#[Value][`From<&str>`]from().("null"); |
| 479 | if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().array.containsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]contains().(&null) { |
| 480 | array.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(null); |
| 481 | } |
| 482 | } |
| 483 | Valuerust-analyzer cargo serde_json 1.0.151 value/Value#::Stringrust-analyzer cargo serde_json 1.0.151 value/Value#String#(string) if string rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&mut A`][`PartialEq<&B>`]ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&mut A`][`PartialEq<&B>`]ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&mut A`][`PartialEq<&B>`]ne()."null" => { |
| 484 | let current_type = corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::takerust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/take().(string).intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(); |
| 485 | *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&mut T`][Deref]deref().instance_type = Valuerust-analyzer cargo serde_json 1.0.151 value/Value#::Arrayrust-analyzer cargo serde_json 1.0.151 value/Value#Array#(vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![current_type, "null".intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().()]); |
| 486 | } |
| 487 | _ => {} |
| 488 | } |
| 489 | } |
| 490 | |
| 491 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(c) = obj.removerust-analyzer cargo serde_json 1.0.151 map/impl#[`Map<String, Value>`]remove().("const") { |
| 492 | if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().c.is_nullrust-analyzer cargo serde_json 1.0.151 value/impl#[Value]is_null().() { |
| 493 | obj.insertrust-analyzer cargo serde_json 1.0.151 map/impl#[`Map<String, Value>`]insert().("enum".to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(), Valuerust-analyzer cargo serde_json 1.0.151 value/Value#::Arrayrust-analyzer cargo serde_json 1.0.151 value/Value#Array#(vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![c, Valuerust-analyzer cargo serde_json 1.0.151 value/Value#::Nullrust-analyzer cargo serde_json 1.0.151 value/Value#Null#])); |
| 494 | } |
| 495 | } else if let 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#(e)) = obj.get_mutrust-analyzer cargo serde_json 1.0.151 map/impl#[`Map<String, Value>`]get_mut().("enum") { |
| 496 | if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().e.containsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]contains().(&Valuerust-analyzer cargo serde_json 1.0.151 value/Value#::Nullrust-analyzer cargo serde_json 1.0.151 value/Value#Null#) { |
| 497 | e.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(Valuerust-analyzer cargo serde_json 1.0.151 value/Value#::Nullrust-analyzer cargo serde_json 1.0.151 value/Value#Null#); |
| 498 | } |
| 499 | } |
| 500 | } |
| 501 | Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(true) => {} |
| 502 | Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(false) => { |
| 503 | *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&mut T`][Deref]deref().schema = <()>::json_schemarust-analyzer cargo schemars 1.2.2 json_schema_impls/primitives/impl#[`()`][JsonSchema]json_schema().(generator); |
| 504 | } |
| 505 | } |
| 506 | } |
| 507 | |
| 508 | #[cfg(test)] |
| 509 | mod testsrust-analyzer cargo schemars 1.2.2 _private/tests/ { |
| 510 | use pretty_assertionsrust-analyzer cargo pretty_assertions 1.4.1 crate/::assert_eqrust-analyzer cargo pretty_assertions 1.4.1 assert_eq!; |
| 511 | |
| 512 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 513 | fn nested_option_schemasrust-analyzer cargo schemars 1.2.2 _private/tests/nested_option_schemas().() { |
| 514 | let mut option_schema = schema_forrust-analyzer cargo schemars 1.2.2 macros/schema_for!!(Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<i8, u8>>); |
| 515 | option_schema.removerust-analyzer cargo schemars 1.2.2 schema/impl#[Schema]remove().("title"); |
| 516 | let mut nested_option_schema = schema_forrust-analyzer cargo schemars 1.2.2 macros/schema_for!!(Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<i8, u8>>>>); |
| 517 | nested_option_schema.removerust-analyzer cargo schemars 1.2.2 schema/impl#[Schema]remove().("title"); |
| 518 | |
| 519 | assert_eqrust-analyzer cargo pretty_assertions 1.4.1 assert_eq!!(option_schema, nested_option_schema); |
| 520 | } |
| 521 | } |