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
use std::{
    collections::BTreeMap,
    net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV4, SocketAddrV6},
    path::{Path, PathBuf},
};

use openapiv3::{Schema, SchemaData, SchemaKind, StringType, Type, VariantOrUnknownOrEmpty};

use crate::ToSchema;

macro_rules! string_impl {
    ($ty:ty) => {
        string_impl!($ty, VariantOrUnknownOrEmpty::Empty);
    };
    ($ty:ty, $format:literal) => {
        string_impl!($ty, VariantOrUnknownOrEmpty::Unknown($format.to_string()));
    };
    ($ty:ty, $format:expr) => {
        impl ToSchema for $ty {
            fn schema(_: &mut BTreeMap<String, Schema>, _: &mut Vec<String>) -> Schema {
                let ty = StringType {
                    format: $format,
                    ..Default::default()
                };

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

string_impl!(str);
string_impl!(String);
string_impl!(Path);
string_impl!(PathBuf);
string_impl!(Ipv4Addr, "ipv4");
string_impl!(Ipv6Addr, "ipv6");
string_impl!(SocketAddrV4);
string_impl!(SocketAddrV6);

macro_rules! one_of_string_impl {
    ($ty:ty; [$($elem:ty),+ $(,)?]) => {
        impl ToSchema for $ty {
            fn schema(schemas: &mut BTreeMap<String, Schema>, schemas_in_progress: &mut Vec<String>) -> Schema {
                Schema {
                    schema_data: SchemaData {
                        title: Some(stringify!($ty).to_string()),
                        ..Default::default()
                    },
                    schema_kind: SchemaKind::OneOf {
                        one_of: [
                            $(
                                <$elem>::schema_ref(schemas, schemas_in_progress),
                            )+
                        ]
                        .to_vec(),
                    },
                }
            }
        }
    };
}

one_of_string_impl!(IpAddr; [Ipv4Addr, Ipv6Addr]);
one_of_string_impl!(SocketAddr; [SocketAddrV4, SocketAddrV6]);