From 997cce17b7dd910d36cb66357860112d5ba93ba9 Mon Sep 17 00:00:00 2001 From: David Komer <6406986+dakom@users.noreply.github.com> Date: Mon, 24 Jun 2024 16:13:35 +0300 Subject: [PATCH 1/3] tests and docs for Order serialization Fixes comment at https://github.com/CosmWasm/cosmwasm/pull/2174#issuecomment-2186067458 --- docs/MESSAGE_TYPES.md | 3 +++ packages/std/src/iterator.rs | 30 ++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/docs/MESSAGE_TYPES.md b/docs/MESSAGE_TYPES.md index e69b8c3d6f..bc07777faf 100644 --- a/docs/MESSAGE_TYPES.md +++ b/docs/MESSAGE_TYPES.md @@ -31,6 +31,7 @@ Rust types as well as `cosmwasm_std` types and how they are encoded in JSON. | [Binary] | string containing base64 data | `"MTIzCg=="` | | | [HexBinary] | string containing hex data | `"b5d7d24e428c"` | | | [Timestamp] | string containing nanoseconds since epoch | `"1677687687000000000"` | | +| [Order] | string containing order variant | `"ascending"` or `"descending"` | | [uint64]: https://docs.rs/cosmwasm-std/1.3.3/cosmwasm_std/struct.Uint64.html [uint128]: https://docs.rs/cosmwasm-std/1.3.3/cosmwasm_std/struct.Uint128.html @@ -48,6 +49,8 @@ Rust types as well as `cosmwasm_std` types and how they are encoded in JSON. https://docs.rs/cosmwasm-std/1.3.3/cosmwasm_std/struct.HexBinary.html [timestamp]: https://docs.rs/cosmwasm-std/1.3.3/cosmwasm_std/struct.Timestamp.html +[order]: + https://docs.rs/cosmwasm-std/1.3.3/cosmwasm_std/enum.Order.html [dev-note-4]: https://medium.com/cosmwasm/dev-note-4-u128-i128-serialization-in-cosmwasm-90cb76784d44 diff --git a/packages/std/src/iterator.rs b/packages/std/src/iterator.rs index 4f39c2bcfc..36a121681d 100644 --- a/packages/std/src/iterator.rs +++ b/packages/std/src/iterator.rs @@ -33,3 +33,33 @@ impl From for i32 { original as _ } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn order_serde() { + let ascending_bytes = br#""ascending""#; + let descending_bytes = br#""descending""#; + + assert_eq!( + serde_json::to_vec(&Order::Ascending).unwrap(), + ascending_bytes + ); + assert_eq!( + serde_json::to_vec(&Order::Descending).unwrap(), + descending_bytes + ); + + assert_eq!( + serde_json::from_slice::(ascending_bytes).unwrap(), + Order::Ascending + ); + + assert_eq!( + serde_json::from_slice::(descending_bytes).unwrap(), + Order::Descending + ); + } +} From a6d289b2449cd0f4fd3c8a8509eaa37bba69e454 Mon Sep 17 00:00:00 2001 From: David Komer <6406986+dakom@users.noreply.github.com> Date: Mon, 24 Jun 2024 17:11:43 +0300 Subject: [PATCH 2/3] order serialization use cosmwasm_std functions --- packages/std/src/iterator.rs | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/packages/std/src/iterator.rs b/packages/std/src/iterator.rs index 36a121681d..41f043a093 100644 --- a/packages/std/src/iterator.rs +++ b/packages/std/src/iterator.rs @@ -36,6 +36,8 @@ impl From for i32 { #[cfg(test)] mod tests { + use crate::{from_json, to_json_vec}; + use super::*; #[test] @@ -43,22 +45,16 @@ mod tests { let ascending_bytes = br#""ascending""#; let descending_bytes = br#""descending""#; - assert_eq!( - serde_json::to_vec(&Order::Ascending).unwrap(), - ascending_bytes - ); - assert_eq!( - serde_json::to_vec(&Order::Descending).unwrap(), - descending_bytes - ); + assert_eq!(to_json_vec(&Order::Ascending).unwrap(), ascending_bytes); + assert_eq!(to_json_vec(&Order::Descending).unwrap(), descending_bytes); assert_eq!( - serde_json::from_slice::(ascending_bytes).unwrap(), + from_json::(ascending_bytes).unwrap(), Order::Ascending ); assert_eq!( - serde_json::from_slice::(descending_bytes).unwrap(), + from_json::(descending_bytes).unwrap(), Order::Descending ); } From e78dd7314c500aadc2021993543a57097437f087 Mon Sep 17 00:00:00 2001 From: David Komer <6406986+dakom@users.noreply.github.com> Date: Mon, 24 Jun 2024 17:33:30 +0300 Subject: [PATCH 3/3] format md --- docs/MESSAGE_TYPES.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/docs/MESSAGE_TYPES.md b/docs/MESSAGE_TYPES.md index bc07777faf..e9258c8f32 100644 --- a/docs/MESSAGE_TYPES.md +++ b/docs/MESSAGE_TYPES.md @@ -49,8 +49,7 @@ Rust types as well as `cosmwasm_std` types and how they are encoded in JSON. https://docs.rs/cosmwasm-std/1.3.3/cosmwasm_std/struct.HexBinary.html [timestamp]: https://docs.rs/cosmwasm-std/1.3.3/cosmwasm_std/struct.Timestamp.html -[order]: - https://docs.rs/cosmwasm-std/1.3.3/cosmwasm_std/enum.Order.html +[order]: https://docs.rs/cosmwasm-std/1.3.3/cosmwasm_std/enum.Order.html [dev-note-4]: https://medium.com/cosmwasm/dev-note-4-u128-i128-serialization-in-cosmwasm-90cb76784d44