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

Add support for rust_decimal and bigdecimal #101

Merged
merged 2 commits into from
Nov 25, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Add support for rust_decimal and bigdecimal
  • Loading branch information
timando committed Aug 27, 2021
commit 68e94e98b1928378f38549d786e7797d4c67e13f
2 changes: 2 additions & 0 deletions schemars/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ smallvec = { version = "1.0", optional = true }
arrayvec = { version = "0.5", default-features = false, optional = true }
url = { version = "2.0", default-features = false, optional = true }
bytes = { version = "1.0", optional = true }
rust_decimal = { version = "1", default-features = false, optional = true }
bigdecimal = { version = "0.3", default-features = false, optional = true }

[dev-dependencies]
pretty_assertions = "0.6.1"
Expand Down
31 changes: 31 additions & 0 deletions schemars/src/json_schema_impls/decimal.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use crate::gen::SchemaGenerator;
use crate::schema::*;
use crate::JsonSchema;

macro_rules! decimal_impl {
($type:ty) => {
decimal_impl!($type => Number, "Number");
};
($type:ty => $instance_type:ident, $name:expr) => {
impl JsonSchema for $type {
no_ref_schema!();

fn schema_name() -> String {
$name.to_owned()
}

fn json_schema(_: &mut SchemaGenerator) -> Schema {
SchemaObject {
instance_type: Some(InstanceType::$instance_type.into()),
..Default::default()
}
.into()
}
}
};
}

#[cfg(feature="rust_decimal")]
decimal_impl!(rust_decimal::Decimal);
#[cfg(feature="bigdecimal")]
decimal_impl!(bigdecimal::BigDecimal);
2 changes: 2 additions & 0 deletions schemars/src/json_schema_impls/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ mod bytes;
#[cfg(feature = "chrono")]
mod chrono;
mod core;
#[cfg(any(feature = "rust_decimal", feature="bigdecimal"))]
mod decimal;
#[cfg(feature = "either")]
mod either;
mod ffi;
Expand Down