schemars/json_schema_impls/
primitives.rs

1use crate::gen::SchemaGenerator;
2use crate::schema::*;
3use crate::JsonSchema;
4use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV4, SocketAddrV6};
5use std::path::{Path, PathBuf};
6
7macro_rules! simple_impl {
8    ($type:ty => $instance_type:ident) => {
9        simple_impl!($type => $instance_type, stringify!($instance_type), None);
10    };
11    ($type:ty => $instance_type:ident, $format:literal) => {
12        simple_impl!($type => $instance_type, $format, Some($format.to_owned()));
13    };
14    ($type:ty => $instance_type:ident, $name:expr, $format:expr) => {
15        impl JsonSchema for $type {
16            no_ref_schema!();
17
18            fn schema_name() -> String {
19                $name.to_owned()
20            }
21
22            fn json_schema(_: &mut SchemaGenerator) -> Schema {
23                SchemaObject {
24                    instance_type: Some(InstanceType::$instance_type.into()),
25                    format: $format,
26                    ..Default::default()
27                }
28                .into()
29            }
30        }
31    };
32}
33
34simple_impl!(str => String);
35simple_impl!(String => String);
36simple_impl!(bool => Boolean);
37simple_impl!(f32 => Number, "float");
38simple_impl!(f64 => Number, "double");
39simple_impl!(i8 => Integer, "int8");
40simple_impl!(i16 => Integer, "int16");
41simple_impl!(i32 => Integer, "int32");
42simple_impl!(i64 => Integer, "int64");
43simple_impl!(i128 => Integer, "int128");
44simple_impl!(isize => Integer, "int");
45simple_impl!(() => Null);
46
47simple_impl!(Path => String);
48simple_impl!(PathBuf => String);
49
50simple_impl!(Ipv4Addr => String, "ipv4");
51simple_impl!(Ipv6Addr => String, "ipv6");
52simple_impl!(IpAddr => String, "ip");
53
54simple_impl!(SocketAddr => String);
55simple_impl!(SocketAddrV4 => String);
56simple_impl!(SocketAddrV6 => String);
57
58macro_rules! unsigned_impl {
59    ($type:ty => $instance_type:ident, $format:expr) => {
60        impl JsonSchema for $type {
61            no_ref_schema!();
62
63            fn schema_name() -> String {
64                $format.to_owned()
65            }
66
67            fn json_schema(_: &mut SchemaGenerator) -> Schema {
68                let mut schema = SchemaObject {
69                    instance_type: Some(InstanceType::$instance_type.into()),
70                    format: Some($format.to_owned()),
71                    ..Default::default()
72                };
73                schema.number().minimum = Some(0.0);
74                schema.into()
75            }
76        }
77    };
78}
79
80unsigned_impl!(u8 => Integer, "uint8");
81unsigned_impl!(u16 => Integer, "uint16");
82unsigned_impl!(u32 => Integer, "uint32");
83unsigned_impl!(u64 => Integer, "uint64");
84unsigned_impl!(u128 => Integer, "uint128");
85unsigned_impl!(usize => Integer, "uint");
86
87impl JsonSchema for char {
88    no_ref_schema!();
89
90    fn schema_name() -> String {
91        "Character".to_owned()
92    }
93
94    fn json_schema(_: &mut SchemaGenerator) -> Schema {
95        SchemaObject {
96            instance_type: Some(InstanceType::String.into()),
97            string: Some(Box::new(StringValidation {
98                min_length: Some(1),
99                max_length: Some(1),
100                ..Default::default()
101            })),
102            ..Default::default()
103        }
104        .into()
105    }
106}