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