predawn_schema/impls/
time.rs

1use std::{
2    borrow::Cow,
3    collections::BTreeMap,
4    time::{Duration, SystemTime},
5};
6
7use openapiv3::{ObjectType, Schema, SchemaData, SchemaKind, Type};
8
9use crate::ToSchema;
10
11impl ToSchema for SystemTime {
12    fn title() -> Cow<'static, str> {
13        "SystemTime".into()
14    }
15
16    fn schema(
17        schemas: &mut BTreeMap<String, Schema>,
18        schemas_in_progress: &mut Vec<String>,
19    ) -> Schema {
20        const SECS_SINCE_EPOCH: &str = "secs_since_epoch";
21        const NANOS_SINCE_EPOCH: &str = "nanos_since_epoch";
22
23        let mut ty = ObjectType::default();
24
25        ty.properties.insert(
26            SECS_SINCE_EPOCH.to_string(),
27            i64::schema_ref_box(schemas, schemas_in_progress),
28        );
29        ty.properties.insert(
30            NANOS_SINCE_EPOCH.to_string(),
31            u32::schema_ref_box(schemas, schemas_in_progress),
32        );
33
34        ty.required.push(SECS_SINCE_EPOCH.to_string());
35        ty.required.push(NANOS_SINCE_EPOCH.to_string());
36
37        Schema {
38            schema_data: SchemaData {
39                title: Some(Self::title().into()),
40                ..Default::default()
41            },
42            schema_kind: SchemaKind::Type(Type::Object(ty)),
43        }
44    }
45}
46
47impl ToSchema for Duration {
48    fn title() -> Cow<'static, str> {
49        "Duration".into()
50    }
51
52    fn schema(
53        schemas: &mut BTreeMap<String, Schema>,
54        schemas_in_progress: &mut Vec<String>,
55    ) -> Schema {
56        const SECS: &str = "secs";
57        const NANOS: &str = "nanos";
58
59        let mut ty = ObjectType::default();
60
61        ty.properties.insert(
62            SECS.to_string(),
63            u64::schema_ref_box(schemas, schemas_in_progress),
64        );
65        ty.properties.insert(
66            NANOS.to_string(),
67            u32::schema_ref_box(schemas, schemas_in_progress),
68        );
69
70        ty.required.push(SECS.to_string());
71        ty.required.push(NANOS.to_string());
72
73        Schema {
74            schema_data: SchemaData {
75                title: Some(Self::title().into()),
76                ..Default::default()
77            },
78            schema_kind: SchemaKind::Type(Type::Object(ty)),
79        }
80    }
81}