Debian Codegraph @

debian/rust-vendor/zerovec/src/schemars.rs

raw
1 // This file is part of ICU4X. For terms of use, please see the file
2 // called LICENSE at the top level of the ICU4X source tree
3 // (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).
4
5 use crate::{
6 map::ZeroMapKV,
7 ule::{AsULE, VarULE},
8 vecs::VarZeroVecFormat,
9 VarZeroVec, ZeroMap, ZeroSlice, ZeroVec,
10 };
11 use alloc::{borrow::Cow, format};
12 use schemars::JsonSchema;
13
14 impl<T: VarULE + JsonSchema + ?Sized, F: VarZeroVecFormat> JsonSchema for VarZeroVec<'_, T, F> {
15 fn inline_schema() -> bool {
16 true
17 }
18
19 fn schema_name() -> Cow<'static, str> {
20 format!("VarZeroVec<{}>", T::schema_name()).into()
21 }
22
23 fn json_schema(generator: &mut schemars::SchemaGenerator) -> schemars::Schema {
24 schemars::json_schema!({
25 "type": "array",
26 "items": generator.subschema_for::<T>(),
27 })
28 }
29 }
30
31 impl<'a, T: AsULE + JsonSchema> JsonSchema for ZeroVec<'a, T> {
32 fn inline_schema() -> bool {
33 true
34 }
35
36 fn schema_name() -> Cow<'static, str> {
37 alloc::format!("ZeroVec<{}>", T::schema_name()).into()
38 }
39
40 fn json_schema(generator: &mut schemars::SchemaGenerator) -> schemars::Schema {
41 schemars::json_schema!({
42 "type": "array",
43 "items": generator.subschema_for::<T>(),
44 })
45 }
46 }
47
48 impl<T: AsULE + JsonSchema> JsonSchema for ZeroSlice<T> {
49 fn inline_schema() -> bool {
50 true
51 }
52
53 fn schema_name() -> Cow<'static, str> {
54 format!("ZeroSlice<{}>", T::schema_name()).into()
55 }
56
57 fn json_schema(generator: &mut schemars::SchemaGenerator) -> schemars::Schema {
58 schemars::json_schema!({
59 "type": "array",
60 "items": generator.subschema_for::<T>(),
61 })
62 }
63 }
64
65 impl<'a, K, V> JsonSchema for ZeroMap<'a, K, V>
66 where
67 K: ZeroMapKV<'a> + ?Sized + JsonSchema,
68 V: ZeroMapKV<'a> + ?Sized + JsonSchema,
69 {
70 fn inline_schema() -> bool {
71 true
72 }
73
74 fn schema_name() -> Cow<'static, str> {
75 format!("ZeroMap<{}, {}>", K::schema_name(), V::schema_name()).into()
76 }
77
78 fn json_schema(generator: &mut schemars::SchemaGenerator) -> schemars::Schema {
79 // forward to the BTreeMap impl, as the impl is quite complex
80 //
81 // as the impl for the BTreeMap requires its arguments to be Sized, i cannot forward
82 // K and V directly, but since schemars simply forwards &T impls to T, this is fine
83 <alloc::collections::BTreeMap<&K, &V> as JsonSchema>::json_schema(generator)
84 }
85 }
86
87 #[cfg(test)]
88 mod tests {
89 use crate::{VarZeroVec, ZeroMap, ZeroSlice, ZeroVec};
90
91 #[test]
92 #[cfg(feature = "schemars")]
93 fn schema_zerovec_u32() {
94 let generator = schemars::SchemaGenerator::default();
95 let schema = generator.into_root_schema_for::<ZeroVec<u32>>();
96
97 insta::assert_json_snapshot!(schema);
98 }
99
100 #[test]
101 #[cfg(feature = "schemars")]
102 fn schema_zerovec_char() {
103 let generator = schemars::SchemaGenerator::default();
104 let schema = generator.into_root_schema_for::<ZeroVec<char>>();
105
106 insta::assert_json_snapshot!(schema);
107 }
108
109 #[test]
110 #[cfg(feature = "schemars")]
111 fn schema_varzerovec_str() {
112 let generator = schemars::SchemaGenerator::default();
113 let schema = generator.into_root_schema_for::<VarZeroVec<str>>();
114
115 insta::assert_json_snapshot!(schema);
116 }
117
118 #[test]
119 #[cfg(feature = "schemars")]
120 fn schema_varzerovec_zeroslice() {
121 let generator = schemars::SchemaGenerator::default();
122 let schema = generator.into_root_schema_for::<VarZeroVec<ZeroSlice<u32>>>();
123
124 insta::assert_json_snapshot!(schema);
125 }
126
127 #[test]
128 fn schema_zeromap_u32_str() {
129 let generator = schemars::SchemaGenerator::default();
130 let schema = generator.into_root_schema_for::<ZeroMap<u32, str>>();
131
132 insta::assert_json_snapshot!(schema);
133 }
134 }