.cargo/vendor/onenote_parser/src/utils.rs
raw| 1 | use crate::errors::Result; |
| 2 | use widestring::U16CString; |
| 3 | |
| 4 | pub(crate) trait Utf16ToString { |
| 5 | fn utf16_to_string(&self) -> Result<String>; |
| 6 | } |
| 7 | |
| 8 | impl Utf16ToString for &[u8] { |
| 9 | fn utf16_to_string(&self) -> Result<String> { |
| 10 | let data: Vec<_> = self |
| 11 | .chunks_exact(2) |
| 12 | .map(|v| u16::from_le_bytes([v[0], v[1]])) |
| 13 | .collect(); |
| 14 | |
| 15 | let value = U16CString::from_vec_truncate(data); |
| 16 | Ok(value.to_string().unwrap()) |
| 17 | } |
| 18 | } |