Debian Codegraph @

examples/custom_serialization.rs

raw symbols SCIP
1 use schemarsrust-analyzer cargo schemars 1.2.2 crate/::{schema_forrust-analyzer cargo schemars 1.2.2 macros/schema_for!, 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#};
2 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#};
3
4 // `int_as_string` and `bool_as_string` use the schema for `String`.
5 #[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:, Deserializerust-analyzer cargo serde_derive 1.0.229 Deserialize:, Serializerust-analyzer cargo serde_derive 1.0.229 Serialize:, JsonSchemarust-analyzer cargo schemars_derive 1.2.2 JsonSchema:)]
6 pub struct MyStructrust-analyzer cargo schemars 1.2.2 MyStruct# {
7 #[serderust-analyzer cargo serde_derive 1.0.229 serde:(default = "eight", with = "as_string")]
8 #[schemarsrust-analyzer cargo schemars_derive 1.2.2 schemars:(with = "String")]
9 pub int_as_stringrust-analyzer cargo schemars 1.2.2 MyStruct#int_as_string.: i32,
10
11 #[serderust-analyzer cargo serde_derive 1.0.229 serde:(default = "eight")]
12 pub int_normalrust-analyzer cargo schemars 1.2.2 MyStruct#int_normal.: i32,
13
14 #[serderust-analyzer cargo serde_derive 1.0.229 serde:(default, with = "as_string")]
15 #[schemarsrust-analyzer cargo schemars_derive 1.2.2 schemars:(schema_with = "make_custom_schema")]
16 pub bool_as_stringrust-analyzer cargo schemars 1.2.2 MyStruct#bool_as_string.: bool,
17
18 #[serderust-analyzer cargo serde_derive 1.0.229 serde:(default)]
19 pub bool_normalrust-analyzer cargo schemars 1.2.2 MyStruct#bool_normal.: bool,
20 }
21
22 fn make_custom_schemarust-analyzer cargo schemars 1.2.2 make_custom_schema().(generator: &mut SchemaGeneratorrust-analyzer cargo schemars 1.2.2 generate/SchemaGenerator#) -> Schemarust-analyzer cargo schemars 1.2.2 schema/Schema# {
23 let mut schema = Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#::json_schemarust-analyzer cargo schemars 1.2.2 json_schema_impls/primitives/impl#[String][JsonSchema]json_schema().(generator);
24 schema.insertrust-analyzer cargo schemars 1.2.2 schema/impl#[Schema]insert().("format".intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(), "boolean".intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().());
25 schema
26 }
27
28 fn eightrust-analyzer cargo schemars 1.2.2 eight().() -> i32 {
29 8
30 }
31
32 // This module serializes values as strings
33 mod as_stringrust-analyzer cargo schemars 1.2.2 as_string/ {
34 use 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#, Deserializerust-analyzer cargo serde_core 1.0.229 de/Deserialize#, Deserializerrust-analyzer cargo serde_core 1.0.229 de/Deserializer#, Serializerrust-analyzer cargo serde_core 1.0.229 ser/Serializer#};
35
36 pub fn serializerust-analyzer cargo schemars 1.2.2 as_string/serialize().<T, S>(value: &T, 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#>
37 where
38 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#,
39 S: Serializerrust-analyzer cargo serde_core 1.0.229 ser/Serializer#,
40 {
41 serializer.collect_strrust-analyzer cargo serde_core 1.0.229 ser/Serializer#collect_str().(value)
42 }
43
44 pub fn deserializerust-analyzer cargo schemars 1.2.2 as_string/deserialize().<'de, T, D>(deserializer: 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#>
45 where
46 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#,
47 D: Deserializerrust-analyzer cargo serde_core 1.0.229 de/Deserializer#<'de>,
48 {
49 let string = Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#::deserializerust-analyzer cargo serde_core 1.0.229 de/impls/impl#[String][`Deserialize<'de>`]deserialize().(deserializer)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
50 string
51 .parserust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]parse().()
52 .map_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map_err().(|_| D::Errorrust-analyzer cargo serde_core 1.0.229 de/Deserializer#Error#::customrust-analyzer cargo serde_core 1.0.229 de/Error#custom().("Input was not valid"))
53 }
54 }
55
56 fn mainrust-analyzer cargo schemars 1.2.2 main().() {
57 let schema = schema_forrust-analyzer cargo schemars 1.2.2 macros/schema_for!!(MyStructrust-analyzer cargo schemars 1.2.2 MyStruct#);
58 printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!("{}", serde_jsonrust-analyzer cargo serde_json 1.0.151 crate/::to_string_prettyrust-analyzer cargo serde_json 1.0.151 ser/to_string_pretty().(&schema).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().());
59 }