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
use std::time::{Duration, SystemTime};

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

use crate::ToSchema;

impl ToSchema for SystemTime {
    fn schema() -> Schema {
        let mut ty = ObjectType::default();

        ty.properties.insert(
            "seconds_since_epoch".to_string(),
            ReferenceOr::Item(Box::new(i64::schema())),
        );
        ty.properties.insert(
            "nanoseconds_since_epoch".to_string(),
            ReferenceOr::Item(Box::new(u32::schema())),
        );

        ty.required.push("seconds_since_epoch".to_string());
        ty.required.push("nanoseconds_since_epoch".to_string());

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

impl ToSchema for Duration {
    fn schema() -> Schema {
        let mut ty = ObjectType::default();

        ty.properties.insert(
            "seconds".to_string(),
            ReferenceOr::Item(Box::new(u64::schema())),
        );
        ty.properties.insert(
            "nanoseconds".to_string(),
            ReferenceOr::Item(Box::new(u32::schema())),
        );

        ty.required.push("seconds".to_string());
        ty.required.push("nanoseconds".to_string());

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