From 39837e28f8b10f635e70353dd77802065dafb1d6 Mon Sep 17 00:00:00 2001 From: Nick Babcock Date: Thu, 16 Nov 2023 06:24:27 -0600 Subject: [PATCH] Fix take_last on last field There's a compilation error on the take_last attribute if it is used on the final field in a struct. This fixes the compilation error. This commit also showcases take_last attribute in the documentation --- README.md | 5 +++++ jomini_derive/src/lib.rs | 2 +- jomini_derive/tests/11-last.rs | 14 ++++++++++++++ src/lib.rs | 5 +++++ 4 files changed, 25 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 5479209..833500c 100644 --- a/README.md +++ b/README.md @@ -42,6 +42,8 @@ pub struct Model { #[jomini(alias = "core", duplicated)] cores: Vec, names: Vec, + #[jomini(take_last)] + checksum: String, } let data = br#" @@ -51,6 +53,8 @@ let data = br#" core = "HAB" names = { "Johan" "Frederick" } core = FRA + checksum = "first" + checksum = "second" "#; let expected = Model { @@ -60,6 +64,7 @@ let expected = Model { fourth: 10, cores: vec!["HAB".to_string(), "FRA".to_string()], names: vec!["Johan".to_string(), "Frederick".to_string()], + checksum: "second".to_string(), }; let actual: Model = jomini::text::de::from_windows1252_slice(data)?; diff --git a/jomini_derive/src/lib.rs b/jomini_derive/src/lib.rs index 0198bed..c9ea3e5 100644 --- a/jomini_derive/src/lib.rs +++ b/jomini_derive/src/lib.rs @@ -450,7 +450,7 @@ pub fn derive(input: TokenStream) -> TokenStream { while let Some(__key) = ::serde::de::MapAccess::next_key::<__Field>(&mut __map)? { match __key { - #(#builder_fields),* + #(#builder_fields),* , _ => { ::serde::de::MapAccess::next_value::<::serde::de::IgnoredAny>(&mut __map)?; } } } diff --git a/jomini_derive/tests/11-last.rs b/jomini_derive/tests/11-last.rs index d964127..421e77b 100644 --- a/jomini_derive/tests/11-last.rs +++ b/jomini_derive/tests/11-last.rs @@ -8,6 +8,15 @@ pub struct Model { fourth: u16, } + +#[derive(JominiDeserialize)] +pub struct Model2 { + human: bool, + fourth: u16, + #[jomini(take_last)] + checksum: String, +} + #[test] fn test_options() { let data = r#" @@ -19,7 +28,12 @@ fn test_options() { }"#; let m: Model = serde_json::from_str(data).unwrap(); + let m2: Model2 = serde_json::from_str(data).unwrap(); assert_eq!(m.checksum, String::from("false")); assert_eq!(m.human, true); assert_eq!(m.fourth, 4); + assert_eq!(m.checksum, m2.checksum); + assert_eq!(m.human, m2.human); + assert_eq!(m.fourth, m2.fourth); + } diff --git a/src/lib.rs b/src/lib.rs index 040e5a0..d7f3d05 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -41,6 +41,8 @@ pub struct Model { #[jomini(alias = "core", duplicated)] cores: Vec, names: Vec, + #[jomini(take_last)] + checksum: String, } let data = br#" @@ -50,6 +52,8 @@ let data = br#" core = "HAB" names = { "Johan" "Frederick" } core = FRA + checksum = "first" + checksum = "second" "#; let expected = Model { @@ -59,6 +63,7 @@ let expected = Model { fourth: 10, cores: vec!["HAB".to_string(), "FRA".to_string()], names: vec!["Johan".to_string(), "Frederick".to_string()], + checksum: "second".to_string(), }; let actual: Model = jomini::text::de::from_windows1252_slice(data)?;