Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat implemented support for schemars via a feature flag #483

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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