diff --git a/pdf/examples/names.rs b/pdf/examples/names.rs index 221dfff1..a1784496 100644 --- a/pdf/examples/names.rs +++ b/pdf/examples/names.rs @@ -73,7 +73,7 @@ fn main() { let mut dests_cb = |key: &PdfString, val: &Option| { //println!("{:?} {:?}", key, val); if let Some(Dest { page: Some(page), ..}) = val { - pages_map.insert(key.to_string_lossy().unwrap(), page.get_inner()); + pages_map.insert(key.to_string_lossy(), page.get_inner()); } count += 1; diff --git a/pdf/examples/read.rs b/pdf/examples/read.rs index 5006ecfe..25ed5a17 100644 --- a/pdf/examples/read.rs +++ b/pdf/examples/read.rs @@ -84,12 +84,7 @@ fn main() -> Result<(), PdfError> { for field in forms.fields.iter() { print!(" {:?} = ", field.name); match field.value { - Primitive::String(ref s) => { - match s.to_string_lossy() { - Ok(s) => println!("{:?}", s), - Err(_) => println!("{:?}", s), - } - } + Primitive::String(ref s) => println!("{}", s.to_string_lossy()), Primitive::Integer(i) => println!("{}", i), Primitive::Name(ref s) => println!("{}", s), ref p => println!("{:?}", p), diff --git a/pdf/src/font.rs b/pdf/src/font.rs index 756d1455..845d7af2 100644 --- a/pdf/src/font.rs +++ b/pdf/src/font.rs @@ -477,10 +477,10 @@ pub fn utf16be_to_char( char::decode_utf16(data.chunks(2).map(|w| u16::from_be_bytes([w[0], w[1]]))) } /// converts UTF16-BE to a string replacing illegal/unknown characters -pub fn utf16be_to_string_lossy(data: &[u8]) -> pdf::error::Result { - Ok(utf16be_to_char(data) +pub fn utf16be_to_string_lossy(data: &[u8]) -> String { + utf16be_to_char(data) .map(|r| r.unwrap_or(std::char::REPLACEMENT_CHARACTER)) - .collect()) + .collect() } /// converts UTF16-BE to a string errors out in illegal/unknonw characters pub fn utf16be_to_string(data: &[u8]) -> pdf::error::Result { @@ -624,6 +624,6 @@ mod tests { assert_eq!(r.to_string(), "UTF16 decode error"); } assert_eq!(utf16be_to_string(&v[..8]).unwrap(), String::from("𝄞mu")); - assert_eq!(utf16be_to_string_lossy(&v).unwrap(), lossy); + assert_eq!(utf16be_to_string_lossy(&v), lossy); } } diff --git a/pdf/src/primitive.rs b/pdf/src/primitive.rs index d84f2a25..ad44a78f 100644 --- a/pdf/src/primitive.rs +++ b/pdf/src/primitive.rs @@ -401,12 +401,12 @@ impl PdfString { /// therefore only lossy decoding is possible replacing unknown characters. /// For decoding correctly see /// pdf_tools/src/lib.rs - pub fn to_string_lossy(&self) -> Result { + pub fn to_string_lossy(&self) -> String { if self.data.starts_with(&[0xfe, 0xff]) { - Ok(crate::font::utf16be_to_string_lossy(&self.data[2..])?) + crate::font::utf16be_to_string_lossy(&self.data[2..]) } else { - Ok(String::from_utf8_lossy(&self.data).into()) + String::from_utf8_lossy(&self.data).into() } } /// without encoding information the PdfString cannot be sensibly decoded into a String @@ -534,7 +534,7 @@ impl Primitive { } pub fn to_string_lossy(&self) -> Result { let s = self.as_string()?; - s.to_string_lossy() + Ok(s.to_string_lossy()) } pub fn to_string(&self) -> Result { let s = self.as_string()?; @@ -642,7 +642,7 @@ impl<'a> TryInto> for &'a Primitive { fn try_into(self) -> Result> { match *self { Primitive::Name(ref s) => Ok(Cow::Borrowed(&*s)), - Primitive::String(ref s) => Ok(Cow::Owned(s.to_string_lossy()?)), + Primitive::String(ref s) => Ok(Cow::Owned(s.to_string_lossy())), ref p => Err(PdfError::UnexpectedPrimitive { expected: "Name or String", found: p.get_debug_name() @@ -655,7 +655,7 @@ impl<'a> TryInto for &'a Primitive { fn try_into(self) -> Result { match *self { Primitive::Name(ref s) => Ok(s.as_str().into()), - Primitive::String(ref s) => Ok(s.to_string_lossy()?), + Primitive::String(ref s) => Ok(s.to_string_lossy()), ref p => Err(PdfError::UnexpectedPrimitive { expected: "Name or String", found: p.get_debug_name() @@ -725,14 +725,14 @@ mod tests { #[test] fn utf16be_string() { let s = PdfString::new([0xfe, 0xff, 0x20, 0x09].as_slice().into()); - assert_eq!(s.to_string_lossy().unwrap(), "\u{2009}"); + assert_eq!(s.to_string_lossy(), "\u{2009}"); } #[test] fn utf16be_invalid_string() { let s = PdfString::new([0xfe, 0xff, 0xd8, 0x34].as_slice().into()); let repl_ch = String::from(std::char::REPLACEMENT_CHARACTER); - assert_eq!(s.to_string_lossy().unwrap(), repl_ch); + assert_eq!(s.to_string_lossy(), repl_ch); } #[test] @@ -740,7 +740,7 @@ mod tests { fn utf16be_invalid_bytelen() { let s = PdfString::new([0xfe, 0xff, 0xd8, 0x34, 0x20].as_slice().into()); let repl_ch = String::from(std::char::REPLACEMENT_CHARACTER); - assert_eq!(s.to_string_lossy().unwrap(), repl_ch); + assert_eq!(s.to_string_lossy(), repl_ch); } #[test] @@ -750,16 +750,16 @@ mod tests { assert!(s.to_string().is_err()); // FIXME verify it is a PdfError::Utf16Decode // verify UTF-16-BE supports umlauts let s = PdfString::new([0xfe, 0xff, 0x00, 0xe4 /*ä*/].as_slice().into()); - assert_eq!(s.to_string_lossy().unwrap(), "ä"); + assert_eq!(s.to_string_lossy(), "ä"); assert_eq!(s.to_string().unwrap(), "ä"); // verify valid UTF-8 bytestream with umlaut works let s = PdfString::new([b'm', b'i', b't', 0xc3, 0xa4 /*ä*/].as_slice().into()); - assert_eq!(s.to_string_lossy().unwrap(), "mitä"); + assert_eq!(s.to_string_lossy(), "mitä"); assert_eq!(s.to_string().unwrap(), "mitä"); // verify valid ISO-8859-1 bytestream with umlaut fails let s = PdfString::new([b'm', b'i', b't', 0xe4/*ä in latin1*/].as_slice().into()); let repl_ch = ['m', 'i', 't', std::char::REPLACEMENT_CHARACTER].iter().collect::(); - assert_eq!(s.to_string_lossy().unwrap(), repl_ch); + assert_eq!(s.to_string_lossy(), repl_ch); assert!(s.to_string().is_err()); // FIXME verify it is a PdfError::Utf16Decode } }