diff options
Diffstat (limited to 'src/encoding.rs')
-rw-r--r-- | src/encoding.rs | 17 |
1 files changed, 11 insertions, 6 deletions
diff --git a/src/encoding.rs b/src/encoding.rs index c117cd0..fc0c335 100644 --- a/src/encoding.rs +++ b/src/encoding.rs @@ -1,4 +1,6 @@ use std::convert::TryFrom; +use std::io; +use crate::error::IntoIOError; use crate::util::read_big_endian; macro_rules! guard { @@ -42,10 +44,13 @@ pub fn from_ucs_2_bom(bytes: &[u8]) -> Option<String> { Some(res) } -pub fn from_utf8_mistaken_as_latin1(latin1: &str) -> Option<String> { - guard!(latin1.chars().all(|c| (c as usize) < 256)); - match std::str::from_utf8(latin1.as_bytes()) { - Ok(res) => Some(res.to_string()), - Err(_) => None, - } +pub fn from_utf8_mistaken_as_latin1(latin1: &str) -> io::Result<String> { + latin1 + .chars() + .map(|c| u8::try_from(u32::from(c))) + .collect::<Result<Vec<u8>, _>>() + .map_err(|e| e.ioerr()) + .and_then(|v| std::str::from_utf8(&v) + .map(|s| s.to_string()) + .map_err(|e| e.ioerr())) } |