schemars/json_schema_impls/
chrono.rs

1use crate::gen::SchemaGenerator;
2use crate::schema::*;
3use crate::JsonSchema;
4use chrono::prelude::*;
5use serde_json::json;
6
7impl JsonSchema for Weekday {
8    no_ref_schema!();
9
10    fn schema_name() -> String {
11        "Weekday".to_owned()
12    }
13
14    fn json_schema(_: &mut SchemaGenerator) -> Schema {
15        SchemaObject {
16            instance_type: Some(InstanceType::String.into()),
17            enum_values: Some(vec![
18                json!("Mon"),
19                json!("Tue"),
20                json!("Wed"),
21                json!("Thu"),
22                json!("Fri"),
23                json!("Sat"),
24                json!("Sun"),
25            ]),
26            ..Default::default()
27        }
28        .into()
29    }
30}
31
32macro_rules! formatted_string_impl {
33    ($ty:ident, $format:literal) => {
34        formatted_string_impl!($ty, $format, JsonSchema for $ty);
35    };
36    ($ty:ident, $format:literal, $($desc:tt)+) => {
37        impl $($desc)+ {
38            no_ref_schema!();
39
40            fn schema_name() -> String {
41                stringify!($ty).to_owned()
42            }
43
44            fn json_schema(_: &mut SchemaGenerator) -> Schema {
45                SchemaObject {
46                    instance_type: Some(InstanceType::String.into()),
47                    format: Some($format.to_owned()),
48                    ..Default::default()
49                }
50                .into()
51            }
52        }
53    };
54}
55
56formatted_string_impl!(NaiveDate, "date");
57formatted_string_impl!(NaiveDateTime, "partial-date-time");
58formatted_string_impl!(NaiveTime, "partial-date-time");
59formatted_string_impl!(DateTime, "date-time", <Tz: TimeZone> JsonSchema for DateTime<Tz>);