| 1 | use crate::SchemaGenerator; |
| 2 | use crate::_alloc_prelude::*; |
| 3 | use crate::{json_schema, JsonSchema, Schema}; |
| 4 | use alloc::borrow::Cow; |
| 5 | |
| 6 | macro_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 | |
| 40 | macro_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 | |
| 61 | simple_impl!(str => "string"); |
| 62 | simple_impl!(String => "string"); |
| 63 | simple_impl!(bool => "boolean"); |
| 64 | simple_impl!(f32 => "number", "float"); |
| 65 | simple_impl!(f64 => "number", "double"); |
| 66 | ranged_impl!(i8 => "integer", "int8"); |
| 67 | ranged_impl!(i16 => "integer", "int16"); |
| 68 | simple_impl!(i32 => "integer", "int32"); |
| 69 | simple_impl!(i64 => "integer", "int64"); |
| 70 | simple_impl!(i128 => "integer", "int128"); |
| 71 | simple_impl!(isize => "integer", "int"); |
| 72 | simple_impl!(() => "null"); |
| 73 | |
| 74 | #[cfg(feature = "std")] |
| 75 | mod 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 | |
| 92 | macro_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 | |
| 112 | ranged_impl!(u8 => "integer", "uint8"); |
| 113 | ranged_impl!(u16 => "integer", "uint16"); |
| 114 | unsigned_impl!(u32 => "integer", "uint32"); |
| 115 | unsigned_impl!(u64 => "integer", "uint64"); |
| 116 | unsigned_impl!(u128 => "integer", "uint128"); |
| 117 | unsigned_impl!(usize => "integer", "uint"); |
| 118 | |
| 119 | impl 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 | } |