1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
use crate::*;
use std::collections::{BTreeSet, HashSet};

#[derive(Clone, Debug, Default, PartialEq)]
pub struct ArrayType {
    pub contains: Option<bool>,
    pub items_type: Box<Schema>,
    pub max_contains: Option<usize>,
    pub max_length: Option<usize>,
    pub min_contains: Option<usize>,
    pub min_length: Option<usize>,
    pub unique: Option<bool>,
}

impl ArrayType {
    /// Create an array schema with the provided item types.
    pub fn new(items_type: impl Into<Schema>) -> Self {
        ArrayType {
            items_type: Box::new(items_type.into()),
            ..ArrayType::default()
        }
    }
}

impl<T: Schematic> Schematic for Vec<T> {
    fn build_schema(mut schema: SchemaBuilder) -> Schema {
        schema.array(ArrayType::new(schema.infer::<T>()))
    }
}

impl<T: Schematic> Schematic for &[T] {
    fn build_schema(mut schema: SchemaBuilder) -> Schema {
        schema.array(ArrayType::new(schema.infer::<T>()))
    }
}

impl<T: Schematic, const N: usize> Schematic for [T; N] {
    fn build_schema(mut schema: SchemaBuilder) -> Schema {
        schema.array(ArrayType {
            items_type: Box::new(schema.infer::<T>()),
            max_length: Some(N),
            min_length: Some(N),
            ..ArrayType::default()
        })
    }
}

impl<T: Schematic, S> Schematic for HashSet<T, S> {
    fn build_schema(mut schema: SchemaBuilder) -> Schema {
        schema.array(ArrayType {
            items_type: Box::new(schema.infer::<T>()),
            unique: Some(true),
            ..ArrayType::default()
        })
    }
}

impl<T: Schematic> Schematic for BTreeSet<T> {
    fn build_schema(mut schema: SchemaBuilder) -> Schema {
        schema.array(ArrayType {
            items_type: Box::new(schema.infer::<T>()),
            unique: Some(true),
            ..ArrayType::default()
        })
    }
}