schemars/json_schema_impls/
tuple.rs

1use crate::SchemaGenerator;
2use crate::_alloc_prelude::*;
3use crate::{json_schema, JsonSchema, Schema};
4use alloc::borrow::Cow;
5
6macro_rules! tuple_impls {
7    ($($len:expr => ($($name:ident)+))+) => {
8        $(
9            impl<$($name: JsonSchema),+> JsonSchema for ($($name,)+) {
10                inline_schema!();
11
12                fn schema_name() -> Cow<'static, str> {
13                    let mut name = "Tuple_of_".to_owned();
14                    name.push_str(&[$($name::schema_name()),+].join("_and_"));
15                    name.into()
16                }
17
18                fn schema_id() -> Cow<'static, str> {
19                    let mut id = "(".to_owned();
20                    id.push_str(&[$($name::schema_id()),+].join(","));
21                    id.push(')');
22
23                    id.into()
24                }
25
26                fn json_schema(generator: &mut SchemaGenerator) -> Schema {
27                    json_schema!({
28                        "type": "array",
29                        "prefixItems": [
30                            $(generator.subschema_for::<$name>()),+
31                        ],
32                        "minItems": $len,
33                        "maxItems": $len,
34                    })
35                }
36            }
37        )+
38    }
39}
40
41tuple_impls! {
42    1 => (T0)
43    2 => (T0 T1)
44    3 => (T0 T1 T2)
45    4 => (T0 T1 T2 T3)
46    5 => (T0 T1 T2 T3 T4)
47    6 => (T0 T1 T2 T3 T4 T5)
48    7 => (T0 T1 T2 T3 T4 T5 T6)
49    8 => (T0 T1 T2 T3 T4 T5 T6 T7)
50    9 => (T0 T1 T2 T3 T4 T5 T6 T7 T8)
51    10 => (T0 T1 T2 T3 T4 T5 T6 T7 T8 T9)
52    11 => (T0 T1 T2 T3 T4 T5 T6 T7 T8 T9 T10)
53    12 => (T0 T1 T2 T3 T4 T5 T6 T7 T8 T9 T10 T11)
54    13 => (T0 T1 T2 T3 T4 T5 T6 T7 T8 T9 T10 T11 T12)
55    14 => (T0 T1 T2 T3 T4 T5 T6 T7 T8 T9 T10 T11 T12 T13)
56    15 => (T0 T1 T2 T3 T4 T5 T6 T7 T8 T9 T10 T11 T12 T13 T14)
57    16 => (T0 T1 T2 T3 T4 T5 T6 T7 T8 T9 T10 T11 T12 T13 T14 T15)
58}