schematic_types/
tuples.rs

1use crate::*;
2use std::fmt;
3
4#[derive(Clone, Debug, Default, PartialEq)]
5#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
6pub struct TupleType {
7    pub items_types: Vec<Box<Schema>>,
8}
9
10impl TupleType {
11    /// Create a tuple schema with the provided item types.
12    pub fn new<I, V>(items_types: I) -> Self
13    where
14        I: IntoIterator<Item = V>,
15        V: Into<Schema>,
16    {
17        TupleType {
18            items_types: items_types
19                .into_iter()
20                .map(|inner| Box::new(inner.into()))
21                .collect(),
22        }
23    }
24}
25
26impl fmt::Display for TupleType {
27    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
28        write!(
29            f,
30            "({})",
31            self.items_types
32                .iter()
33                .map(|item| item.to_string())
34                .collect::<Vec<_>>()
35                .join(", ")
36        )
37    }
38}
39
40macro_rules! impl_tuple {
41    ($($arg: ident),*) => {
42        impl<$($arg: Schematic),*> Schematic for ($($arg,)*) {
43            fn build_schema(mut schema: SchemaBuilder) -> Schema {
44                schema.tuple(TupleType::new([
45                    $(schema.infer::<$arg>(),)*
46                ]))
47            }
48        }
49    };
50}
51
52impl_tuple!(T0);
53impl_tuple!(T0, T1);
54impl_tuple!(T0, T1, T2);
55impl_tuple!(T0, T1, T2, T3);
56impl_tuple!(T0, T1, T2, T3, T4);
57impl_tuple!(T0, T1, T2, T3, T4, T5);
58impl_tuple!(T0, T1, T2, T3, T4, T5, T6);
59impl_tuple!(T0, T1, T2, T3, T4, T5, T6, T7);
60impl_tuple!(T0, T1, T2, T3, T4, T5, T6, T7, T8);
61impl_tuple!(T0, T1, T2, T3, T4, T5, T6, T7, T8, T9);
62impl_tuple!(T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10);
63impl_tuple!(T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11);
64impl_tuple!(T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12);