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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#![allow(deprecated, unused_imports, unused_macros)]

use crate::*;

macro_rules! impl_unknown {
    ($type:ty) => {
        impl Schematic for $type {}
    };
}

macro_rules! impl_set {
    ($type:ty) => {
        impl<T: Schematic, S> Schematic for $type {
            fn build_schema(mut schema: SchemaBuilder) -> Schema {
                schema.array(ArrayType::new(schema.infer::<T>()))
            }
        }
    };
}

macro_rules! impl_map {
    ($type:ty) => {
        impl<K: Schematic, V: Schematic, S> Schematic for $type {
            fn build_schema(mut schema: SchemaBuilder) -> Schema {
                schema.object(ObjectType::new(schema.infer::<K>(), schema.infer::<V>()))
            }
        }
    };
}

macro_rules! impl_string {
    ($type:ty) => {
        impl Schematic for $type {
            fn build_schema(mut schema: SchemaBuilder) -> Schema {
                schema.string_default()
            }
        }
    };
}

macro_rules! impl_string_format {
    ($type:ty, $format:expr) => {
        impl Schematic for $type {
            fn build_schema(mut schema: SchemaBuilder) -> Schema {
                schema.string(StringType {
                    format: Some($format.into()),
                    ..StringType::default()
                })
            }
        }
    };
}

#[cfg(feature = "chrono")]
mod chrono_feature {
    use super::*;

    macro_rules! impl_with_tz {
        ($type:path, $format:expr) => {
            impl<Tz: chrono::TimeZone> Schematic for $type {
                fn build_schema(mut schema: SchemaBuilder) -> Schema {
                    schema.string(StringType {
                        format: Some($format.into()),
                        ..StringType::default()
                    })
                }
            }
        };
    }

    impl_with_tz!(chrono::Date<Tz>, "date");
    impl_with_tz!(chrono::DateTime<Tz>, "date-time");
    impl_string_format!(chrono::Duration, "duration");
    impl_string_format!(chrono::Days, "duration");
    impl_string_format!(chrono::Months, "duration");
    impl_string_format!(chrono::IsoWeek, "date");
    impl_string_format!(chrono::NaiveWeek, "date");
    impl_string_format!(chrono::NaiveDate, "date");
    impl_string_format!(chrono::NaiveDateTime, "date-time");
    impl_string_format!(chrono::NaiveTime, "time");
}

#[cfg(feature = "indexmap")]
mod indexmap_feature {
    use super::*;

    impl_map!(indexmap::IndexMap<K, V, S>);
    impl_set!(indexmap::IndexSet<T, S>);
}

#[cfg(feature = "regex")]
mod regex_feature {
    use super::*;

    impl_string_format!(regex::Regex, "regex");
}

#[cfg(feature = "relative_path")]
mod relative_path_feature {
    use super::*;

    impl_string_format!(&relative_path::RelativePath, "path");
    impl_string_format!(relative_path::RelativePath, "path");
    impl_string_format!(relative_path::RelativePathBuf, "path");
}

#[cfg(feature = "rust_decimal")]
mod rust_decimal_feature {
    use super::*;

    impl_string_format!(rust_decimal::Decimal, "decimal");
}

#[cfg(feature = "semver")]
mod semver_feature {
    use super::*;

    impl_string!(semver::Version);
    impl_string!(semver::VersionReq);
}

#[cfg(feature = "serde_json")]
mod serde_json_feature {
    use super::*;

    impl_unknown!(serde_json::Value);

    // This isn't accurate since we can't access the `N` enum
    impl Schematic for serde_json::Number {
        fn build_schema(mut schema: SchemaBuilder) -> Schema {
            schema.integer(IntegerType::new_kind(IntegerKind::I64))
        }
    }

    impl<K: Schematic, V: Schematic> Schematic for serde_json::Map<K, V> {
        fn build_schema(mut schema: SchemaBuilder) -> Schema {
            schema.object(ObjectType::new(schema.infer::<K>(), schema.infer::<V>()))
        }
    }
}

#[cfg(feature = "serde_toml")]
mod serde_toml_feature {
    use super::*;

    impl_unknown!(toml::Value);

    impl<K: Schematic, V: Schematic> Schematic for toml::map::Map<K, V> {
        fn build_schema(mut schema: SchemaBuilder) -> Schema {
            schema.object(ObjectType::new(schema.infer::<K>(), schema.infer::<V>()))
        }
    }
}

#[cfg(feature = "serde_yaml")]
mod serde_yaml_feature {
    use super::*;

    impl_unknown!(serde_yaml::Value);

    // This isn't accurate since we can't access the `N` enum
    impl Schematic for serde_yaml::Number {
        fn build_schema(mut schema: SchemaBuilder) -> Schema {
            schema.integer(IntegerType::new_kind(IntegerKind::I64))
        }
    }

    impl Schematic for serde_yaml::Mapping {
        fn build_schema(mut schema: SchemaBuilder) -> Schema {
            schema.object(ObjectType::new(
                schema.infer::<serde_yaml::Value>(),
                schema.infer::<serde_yaml::Value>(),
            ))
        }
    }
}

#[cfg(feature = "url")]
mod url_feature {
    use super::*;

    impl_string_format!(url::Url, "uri");
}