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
73
74
75
76
77
use std::{
    collections::BTreeMap,
    num::{
        NonZeroI128, NonZeroI16, NonZeroI32, NonZeroI64, NonZeroI8, NonZeroIsize, NonZeroU128,
        NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU8, NonZeroUsize,
    },
};

use openapiv3::{
    AnySchema, IntegerType, ReferenceOr, Schema, SchemaData, SchemaKind, Type,
    VariantOrUnknownOrEmpty,
};

use crate::ToSchema;

macro_rules! nonzero_signed_impl {
    ($ty:ty, $format:literal) => {
        impl ToSchema for $ty {
            fn schema(_: &mut BTreeMap<String, Schema>, _: &mut Vec<String>) -> Schema {
                Schema {
                    schema_data: SchemaData {
                        title: Some(stringify!($ty).to_string()),
                        ..Default::default()
                    },
                    schema_kind: SchemaKind::Any(AnySchema {
                        typ: Some("integer".to_string()),
                        format: Some($format.to_string()),
                        not: Some(Box::new(ReferenceOr::Item(Schema {
                            schema_data: SchemaData::default(),
                            schema_kind: SchemaKind::Type(Type::Integer(IntegerType {
                                format: VariantOrUnknownOrEmpty::Unknown($format.to_string()),
                                maximum: Some(0),
                                minimum: Some(0),
                                ..Default::default()
                            })),
                        }))),
                        ..Default::default()
                    }),
                }
            }
        }
    };
}

nonzero_signed_impl!(NonZeroI8, "int8");
nonzero_signed_impl!(NonZeroI16, "int16");
nonzero_signed_impl!(NonZeroI32, "int32");
nonzero_signed_impl!(NonZeroI64, "int64");
nonzero_signed_impl!(NonZeroI128, "int128");
nonzero_signed_impl!(NonZeroIsize, "int");

macro_rules! nonzero_unsigned_impl {
    ($ty:ty, $format:literal) => {
        impl ToSchema for $ty {
            fn schema(_: &mut BTreeMap<String, Schema>, _: &mut Vec<String>) -> Schema {
                Schema {
                    schema_data: SchemaData {
                        title: Some(stringify!($ty).to_string()),
                        ..Default::default()
                    },
                    schema_kind: SchemaKind::Type(Type::Integer(IntegerType {
                        format: VariantOrUnknownOrEmpty::Unknown($format.to_string()),
                        minimum: Some(1),
                        ..Default::default()
                    })),
                }
            }
        }
    };
}

nonzero_unsigned_impl!(NonZeroU8, "uint8");
nonzero_unsigned_impl!(NonZeroU16, "uint16");
nonzero_unsigned_impl!(NonZeroU32, "uint32");
nonzero_unsigned_impl!(NonZeroU64, "uint64");
nonzero_unsigned_impl!(NonZeroU128, "uint128");
nonzero_unsigned_impl!(NonZeroUsize, "uint");