Skip to content

Commit

Permalink
Add serde support to number type (#2645)
Browse files Browse the repository at this point in the history
## Motivation and Context
This is a child PR of #2616

The changes that this PR introduces is same as the ones that were merged
to `unstable-serde-support` branch before.

Initially, we tried to make commit to unstable-serde-support branch and
merge changes one by one in small PRs. However, in order to make it up
to date with the main branch, we would need to go through a large PR of
over 700 files.

Thus, I decided to create individual PRs that commits directly to `main`
branch.

## Description
- Implements `serde` support to `Number`

## Testing
- Test checks whether the serialized/de-serialized data matches with the
expected value

## Checklist
NA

----

_By submitting this pull request, I confirm that you can use, modify,
copy, and redistribute this contribution, under the terms of your
choice._

---------

Co-authored-by: John DiSanti <[email protected]>
Co-authored-by: John DiSanti <[email protected]>
  • Loading branch information
3 people authored May 30, 2023
1 parent b30b063 commit 840221d
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions rust-runtime/aws-smithy-types/src/number.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,33 @@
* SPDX-License-Identifier: Apache-2.0
*/

//! A number type that implements Javascript / JSON semantics.

use crate::error::{TryFromNumberError, TryFromNumberErrorKind};
#[cfg(all(
aws_sdk_unstable,
any(feature = "serde-serialize", feature = "serde-deserialize")
))]
use serde;

/// A number type that implements Javascript / JSON semantics, modeled on serde_json:
/// <https://docs.serde.rs/src/serde_json/number.rs.html#20-22>
#[derive(Debug, Clone, Copy, PartialEq)]
#[cfg_attr(
all(aws_sdk_unstable, feature = "serde-deserialize"),
derive(serde::Deserialize)
)]
#[cfg_attr(
all(aws_sdk_unstable, feature = "serde-serialize"),
derive(serde::Serialize)
)]
#[cfg_attr(
any(
all(aws_sdk_unstable, feature = "serde-deserialize"),
all(aws_sdk_unstable, feature = "serde-serialize")
),
serde(untagged)
)]
pub enum Number {
/// Unsigned 64-bit integer value.
PosInt(u64),
Expand Down Expand Up @@ -441,4 +463,31 @@ mod test {
1452089100f32
);
}

#[test]
#[cfg(all(
test,
aws_sdk_unstable,
feature = "serde-deserialize",
feature = "serde-serialize"
))]
/// ensures that numbers are deserialized as expected
/// 0 <= PosInt
/// 0 > NegInt
/// non integer values == Float
fn number_serde() {
let n: Number = serde_json::from_str("1.1").unwrap();
assert_eq!(n, Number::Float(1.1));
let n: Number = serde_json::from_str("1").unwrap();
assert_eq!(n, Number::PosInt(1));
let n: Number = serde_json::from_str("0").unwrap();
assert_eq!(n, Number::PosInt(0));
let n: Number = serde_json::from_str("-1").unwrap();
assert_eq!(n, Number::NegInt(-1));

assert_eq!("1.1", serde_json::to_string(&Number::Float(1.1)).unwrap());
assert_eq!("1", serde_json::to_string(&Number::PosInt(1)).unwrap());
assert_eq!("0", serde_json::to_string(&Number::PosInt(0)).unwrap());
assert_eq!("-1", serde_json::to_string(&Number::NegInt(-1)).unwrap());
}
}

0 comments on commit 840221d

Please sign in to comment.