From fc9ed821ffdcdd5abf8012e72deb354d9f6a914e Mon Sep 17 00:00:00 2001 From: Javier Arias Date: Wed, 5 Apr 2023 10:38:11 +0100 Subject: [PATCH] Fix encoding problem --- src/field/mod.rs | 18 ++++++++---------- src/lib.rs | 5 ++++- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/src/field/mod.rs b/src/field/mod.rs index a16c321..d48af68 100644 --- a/src/field/mod.rs +++ b/src/field/mod.rs @@ -59,16 +59,14 @@ impl fmt::Display for Field<'_> { } _ => { // variable data field - self.get_data::<[u8]>() - .iter() - .map(|&b| { - if b == SUBFIELD_DELIMITER { - '$' - } else { - b as char - } - }) - .collect::() + String::from_utf8_lossy( + &self + .get_data::<[u8]>() + .iter() + .map(|b| if *b == SUBFIELD_DELIMITER { 36 } else { *b }) + .collect::>(), + ) + .to_string() } }; write!(f, "{} {}", tag, field_data) diff --git a/src/lib.rs b/src/lib.rs index 2eaaa53..3d182c5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1165,6 +1165,9 @@ mod tests { builder .add_fields(fields!( data fields: [ + b"264", b" 1", [ + b'a' => "León, Spain", + ], b"245", b"00", [ b'a' => "Book title", b'b' => "Book Subtitle", @@ -1184,7 +1187,7 @@ mod tests { .unwrap(); let record = builder.get_record().unwrap(); - let expected = "=LDR 00191nam 2200085 i 4500\n=001 000000001\n=008 210128t20212021enka\\\\\\\\sb\\\\\\\\000\\0\\eng\\d\n=041 0 $aeng\n=100 1 $aAuthor Name\n=245 00$aBook title$bBook Subtitle\n".to_string(); + let expected = "=LDR 00220nam 2200097 i 4500\n=001 000000001\n=008 210128t20212021enka\\\\\\\\sb\\\\\\\\000\\0\\eng\\d\n=041 0 $aeng\n=100 1 $aAuthor Name\n=245 00$aBook title$bBook Subtitle\n=264 1$aLeón, Spain\n".to_string(); assert_eq!(format!("{}", record), expected); }