schemars/json_schema_impls/
nonzero.rs

1use crate::_alloc_prelude::*;
2use crate::{JsonSchema, Schema, SchemaGenerator};
3use alloc::borrow::Cow;
4use core::num::*;
5
6macro_rules! nonzero_signed_impl {
7    ($type:ty => $primitive:ty) => {
8        impl JsonSchema for $type {
9            inline_schema!();
10
11            fn schema_name() -> Cow<'static, str> {
12                stringify!($type).into()
13            }
14
15            fn schema_id() -> Cow<'static, str> {
16                stringify!(std::num::$type).into()
17            }
18
19            fn json_schema(generator: &mut SchemaGenerator) -> Schema {
20                let mut schema = <$primitive>::json_schema(generator);
21                schema.insert("not".to_owned(), serde_json::json!({
22                    "const": 0
23                }));
24                schema
25            }
26        }
27    };
28}
29
30nonzero_signed_impl!(NonZeroI8 => i8);
31nonzero_signed_impl!(NonZeroI16 => i16);
32nonzero_signed_impl!(NonZeroI32 => i32);
33nonzero_signed_impl!(NonZeroI64 => i64);
34nonzero_signed_impl!(NonZeroI128 => i128);
35nonzero_signed_impl!(NonZeroIsize => isize);
36
37macro_rules! nonzero_unsigned_impl {
38    ($type:ty => $primitive:ty) => {
39        impl JsonSchema for $type {
40            inline_schema!();
41
42            fn schema_name() -> Cow<'static, str> {
43                stringify!($type).into()
44            }
45
46            fn schema_id() -> Cow<'static, str> {
47                stringify!(std::num::$type).into()
48            }
49
50            fn json_schema(generator: &mut SchemaGenerator) -> Schema {
51                let mut schema = <$primitive>::json_schema(generator);
52                schema.insert("minimum".to_owned(), 1.into());
53                schema
54            }
55        }
56    };
57}
58
59nonzero_unsigned_impl!(NonZeroU8 => u8);
60nonzero_unsigned_impl!(NonZeroU16 => u16);
61nonzero_unsigned_impl!(NonZeroU32 => u32);
62nonzero_unsigned_impl!(NonZeroU64 => u64);
63nonzero_unsigned_impl!(NonZeroU128 => u128);
64nonzero_unsigned_impl!(NonZeroUsize => usize);