From 23d9e297c1cde98386f6254ac619ae0030bb39cb Mon Sep 17 00:00:00 2001 From: Simon Knobloch Date: Mon, 30 Sep 2024 15:00:26 +0200 Subject: [PATCH] Feat implemented support for schemars via a feature flag --- Cargo.toml | 42 +++++++++++++++++++++++++++++++++--------- src/features.rs | 15 +++++++++++++++ src/lib.rs | 4 ++++ src/system.rs | 22 ++++++++++++++++++++++ 4 files changed, 74 insertions(+), 9 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 66acf49c..89d1d8e3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,25 +18,43 @@ autotests = true autobenches = true [package.metadata.docs.rs] -features = ["usize", "u32", "u64", "isize", "i32", "i64", "bigint", "biguint", "rational", "rational32", "rational64", "bigrational", "serde"] +features = [ + "usize", + "u32", + "u64", + "isize", + "i32", + "i64", + "bigint", + "biguint", + "rational", + "rational32", + "rational64", + "bigrational", + "serde", + "schemars", +] [badges] maintenance = { status = "actively-developed" } [workspace] -members = [ - "tests/feature_check", - "uom-macros", - "tests/edition_check", -] +members = ["tests/feature_check", "uom-macros", "tests/edition_check"] [dependencies] num-traits = { version = "0.2", default-features = false } num-rational = { version = "0.4", optional = true, default-features = false } -num-bigint = { version = "0.4", optional = true, default-features = false, features = ["std"] } -num-complex = { version = "0.4", optional = true, default-features = false, features = ["std"] } +num-bigint = { version = "0.4", optional = true, default-features = false, features = [ + "std", +] } +num-complex = { version = "0.4", optional = true, default-features = false, features = [ + "std", +] } serde = { version = "1.0", optional = true, default-features = false } typenum = "1.13" +schemars = { version = "0.8", optional = true, default-features = false, features = [ + "derive", +] } [dev-dependencies] approx = "0.5" @@ -71,7 +89,12 @@ f32 = [] f64 = [] si = [] std = ["num-traits/std"] -serde = ["dep:serde", "num-rational?/serde", "num-bigint?/serde", "num-complex?/serde"] +serde = [ + "dep:serde", + "num-rational?/serde", + "num-bigint?/serde", + "num-complex?/serde", +] # The try-from feature is deprecated and will be removed in a future release of uom. Functionality # previously exposed by the feature is now enabled by default. try-from = [] @@ -81,6 +104,7 @@ use_serde = ["serde"] rational-support = ["num-rational"] bigint-support = ["num-bigint", "num-rational/num-bigint-std"] complex-support = ["num-complex"] +schemars = ["dep:schemars"] [[example]] name = "base" diff --git a/src/features.rs b/src/features.rs index ba4aa4f4..7d31ed69 100644 --- a/src/features.rs +++ b/src/features.rs @@ -65,6 +65,21 @@ macro_rules! serde { ($($tt:tt)*) => {}; } +#[doc(hidden)] +#[macro_export] +#[cfg(feature = "schemars")] +macro_rules! schemars { + ($($tt:tt)*) => { $($tt)* }; +} + +/// Does not expand the given block of code when `uom` is compiled without the `serde` feature. +#[doc(hidden)] +#[macro_export] +#[cfg(not(feature = "schemars"))] +macro_rules! schemars { + ($($tt:tt)*) => {}; +} + /// Expands the given block of code when `uom` is compiled with the `si` feature. #[doc(hidden)] #[macro_export] diff --git a/src/lib.rs b/src/lib.rs index 498810d4..97cd081d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -219,6 +219,10 @@ pub extern crate num_complex; #[cfg(feature = "serde")] pub extern crate serde; +#[doc(hidden)] +#[cfg(feature = "schemars")] +pub extern crate schemars; + #[doc(hidden)] pub extern crate typenum; diff --git a/src/system.rs b/src/system.rs index b90e3b25..d3930c6b 100644 --- a/src/system.rs +++ b/src/system.rs @@ -1379,6 +1379,28 @@ macro_rules! system { } }} + schemars! { + impl $crate::schemars::JsonSchema for Quantity + where + D: Dimension + ?Sized, + U: Units + ?Sized, + V: $crate::num::Num + $crate::Conversion + $crate::schemars::JsonSchema + { + fn schema_name() -> String { + "Quantity".to_owned() + } + + fn json_schema(_gen: &mut $crate::schemars::gen::SchemaGenerator) -> $crate::schemars::schema::Schema { + use $crate::schemars::schema::{InstanceType, SchemaObject}; + + SchemaObject { + instance_type: Some(InstanceType::Number.into()), + ..Default::default() + }.into() + } + } + } + /// Utilities for formatting and printing quantities. pub mod fmt { use $crate::lib::fmt;