1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
use std::{
    collections::BTreeMap,
    time::{Duration, SystemTime},
};

use openapiv3::{ObjectType, Schema, SchemaData, SchemaKind, Type};

use crate::ToSchema;

impl ToSchema for SystemTime {
    fn schema(
        schemas: &mut BTreeMap<String, Schema>,
        schemas_in_progress: &mut Vec<String>,
    ) -> Schema {
        const SECS_SINCE_EPOCH: &str = "secs_since_epoch";
        const NANOS_SINCE_EPOCH: &str = "nanos_since_epoch";

        let mut ty = ObjectType::default();

        ty.properties.insert(
            SECS_SINCE_EPOCH.to_string(),
            i64::schema_ref_box(schemas, schemas_in_progress),
        );
        ty.properties.insert(
            NANOS_SINCE_EPOCH.to_string(),
            u32::schema_ref_box(schemas, schemas_in_progress),
        );

        ty.required.push(SECS_SINCE_EPOCH.to_string());
        ty.required.push(NANOS_SINCE_EPOCH.to_string());

        Schema {
            schema_data: SchemaData {
                title: Some("SystemTime".to_string()),
                ..Default::default()
            },
            schema_kind: SchemaKind::Type(Type::Object(ty)),
        }
    }
}

impl ToSchema for Duration {
    fn schema(
        schemas: &mut BTreeMap<String, Schema>,
        schemas_in_progress: &mut Vec<String>,
    ) -> Schema {
        const SECS: &str = "secs";
        const NANOS: &str = "nanos";

        let mut ty = ObjectType::default();

        ty.properties.insert(
            SECS.to_string(),
            u64::schema_ref_box(schemas, schemas_in_progress),
        );
        ty.properties.insert(
            NANOS.to_string(),
            u32::schema_ref_box(schemas, schemas_in_progress),
        );

        ty.required.push(SECS.to_string());
        ty.required.push(NANOS.to_string());

        Schema {
            schema_data: SchemaData {
                title: Some("Duration".to_string()),
                ..Default::default()
            },
            schema_kind: SchemaKind::Type(Type::Object(ty)),
        }
    }
}