| 1 | use crate::_alloc_prelude::*; |
| 2 | use crate::generate::Contract; |
| 3 | use crate::{JsonSchema, Schema, SchemaGenerator}; |
| 4 | use alloc::borrow::Cow; |
| 5 | use serde_json::Value; |
| 6 | |
| 7 | macro_rules! decimal_impl { |
| 8 | ($type:ty) => { |
| 9 | impl JsonSchema for $type { |
| 10 | inline_schema!(); |
| 11 | |
| 12 | fn schema_name() -> Cow<'static, str> { |
| 13 | "Decimal".into() |
| 14 | } |
| 15 | |
| 16 | fn json_schema(generator: &mut SchemaGenerator) -> Schema { |
| 17 | let (ty, pattern) = match generator.contract() { |
| 18 | Contract::Deserialize => ( |
| 19 | Value::Array(vec!["string".into(), "number".into()]), |
| 20 | r"^-?\d+(\.\d+)?([eE]\d+)?$".into(), |
| 21 | ), |
| 22 | Contract::Serialize => ("string".into(), r"^-?\d+(\.\d+)?$".into()), |
| 23 | }; |
| 24 | |
| 25 | let mut result = Schema::default(); |
| 26 | result.insert("type".to_owned(), ty); |
| 27 | result.insert("pattern".to_owned(), pattern); |
| 28 | result |
| 29 | } |
| 30 | } |
| 31 | }; |
| 32 | } |
| 33 | |
| 34 | #[cfg(feature = "rust_decimal1")] |
| 35 | decimal_impl!(rust_decimal1::Decimal); |
| 36 | #[cfg(feature = "bigdecimal04")] |
| 37 | decimal_impl!(bigdecimal04::BigDecimal); |