Skip to content

Commit

Permalink
Feat implemented support for schemars via a feature flag
Browse files Browse the repository at this point in the history
  • Loading branch information
akowi-sknobloch committed Sep 30, 2024
1 parent edee7cb commit 23d9e29
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 9 deletions.
42 changes: 33 additions & 9 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 = []
Expand All @@ -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"
Expand Down
15 changes: 15 additions & 0 deletions src/features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
4 changes: 4 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
22 changes: 22 additions & 0 deletions src/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1379,6 +1379,28 @@ macro_rules! system {
}
}}

schemars! {
impl<D, U, V> $crate::schemars::JsonSchema for Quantity<D, U, V>
where
D: Dimension + ?Sized,
U: Units<V> + ?Sized,
V: $crate::num::Num + $crate::Conversion<V> + $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;
Expand Down

0 comments on commit 23d9e29

Please sign in to comment.