schemars/json_schema_impls/
primitives.rs1use crate::SchemaGenerator;
2use crate::_alloc_prelude::*;
3use crate::{json_schema, JsonSchema, Schema};
4use alloc::borrow::Cow;
5
6macro_rules! simple_impl {
7 ($type:ty => $instance_type:literal) => {
8 impl JsonSchema for $type {
9 inline_schema!();
10
11 fn schema_name() -> Cow<'static, str> {
12 $instance_type.into()
13 }
14
15 fn json_schema(_: &mut SchemaGenerator) -> Schema {
16 json_schema!({
17 "type": $instance_type
18 })
19 }
20 }
21 };
22 ($type:ty => $instance_type:literal, $format:literal) => {
23 impl JsonSchema for $type {
24 inline_schema!();
25
26 fn schema_name() -> Cow<'static, str> {
27 $format.into()
28 }
29
30 fn json_schema(_: &mut SchemaGenerator) -> Schema {
31 json_schema!({
32 "type": $instance_type,
33 "format": $format
34 })
35 }
36 }
37 };
38}
39
40macro_rules! ranged_impl {
41 ($type:ty => $instance_type:literal, $format:literal) => {
42 impl JsonSchema for $type {
43 inline_schema!();
44
45 fn schema_name() -> Cow<'static, str> {
46 $format.into()
47 }
48
49 fn json_schema(_: &mut SchemaGenerator) -> Schema {
50 json_schema!({
51 "type": $instance_type,
52 "format": $format,
53 "minimum": <$type>::MIN,
54 "maximum": <$type>::MAX
55 })
56 }
57 }
58 };
59}
60
61simple_impl!(str => "string");
62simple_impl!(String => "string");
63simple_impl!(bool => "boolean");
64simple_impl!(f32 => "number", "float");
65simple_impl!(f64 => "number", "double");
66ranged_impl!(i8 => "integer", "int8");
67ranged_impl!(i16 => "integer", "int16");
68simple_impl!(i32 => "integer", "int32");
69simple_impl!(i64 => "integer", "int64");
70simple_impl!(i128 => "integer", "int128");
71simple_impl!(isize => "integer", "int");
72simple_impl!(() => "null");
73
74#[cfg(feature = "std")]
75mod std_types {
76 use super::*;
77 use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV4, SocketAddrV6};
78 use std::path::{Path, PathBuf};
79
80 simple_impl!(Path => "string");
81 simple_impl!(PathBuf => "string");
82
83 simple_impl!(Ipv4Addr => "string", "ipv4");
84 simple_impl!(Ipv6Addr => "string", "ipv6");
85 simple_impl!(IpAddr => "string", "ip");
86
87 simple_impl!(SocketAddr => "string");
88 simple_impl!(SocketAddrV4 => "string");
89 simple_impl!(SocketAddrV6 => "string");
90}
91
92macro_rules! unsigned_impl {
93 ($type:ty => $instance_type:literal, $format:literal) => {
94 impl JsonSchema for $type {
95 inline_schema!();
96
97 fn schema_name() -> Cow<'static, str> {
98 $format.into()
99 }
100
101 fn json_schema(_: &mut SchemaGenerator) -> Schema {
102 json_schema!({
103 "type": $instance_type,
104 "format": $format,
105 "minimum": 0
106 })
107 }
108 }
109 };
110}
111
112ranged_impl!(u8 => "integer", "uint8");
113ranged_impl!(u16 => "integer", "uint16");
114unsigned_impl!(u32 => "integer", "uint32");
115unsigned_impl!(u64 => "integer", "uint64");
116unsigned_impl!(u128 => "integer", "uint128");
117unsigned_impl!(usize => "integer", "uint");
118
119impl JsonSchema for char {
120 inline_schema!();
121
122 fn schema_name() -> Cow<'static, str> {
123 "Character".into()
124 }
125
126 fn schema_id() -> Cow<'static, str> {
127 "char".into()
128 }
129
130 fn json_schema(_: &mut SchemaGenerator) -> Schema {
131 json_schema!({
132 "type": "string",
133 "minLength": 1,
134 "maxLength": 1,
135 })
136 }
137}