Take the UTF-8 byte sequence and parse it into a `String`. # Errors Returns a custom parse error if we encounter an invalid escaped UTF-8 sequence.
(
input: &mut &str,
checkpoint: Checkpoint<&str, &str>,
bytes: Vec<u8>,
)
| 147 | /// |
| 148 | /// Returns a custom parse error if we encounter an invalid escaped UTF-8 sequence. |
| 149 | fn bytes_to_string( |
| 150 | input: &mut &str, |
| 151 | checkpoint: Checkpoint<&str, &str>, |
| 152 | bytes: Vec<u8>, |
| 153 | ) -> ModalResult<String> { |
| 154 | match String::from_utf8(bytes) { |
| 155 | Ok(decoded) => Ok(decoded), |
| 156 | Err(_) => { |
| 157 | let mut error = ContextError::new(); |
| 158 | error = error.add_context(input, &checkpoint, StrContext::Label("UTF-8 byte sequence")); |
| 159 | Err(ErrMode::Cut(error)) |
| 160 | } |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | #[cfg(test)] |
| 165 | mod tests { |