schemars/json_schema_impls/
nonzero_signed.rs

1use crate::gen::SchemaGenerator;
2use crate::schema::*;
3use crate::JsonSchema;
4use std::borrow::Cow;
5use std::num::*;
6
7macro_rules! nonzero_unsigned_impl {
8    ($type:ty => $primitive:ty) => {
9        impl JsonSchema for $type {
10            no_ref_schema!();
11
12            fn schema_name() -> String {
13                stringify!($type).to_owned()
14            }
15
16            fn schema_id() -> Cow<'static, str> {
17                Cow::Borrowed(stringify!(std::num::$type))
18            }
19
20            fn json_schema(gen: &mut SchemaGenerator) -> Schema {
21                let zero_schema: Schema = SchemaObject {
22                    const_value: Some(0.into()),
23                    ..Default::default()
24                }
25                .into();
26                let mut schema: SchemaObject = <$primitive>::json_schema(gen).into();
27                schema.subschemas().not = Some(Box::from(zero_schema));
28                schema.into()
29            }
30        }
31    };
32}
33
34nonzero_unsigned_impl!(NonZeroI8 => i8);
35nonzero_unsigned_impl!(NonZeroI16 => i16);
36nonzero_unsigned_impl!(NonZeroI32 => i32);
37nonzero_unsigned_impl!(NonZeroI64 => i64);
38nonzero_unsigned_impl!(NonZeroI128 => i128);
39nonzero_unsigned_impl!(NonZeroIsize => isize);