diff --git a/schemars/src/json_schema_impls/mod.rs b/schemars/src/json_schema_impls/mod.rs index 58b27c2..a505ecd 100644 --- a/schemars/src/json_schema_impls/mod.rs +++ b/schemars/src/json_schema_impls/mod.rs @@ -32,16 +32,17 @@ macro_rules! forward_impl { } mod array; +#[cfg(std_atomic)] +mod atomic; #[cfg(feature = "chrono")] mod chrono; mod core; mod ffi; mod maps; +mod nonzero_unsigned; mod primitives; mod sequences; mod serdejson; mod time; mod tuple; mod wrapper; -#[cfg(std_atomic)] -mod atomic; \ No newline at end of file diff --git a/schemars/src/json_schema_impls/nonzero_unsigned.rs b/schemars/src/json_schema_impls/nonzero_unsigned.rs new file mode 100644 index 0000000..ded43f3 --- /dev/null +++ b/schemars/src/json_schema_impls/nonzero_unsigned.rs @@ -0,0 +1,44 @@ +use crate::gen::SchemaGenerator; +use crate::schema::*; +use crate::JsonSchema; +use std::num::*; + +macro_rules! nonzero_unsigned_impl { + ($type:ty => $primitive:ty) => { + impl JsonSchema for $type { + no_ref_schema!(); + + fn schema_name() -> String { + stringify!($type).to_owned() + } + + fn json_schema(gen: &mut SchemaGenerator) -> Schema { + let mut schema: SchemaObject = <$primitive>::json_schema(gen).into(); + schema.number().minimum = Some(1.0); + schema.into() + } + } + }; +} + +nonzero_unsigned_impl!(NonZeroU8 => u8); +nonzero_unsigned_impl!(NonZeroU16 => u16); +nonzero_unsigned_impl!(NonZeroU32 => u32); +nonzero_unsigned_impl!(NonZeroU64 => u64); +nonzero_unsigned_impl!(NonZeroU128 => u128); +nonzero_unsigned_impl!(NonZeroUsize => usize); + +#[cfg(test)] +mod tests { + use super::*; + use crate::tests::schema_object_for; + use pretty_assertions::assert_eq; + + #[test] + fn schema_for_atomics() { + let schema = schema_object_for::(); + assert_eq!(schema.number.unwrap().minimum, Some(1.0)); + assert_eq!(schema.instance_type, Some(InstanceType::Integer.into())); + assert_eq!(schema.format, Some("uint32".to_owned())); + } +}