Creates a [`String`] from a byte vector which may be gzip compressed. If `buffer` contains gzip compressed data, it decompressed before converting it into a `String`. Detects whether `buffer` contains gzip compressed data by checking if it is longer than two bytes and whether the first two bytes are the [`GZIP_MAGIC_NUMBER`]. # Errors Returns an error if - `buffer` contains invalid gzip compres
(buffer: Vec<u8>)
| 24 | /// - `buffer` contains invalid gzip compressed data |
| 25 | /// - or `buffer` can not be converted to `String`. |
| 26 | pub fn mtree_buffer_to_string(buffer: Vec<u8>) -> Result<String, Error> { |
| 27 | if buffer.len() >= 2 && [buffer[0], buffer[1]] == GZIP_MAGIC_NUMBER { |
| 28 | let mut decoder = GzDecoder::new(buffer.as_slice()); |
| 29 | |
| 30 | let mut content = String::new(); |
| 31 | decoder |
| 32 | .read_to_string(&mut content) |
| 33 | .map_err(Error::InvalidGzip)?; |
| 34 | Ok(content) |
| 35 | } else { |
| 36 | Ok(String::from_utf8(buffer)?) |
| 37 | } |
| 38 | } |
no test coverage detected