schemars/json_schema_impls/
sequences.rs

1use crate::SchemaGenerator;
2use crate::_alloc_prelude::*;
3use crate::{json_schema, JsonSchema, Schema};
4use alloc::borrow::Cow;
5
6macro_rules! seq_impl {
7    ($($desc:tt)+) => {
8        impl $($desc)+
9        where
10            T: JsonSchema,
11        {
12            inline_schema!();
13
14            fn schema_name() -> Cow<'static, str> {
15                format!("Array_of_{}", T::schema_name()).into()
16            }
17
18            fn schema_id() -> Cow<'static, str> {
19                format!("[{}]", T::schema_id()).into()
20            }
21
22            fn json_schema(generator: &mut SchemaGenerator) -> Schema {
23                json_schema!({
24                    "type": "array",
25                    "items": generator.subschema_for::<T>(),
26                })
27            }
28        }
29    };
30}
31
32macro_rules! set_impl {
33    ($($desc:tt)+) => {
34        impl $($desc)+
35        where
36            T: JsonSchema,
37        {
38            inline_schema!();
39
40            fn schema_name() -> Cow<'static, str> {
41                format!("Set_of_{}", T::schema_name()).into()
42            }
43
44            fn schema_id() -> Cow<'static, str> {
45                format!("Set<{}>", T::schema_id()).into()
46            }
47
48            fn json_schema(generator: &mut SchemaGenerator) -> Schema {
49                json_schema!({
50                    "type": "array",
51                    "uniqueItems": true,
52                    "items": generator.subschema_for::<T>(),
53                })
54            }
55        }
56    };
57}
58
59seq_impl!(<T> JsonSchema for alloc::collections::BinaryHeap<T>);
60seq_impl!(<T> JsonSchema for alloc::collections::LinkedList<T>);
61seq_impl!(<T> JsonSchema for [T]);
62seq_impl!(<T> JsonSchema for alloc::vec::Vec<T>);
63seq_impl!(<T> JsonSchema for alloc::collections::VecDeque<T>);
64
65set_impl!(<T> JsonSchema for alloc::collections::BTreeSet<T>);
66
67#[cfg(feature = "std")]
68set_impl!(<T, H> JsonSchema for std::collections::HashSet<T, H>);