Skip to main content

schematic_types/
externals.rs

1#![allow(deprecated, unused_imports, unused_macros)]
2
3use crate::*;
4
5macro_rules! impl_unknown {
6    ($type:ty) => {
7        impl Schematic for $type {}
8    };
9}
10
11macro_rules! impl_set {
12    ($type:ty) => {
13        impl<T: Schematic, S> Schematic for $type {
14            fn build_schema(mut schema: SchemaBuilder) -> Schema {
15                schema.array(ArrayType::new(schema.infer::<T>()))
16            }
17        }
18    };
19}
20
21macro_rules! impl_map {
22    ($type:ty) => {
23        impl<K: Schematic, V: Schematic, S> Schematic for $type {
24            fn build_schema(mut schema: SchemaBuilder) -> Schema {
25                schema.object(ObjectType::new(schema.infer::<K>(), schema.infer::<V>()))
26            }
27        }
28    };
29}
30
31macro_rules! impl_string {
32    ($type:ty) => {
33        impl Schematic for $type {
34            fn build_schema(mut schema: SchemaBuilder) -> Schema {
35                schema.string_default()
36            }
37        }
38    };
39}
40
41macro_rules! impl_string_format {
42    ($type:ty, $format:expr_2021) => {
43        impl Schematic for $type {
44            fn build_schema(mut schema: SchemaBuilder) -> Schema {
45                schema.string(StringType {
46                    format: Some($format.into()),
47                    ..StringType::default()
48                })
49            }
50        }
51    };
52}
53
54#[cfg(feature = "chrono")]
55mod chrono_feature {
56    use super::*;
57
58    macro_rules! impl_with_tz {
59        ($type:path, $format:expr_2021) => {
60            impl<Tz: chrono::TimeZone> Schematic for $type {
61                fn build_schema(mut schema: SchemaBuilder) -> Schema {
62                    schema.string(StringType {
63                        format: Some($format.into()),
64                        ..StringType::default()
65                    })
66                }
67            }
68        };
69    }
70
71    impl_with_tz!(chrono::Date<Tz>, "date");
72    impl_with_tz!(chrono::DateTime<Tz>, "date-time");
73    impl_string_format!(chrono::Duration, "duration");
74    impl_string_format!(chrono::Days, "duration");
75    impl_string_format!(chrono::Months, "duration");
76    impl_string_format!(chrono::IsoWeek, "date");
77    impl_string_format!(chrono::NaiveWeek, "date");
78    impl_string_format!(chrono::NaiveDate, "date");
79    impl_string_format!(chrono::NaiveDateTime, "date-time");
80    impl_string_format!(chrono::NaiveTime, "time");
81}
82
83#[cfg(feature = "indexmap")]
84mod indexmap_feature {
85    use super::*;
86
87    impl_map!(indexmap::IndexMap<K, V, S>);
88    impl_set!(indexmap::IndexSet<T, S>);
89}
90
91#[cfg(feature = "regex")]
92mod regex_feature {
93    use super::*;
94
95    impl_string_format!(regex::Regex, "regex");
96}
97
98#[cfg(feature = "relative_path")]
99mod relative_path_feature {
100    use super::*;
101
102    impl_string_format!(&relative_path::RelativePath, "path");
103    impl_string_format!(relative_path::RelativePath, "path");
104    impl_string_format!(relative_path::RelativePathBuf, "path");
105}
106
107#[cfg(feature = "rust_decimal")]
108mod rust_decimal_feature {
109    use super::*;
110
111    impl_string_format!(rust_decimal::Decimal, "decimal");
112}
113
114#[cfg(feature = "semver")]
115mod semver_feature {
116    use super::*;
117
118    impl_string!(semver::Version);
119    impl_string!(semver::VersionReq);
120}
121
122#[cfg(feature = "serde_json")]
123mod serde_json_feature {
124    use super::*;
125
126    impl_unknown!(serde_json::Value);
127
128    // This isn't accurate since we can't access the `N` enum
129    impl Schematic for serde_json::Number {
130        fn build_schema(mut schema: SchemaBuilder) -> Schema {
131            schema.integer(IntegerType::new_kind(IntegerKind::I64))
132        }
133    }
134
135    impl<K: Schematic, V: Schematic> Schematic for serde_json::Map<K, V> {
136        fn build_schema(mut schema: SchemaBuilder) -> Schema {
137            schema.object(ObjectType::new(schema.infer::<K>(), schema.infer::<V>()))
138        }
139    }
140}
141
142#[cfg(feature = "serde_rpkl")]
143mod serde_rpkl_feature {
144    use super::*;
145
146    impl_unknown!(rpkl::Value);
147}
148
149#[cfg(feature = "serde_toml")]
150mod serde_toml_feature {
151    use super::*;
152
153    impl_unknown!(toml::Value);
154
155    impl<K: Schematic, V: Schematic> Schematic for toml::map::Map<K, V> {
156        fn build_schema(mut schema: SchemaBuilder) -> Schema {
157            schema.object(ObjectType::new(schema.infer::<K>(), schema.infer::<V>()))
158        }
159    }
160}
161
162#[cfg(feature = "serde_yaml")]
163mod serde_yaml_feature {
164    use super::*;
165
166    impl_unknown!(serde_yaml::Value);
167
168    // This isn't accurate since we can't access the `N` enum
169    impl Schematic for serde_yaml::Number {
170        fn build_schema(mut schema: SchemaBuilder) -> Schema {
171            schema.integer(IntegerType::new_kind(IntegerKind::I64))
172        }
173    }
174
175    impl Schematic for serde_yaml::Mapping {
176        fn build_schema(mut schema: SchemaBuilder) -> Schema {
177            schema.object(ObjectType::new(
178                schema.infer::<serde_yaml::Value>(),
179                schema.infer::<serde_yaml::Value>(),
180            ))
181        }
182    }
183}
184
185#[cfg(feature = "serde_yaml_norway")]
186mod serde_yaml_normay_feature {
187    use super::*;
188
189    impl_unknown!(serde_norway::Value);
190
191    // This isn't accurate since we can't access the `N` enum
192    impl Schematic for serde_norway::Number {
193        fn build_schema(mut schema: SchemaBuilder) -> Schema {
194            schema.integer(IntegerType::new_kind(IntegerKind::I64))
195        }
196    }
197
198    impl Schematic for serde_norway::Mapping {
199        fn build_schema(mut schema: SchemaBuilder) -> Schema {
200            schema.object(ObjectType::new(
201                schema.infer::<serde_norway::Value>(),
202                schema.infer::<serde_norway::Value>(),
203            ))
204        }
205    }
206}
207
208#[cfg(feature = "url")]
209mod url_feature {
210    use super::*;
211
212    impl_string_format!(url::Url, "uri");
213}
214
215#[cfg(feature = "uuid")]
216mod uuid_feature {
217    use super::*;
218
219    impl_string_format!(uuid::Uuid, "uuid");
220}