schemars/json_schema_impls/
nonzero_unsigned.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 mut schema: SchemaObject = <$primitive>::json_schema(gen).into();
22 schema.number().minimum = Some(1.0);
23 schema.into()
24 }
25 }
26 };
27}
28
29nonzero_unsigned_impl!(NonZeroU8 => u8);
30nonzero_unsigned_impl!(NonZeroU16 => u16);
31nonzero_unsigned_impl!(NonZeroU32 => u32);
32nonzero_unsigned_impl!(NonZeroU64 => u64);
33nonzero_unsigned_impl!(NonZeroU128 => u128);
34nonzero_unsigned_impl!(NonZeroUsize => usize);
35
36#[cfg(test)]
37mod tests {
38 use super::*;
39 use crate::tests::schema_object_for;
40 use pretty_assertions::assert_eq;
41
42 #[test]
43 fn schema_for_nonzero_u32() {
44 let schema = schema_object_for::<NonZeroU32>();
45 assert_eq!(schema.number.unwrap().minimum, Some(1.0));
46 assert_eq!(schema.instance_type, Some(InstanceType::Integer.into()));
47 assert_eq!(schema.format, Some("uint32".to_owned()));
48 }
49}