| 1 | use craterust-analyzer cargo schemars 1.2.2 crate/::preluderust-analyzer cargo schemars 1.2.2 prelude/::*; |
| 2 | use schemarsrust-analyzer cargo schemars 1.2.2 crate/::generaterust-analyzer cargo schemars 1.2.2 generate/::SchemaSettingsrust-analyzer cargo schemars 1.2.2 generate/SchemaSettings#; |
| 3 | |
| 4 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(JsonSchemarust-analyzer cargo schemars_derive 1.2.2 JsonSchema:, Deserializerust-analyzer cargo serde_derive 1.0.229 Deserialize:, Serializerust-analyzer cargo serde_derive 1.0.229 Serialize:, Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default:)] |
| 5 | struct Structrust-analyzer cargo schemars 1.2.2 schema_with/Struct#<T: Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default# + Serializerust-analyzer cargo serde_core 1.0.229 ser/Serialize#> { |
| 6 | #[serderust-analyzer cargo schemars_derive 1.2.2 serde:(with = "int_as_str")] |
| 7 | #[schemarsrust-analyzer cargo schemars_derive 1.2.2 schemars:(schema_with = "int_as_str::json_schema")] |
| 8 | xrust-analyzer cargo schemars 1.2.2 schema_with/Struct#x.: i64, |
| 9 | #[schemarsrust-analyzer cargo schemars_derive 1.2.2 schemars:(schema_with = "from_serialize_default::<T>")] |
| 10 | trust-analyzer cargo schemars 1.2.2 schema_with/Struct#t.: T, |
| 11 | } |
| 12 | |
| 13 | mod int_as_strrust-analyzer cargo schemars 1.2.2 schema_with/int_as_str/ { |
| 14 | pub(superrust-analyzer cargo schemars 1.2.2 schema_with/) fn serializerust-analyzer cargo schemars 1.2.2 schema_with/int_as_str/serialize().<S, T>(value: &T, ser: 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#> |
| 15 | where |
| 16 | S: serderust-analyzer cargo serde 1.0.229 crate/::Serializerrust-analyzer cargo serde_core 1.0.229 ser/Serializer#, |
| 17 | T: stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Displayrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Display#, |
| 18 | { |
| 19 | ser.collect_strrust-analyzer cargo serde_core 1.0.229 ser/Serializer#collect_str().(value) |
| 20 | } |
| 21 | |
| 22 | pub(superrust-analyzer cargo schemars 1.2.2 schema_with/) fn deserializerust-analyzer cargo schemars 1.2.2 schema_with/int_as_str/deserialize().<'de, D, T>(deser: D) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<T, D::Errorrust-analyzer cargo serde_core 1.0.229 de/Deserializer#Error#> |
| 23 | where |
| 24 | D: serderust-analyzer cargo serde 1.0.229 crate/::Deserializerrust-analyzer cargo serde_core 1.0.229 de/Deserializer#<'de>, |
| 25 | T: stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/::FromStrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/traits/FromStr#<Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/traits/FromStr#Err# = stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::numrust-analyzer cargo std https://github.com/rust-lang/rust/library/std num/::ParseIntErrorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/error/ParseIntError#>, |
| 26 | { |
| 27 | <&str as serderust-analyzer cargo serde 1.0.229 crate/::Deserializerust-analyzer cargo serde_core 1.0.229 de/Deserialize#>::deserializerust-analyzer cargo serde_core 1.0.229 de/impls/impl#[`&'a str`][`Deserialize<'de>`]deserialize().(deser)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch(). |
| 28 | .parserust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]parse().() |
| 29 | .map_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map_err().(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#::customrust-analyzer cargo serde_core 1.0.229 de/Error#custom().) |
| 30 | } |
| 31 | |
| 32 | pub(superrust-analyzer cargo schemars 1.2.2 schema_with/) fn json_schemarust-analyzer cargo schemars 1.2.2 schema_with/int_as_str/json_schema().(_: &mut schemarsrust-analyzer cargo schemars 1.2.2 crate/::SchemaGeneratorrust-analyzer cargo schemars 1.2.2 generate/SchemaGenerator#) -> schemarsrust-analyzer cargo schemars 1.2.2 crate/::Schemarust-analyzer cargo schemars 1.2.2 schema/Schema# { |
| 33 | schemarsrust-analyzer cargo schemars 1.2.2 crate/::json_schemarust-analyzer cargo schemars 1.2.2 macros/json_schema!!({ |
| 34 | "type": "string", |
| 35 | "pattern": r"^-?\d+$" |
| 36 | }) |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | fn from_serialize_defaultrust-analyzer cargo schemars 1.2.2 schema_with/from_serialize_default().<T: Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default# + Serializerust-analyzer cargo serde_core 1.0.229 ser/Serialize#>( |
| 41 | generator: &mut schemarsrust-analyzer cargo schemars 1.2.2 crate/::SchemaGeneratorrust-analyzer cargo schemars 1.2.2 generate/SchemaGenerator#, |
| 42 | ) -> schemarsrust-analyzer cargo schemars 1.2.2 crate/::Schemarust-analyzer cargo schemars 1.2.2 schema/Schema# { |
| 43 | generator |
| 44 | .settingsrust-analyzer cargo schemars 1.2.2 generate/impl#[SchemaGenerator]settings().() |
| 45 | .clonerust-analyzer cargo schemars 1.2.2 generate/impl#[SchemaSettings][Clone]clone().() |
| 46 | .into_generatorrust-analyzer cargo schemars 1.2.2 generate/impl#[SchemaSettings]into_generator().() |
| 47 | .into_root_schema_for_valuerust-analyzer cargo schemars 1.2.2 generate/impl#[SchemaGenerator]into_root_schema_for_value().(&T::defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default#default().()) |
| 48 | .unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().() |
| 49 | } |
| 50 | |
| 51 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 52 | fn field_schema_withrust-analyzer cargo schemars 1.2.2 schema_with/field_schema_with().() { |
| 53 | testrust-analyzer cargo schemars 1.2.2 test!!(Structrust-analyzer cargo schemars 1.2.2 schema_with/Struct#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#>) |
| 54 | .assert_snapshotrust-analyzer cargo schemars 1.2.2 test_helper/impl#[`TestHelper<T>`]assert_snapshot().() |
| 55 | .assert_allows_ser_roundtrip_defaultrust-analyzer cargo schemars 1.2.2 test_helper/impl#[`TestHelper<T>`]assert_allows_ser_roundtrip_default().() |
| 56 | .assert_matches_de_roundtriprust-analyzer cargo schemars 1.2.2 test_helper/impl#[`TestHelper<T>`]assert_matches_de_roundtrip().(arbitrary_values_exceptrust-analyzer cargo schemars 1.2.2 test_helper/arbitrary_values_except().( |
| 57 | Valuerust-analyzer cargo serde_json 1.0.151 value/Value#::is_arrayrust-analyzer cargo serde_json 1.0.151 value/impl#[Value]is_array()., |
| 58 | "structs with `#derive(Deserialize)` can technically be deserialized from sequences, but that's not intended to be used via JSON, so schemars ignores it", |
| 59 | )); |
| 60 | } |
| 61 | |
| 62 | struct NotJsonSchemarust-analyzer cargo schemars 1.2.2 schema_with/NotJsonSchema#; |
| 63 | |
| 64 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(JsonSchemarust-analyzer cargo schemars_derive 1.2.2 JsonSchema:)] |
| 65 | #[schemars(with = "Struct<String>")] |
| 66 | struct Struct2rust-analyzer cargo schemars 1.2.2 schema_with/Struct2# { |
| 67 | _xrust-analyzer cargo schemars 1.2.2 schema_with/Struct2#_x.: NotJsonSchemarust-analyzer cargo schemars 1.2.2 schema_with/NotJsonSchema#, |
| 68 | } |
| 69 | |
| 70 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 71 | fn container_withrust-analyzer cargo schemars 1.2.2 schema_with/container_with().() { |
| 72 | testrust-analyzer cargo schemars 1.2.2 test!!(Struct2rust-analyzer cargo schemars 1.2.2 schema_with/Struct2#).assert_identicalrust-analyzer cargo schemars 1.2.2 test_helper/impl#[`TestHelper<T>`]assert_identical().::<Structrust-analyzer cargo schemars 1.2.2 schema_with/Struct#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#>>(); |
| 73 | } |
| 74 | |
| 75 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(JsonSchemarust-analyzer cargo schemars_derive 1.2.2 JsonSchema:)] |
| 76 | #[schemars(with = "Struct<String>", description = "Testing...")] |
| 77 | struct Struct3rust-analyzer cargo schemars 1.2.2 schema_with/Struct3# { |
| 78 | _xrust-analyzer cargo schemars 1.2.2 schema_with/Struct3#_x.: NotJsonSchemarust-analyzer cargo schemars 1.2.2 schema_with/NotJsonSchema#, |
| 79 | } |
| 80 | |
| 81 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 82 | fn container_with_metadatarust-analyzer cargo schemars 1.2.2 schema_with/container_with_metadata().() { |
| 83 | testrust-analyzer cargo schemars 1.2.2 test!!(Struct3rust-analyzer cargo schemars 1.2.2 schema_with/Struct3#).assert_snapshotrust-analyzer cargo schemars 1.2.2 test_helper/impl#[`TestHelper<T>`]assert_snapshot().().customrust-analyzer cargo schemars 1.2.2 test_helper/impl#[`TestHelper<T>`]custom().(|schema, _| { |
| 84 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(schema.getrust-analyzer cargo schemars 1.2.2 schema/impl#[Schema]get().("description"), Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(&"Testing...".intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().())); |
| 85 | }); |
| 86 | |
| 87 | assert_nerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_ne!!(<Struct2rust-analyzer cargo schemars 1.2.2 schema_with/Struct2#>::schema_idrust-analyzer cargo schemars 1.2.2 schema_with/impl#[Struct2][JsonSchema]schema_id().(), <Struct3rust-analyzer cargo schemars 1.2.2 schema_with/Struct3#>::schema_idrust-analyzer cargo schemars 1.2.2 schema_with/impl#[Struct3][JsonSchema]schema_id().()); |
| 88 | } |
| 89 | |
| 90 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(JsonSchemarust-analyzer cargo schemars_derive 1.2.2 JsonSchema:)] |
| 91 | #[schemars(schema_with = "int_as_str::json_schema")] |
| 92 | struct IntAsStringrust-analyzer cargo schemars 1.2.2 schema_with/IntAsString# { |
| 93 | _xrust-analyzer cargo schemars 1.2.2 schema_with/IntAsString#_x.: NotJsonSchemarust-analyzer cargo schemars 1.2.2 schema_with/NotJsonSchema#, |
| 94 | } |
| 95 | |
| 96 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 97 | fn container_schema_withrust-analyzer cargo schemars 1.2.2 schema_with/container_schema_with().() { |
| 98 | testrust-analyzer cargo schemars 1.2.2 test!!(IntAsStringrust-analyzer cargo schemars 1.2.2 schema_with/IntAsString#) |
| 99 | .assert_snapshotrust-analyzer cargo schemars 1.2.2 test_helper/impl#[`TestHelper<T>`]assert_snapshot().() |
| 100 | .customrust-analyzer cargo schemars 1.2.2 test_helper/impl#[`TestHelper<T>`]custom().(|schema, contract| { |
| 101 | let mut generator = SchemaSettingsrust-analyzer cargo schemars 1.2.2 generate/SchemaSettings#::defaultrust-analyzer cargo schemars 1.2.2 generate/impl#[SchemaSettings][Default]default().() |
| 102 | .withrust-analyzer cargo schemars 1.2.2 generate/impl#[SchemaSettings]with().(|s| s.contractrust-analyzer cargo schemars 1.2.2 generate/SchemaSettings#contract. = contract) |
| 103 | .into_generatorrust-analyzer cargo schemars 1.2.2 generate/impl#[SchemaSettings]into_generator().(); |
| 104 | let expected = int_as_strrust-analyzer cargo schemars 1.2.2 schema_with/int_as_str/::json_schemarust-analyzer cargo schemars 1.2.2 schema_with/int_as_str/json_schema().(&mut generator); |
| 105 | |
| 106 | // `title` and `$schema` are added for root schemas - ignore for comparison purposes |
| 107 | let mut schema = schema.clonerust-analyzer cargo schemars 1.2.2 schema/impl#[Schema][Clone]clone().(); |
| 108 | schema.removerust-analyzer cargo schemars 1.2.2 schema/impl#[Schema]remove().("title"); |
| 109 | schema.removerust-analyzer cargo schemars 1.2.2 schema/impl#[Schema]remove().("$schema"); |
| 110 | |
| 111 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(schema, expected); |
| 112 | }); |
| 113 | } |