From f87fcec1cf3f67510957e18b7f700534afb66cb3 Mon Sep 17 00:00:00 2001 From: Bo Jeanes Date: Tue, 6 Sep 2022 08:50:56 +1000 Subject: [PATCH] Update serde_aux for fix in vityafx/serde-aux#26 --- Cargo.lock | 4 ++-- sungrow-winets/src/lib.rs | 50 ++++++--------------------------------- 2 files changed, 9 insertions(+), 45 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 4ae58f4..1712e5c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1174,9 +1174,9 @@ dependencies = [ [[package]] name = "serde-aux" -version = "3.1.0" +version = "3.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d0a77223b653fa95f3f9864f3eb25b93e4ed170687eb42d85b6b98af21d5e1de" +checksum = "730ba304b6ac130bf8aafc51b048f00431ef37601ab2b483a8e01e4f172ef2fb" dependencies = [ "chrono", "serde", diff --git a/sungrow-winets/src/lib.rs b/sungrow-winets/src/lib.rs index 234410e..e277c6b 100644 --- a/sungrow-winets/src/lib.rs +++ b/sungrow-winets/src/lib.rs @@ -523,29 +523,18 @@ impl From for Result<()> { // // Modbus uses u16 "words" instead of bytes, and the data above should always represent this, so we can take groups // of 2 and consume them as a hex-represented u16. -// -// TODO: can be simpler once https://github.com/vityafx/serde-aux/issues/26 is resolved fn words_from_string<'de, D>(deserializer: D) -> std::result::Result, D::Error> where D: serde::Deserializer<'de>, { - StringOrVecToVec::with_separator(' ').into_deserializer()(deserializer).map( - |vec: Vec| { - vec.chunks_exact(2) - .map(|chunk| { - let bytes: [u8; 2] = chunk - .iter() - .map(|byte_str| { - u8::from_str_radix(byte_str, 16).expect("API shouldn't return bad hex") - }) - .collect::>() - .try_into() - .expect("we always have two elements, because of `chunks_exact`"); - u16::from_be_bytes(bytes) - }) - .collect::>() - }, + StringOrVecToVec::new(' ', |s| u8::from_str_radix(s, 16), true).into_deserializer()( + deserializer, ) + .map(|vec| { + vec.chunks_exact(2) + .map(|bytes| u16::from_be_bytes(bytes.try_into().unwrap())) + .collect() + }) } #[test] @@ -563,28 +552,3 @@ fn test_words_from_string() { &[0x00AA, 0x0001, 0x000D, 0x001E, 0x000F, 0x0000, 0x0055] ); } - -#[test] -#[ignore] // For a bug report in serde_aux: https://github.com/vityafx/serde-aux/issues/26 -fn test_bytes_from_string() { - fn bytes_from_string<'de, D>(deserializer: D) -> std::result::Result, D::Error> - where - D: serde::Deserializer<'de>, - { - StringOrVecToVec::new(' ', |s| { - println!("{:?}", &s); - u8::from_str_radix(s, 16) - }) - .into_deserializer()(deserializer) - } - - #[derive(serde::Deserialize, Debug)] - struct MyStruct { - #[serde(deserialize_with = "bytes_from_string")] - list: Vec, - } - - let s = r#" { "list": "a1 b2 c3 d4 " } "#; - let a: MyStruct = serde_json::from_str(s).unwrap(); - assert_eq!(&a.list, &[0xa1, 0xb2, 0xc3, 0xd4]); -}