Skip to content

Commit

Permalink
Merge pull request #132 from rakaly/take_last
Browse files Browse the repository at this point in the history
Fix take_last on last field
  • Loading branch information
nickbabcock authored Nov 16, 2023
2 parents 5bc5a55 + 39837e2 commit d600326
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 1 deletion.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ pub struct Model {
#[jomini(alias = "core", duplicated)]
cores: Vec<String>,
names: Vec<String>,
#[jomini(take_last)]
checksum: String,
}

let data = br#"
Expand All @@ -51,6 +53,8 @@ let data = br#"
core = "HAB"
names = { "Johan" "Frederick" }
core = FRA
checksum = "first"
checksum = "second"
"#;

let expected = Model {
Expand All @@ -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)?;
Expand Down
2 changes: 1 addition & 1 deletion jomini_derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?; }
}
}
Expand Down
14 changes: 14 additions & 0 deletions jomini_derive/tests/11-last.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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#"
Expand All @@ -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);

}
5 changes: 5 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ pub struct Model {
#[jomini(alias = "core", duplicated)]
cores: Vec<String>,
names: Vec<String>,
#[jomini(take_last)]
checksum: String,
}
let data = br#"
Expand All @@ -50,6 +52,8 @@ let data = br#"
core = "HAB"
names = { "Johan" "Frederick" }
core = FRA
checksum = "first"
checksum = "second"
"#;
let expected = Model {
Expand All @@ -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)?;
Expand Down

0 comments on commit d600326

Please sign in to comment.