Debian Codegraph @

tests/integration/map.rs

raw symbols SCIP
1 use crate::prelude::*;
2 use std::collections::{BTreeMap, HashMap};
3
4 #[derive(JsonSchema, Deserialize, Serialize, Default, PartialEq, Eq, PartialOrd, Ord)]
5 #[schemars(extend("pattern" = "^[0-9a-f]*$"))]
6 struct HexNumber(String);
7
8 #[derive(JsonSchema, Deserialize, Serialize, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
9 struct IndirectU32(u32);
10
11 #[derive(JsonSchema, Deserialize, Serialize, PartialEq, Eq, PartialOrd, Ord, Hash)]
12 enum Enum {
13 Unit1,
14 Unit2,
15 #[serde(untagged)]
16 UntaggedI8(i8),
17 #[serde(untagged)]
18 UntaggedIndirectU32(IndirectU32),
19 }
20
21 #[derive(JsonSchema, Deserialize, Serialize, Default)]
22 struct Maps {
23 s_map: HashMap<String, bool>,
24 i_map: BTreeMap<i8, bool>,
25 u_map: HashMap<u64, bool>,
26 pattern_map: BTreeMap<HexNumber, bool>,
27 enum_map: HashMap<Enum, bool>,
28 }
29
30 #[test]
31 fn maps() {
32 test!(Maps)
33 .assert_snapshot()
34 .assert_allows_ser_roundtrip([Maps {
35 s_map: HashMap::from_iter([("test".to_owned(), true)]),
36 i_map: BTreeMap::from_iter([(-123, true), (123, true)]),
37 u_map: HashMap::from_iter([(123, true)]),
38 pattern_map: BTreeMap::from_iter([(HexNumber("b4df00d".to_owned()), true)]),
39 enum_map: HashMap::from_iter([(Enum::Unit1, true)]),
40 }])
41 .assert_allows_ser_only(
42 // serde allow serializing untagged non-string newtype variants, but not deserializing them.
43 [
44 Maps {
45 enum_map: HashMap::from_iter([(Enum::UntaggedI8(-100), true)]),
46 ..Default::default()
47 },
48 Maps {
49 // It's unfortunate that this adds an unused `IndirectU32` to the schema's `$defs`,
50 // but it's ultimately harmless.
51 enum_map: HashMap::from_iter([(
52 Enum::UntaggedIndirectU32(IndirectU32(1000000)),
53 true,
54 )]),
55 ..Default::default()
56 },
57 ],
58 )
59 .assert_matches_de_roundtrip(arbitrary_values());
60 }