schemars/json_schema_impls/
chrono04.rs

1use crate::SchemaGenerator;
2use crate::{json_schema, JsonSchema, Schema};
3use alloc::borrow::Cow;
4use chrono04::{prelude::*, TimeDelta};
5
6impl JsonSchema for Weekday {
7    inline_schema!();
8
9    fn schema_name() -> Cow<'static, str> {
10        "Weekday".into()
11    }
12
13    fn schema_id() -> Cow<'static, str> {
14        "chrono::Weekday".into()
15    }
16
17    fn json_schema(_: &mut SchemaGenerator) -> Schema {
18        json_schema!({
19            "type": "string",
20            "enum": [
21                "Mon",
22                "Tue",
23                "Wed",
24                "Thu",
25                "Fri",
26                "Sat",
27                "Sun",
28            ]
29        })
30    }
31}
32
33impl JsonSchema for TimeDelta {
34    inline_schema!();
35
36    fn schema_name() -> Cow<'static, str> {
37        "TimeDelta".into()
38    }
39
40    fn schema_id() -> Cow<'static, str> {
41        "chrono::TimeDelta".into()
42    }
43
44    fn json_schema(_: &mut SchemaGenerator) -> Schema {
45        json_schema!({
46          "type": "array",
47          "prefixItems": [
48            {
49              "type": "integer",
50              "format": "int64"
51            },
52            {
53              "type": "integer",
54              "minimum": 0,
55              "exclusiveMaximum": 1_000_000_000
56            }
57          ],
58          "minItems": 2,
59          "maxItems": 2
60        })
61    }
62}
63
64macro_rules! formatted_string_impl {
65    ($ty:ident, $format:literal) => {
66        formatted_string_impl!($ty, $format, JsonSchema for $ty);
67    };
68    ($ty:ident, $format:literal, $($desc:tt)+) => {
69        impl $($desc)+ {
70            inline_schema!();
71
72            fn schema_name() -> Cow<'static, str> {
73                stringify!($ty).into()
74            }
75
76            fn schema_id() -> Cow<'static, str>  {
77                stringify!(chrono::$ty).into()
78            }
79
80            fn json_schema(_: &mut SchemaGenerator) -> Schema {
81                json_schema!({
82                    "type": "string",
83                    "format": $format
84                })
85            }
86        }
87    };
88}
89
90formatted_string_impl!(NaiveDate, "date");
91formatted_string_impl!(NaiveDateTime, "partial-date-time");
92formatted_string_impl!(NaiveTime, "partial-time");
93formatted_string_impl!(DateTime, "date-time", <Tz: TimeZone> JsonSchema for DateTime<Tz>);