| 1 | use crate::{json_schema, JsonSchema, Schema, SchemaGenerator}; |
| 2 | use alloc::borrow::Cow; |
| 3 | use jiff02::civil::{Date, DateTime, Time}; |
| 4 | use jiff02::{SignedDuration, Span, Timestamp, Zoned}; |
| 5 | |
| 6 | macro_rules! formatted_string_impl { |
| 7 | ($ty:ident, $format:literal) => { |
| 8 | formatted_string_impl!($ty, $format, JsonSchema for $ty); |
| 9 | }; |
| 10 | ($ty:ident, $format:literal, $($desc:tt)+) => { |
| 11 | impl $($desc)+ { |
| 12 | inline_schema!(); |
| 13 | |
| 14 | fn schema_name() -> Cow<'static, str> { |
| 15 | stringify!($ty).into() |
| 16 | } |
| 17 | |
| 18 | fn schema_id() -> Cow<'static, str> { |
| 19 | stringify!(jiff::$ty).into() |
| 20 | } |
| 21 | |
| 22 | fn json_schema(_: &mut SchemaGenerator) -> Schema { |
| 23 | json_schema!({ |
| 24 | "type": "string", |
| 25 | "format": $format |
| 26 | }) |
| 27 | } |
| 28 | } |
| 29 | }; |
| 30 | } |
| 31 | |
| 32 | formatted_string_impl!(SignedDuration, "duration"); |
| 33 | formatted_string_impl!(Span, "duration"); |
| 34 | formatted_string_impl!(Timestamp, "date-time"); |
| 35 | formatted_string_impl!(Zoned, "zoned-date-time"); |
| 36 | formatted_string_impl!(Date, "date"); |
| 37 | formatted_string_impl!(Time, "partial-time"); |
| 38 | formatted_string_impl!(DateTime, "partial-date-time"); |