bytelines/
util.rs

1//! Module exposing utility handlers across read types.
2use std::io::Result;
3
4/// Handles a line of input and maps into the provided buffer and returns a reference.
5pub fn handle_line(input: Result<usize>, buffer: &mut Vec<u8>) -> Option<Result<&[u8]>> {
6    match input {
7        // short circuit on error
8        Err(e) => Some(Err(e)),
9
10        // no input, done
11        Ok(0) => None,
12
13        // bytes!
14        Ok(mut n) => {
15            // always "pop" the delim
16            if buffer[n - 1] == b'\n' {
17                n -= 1;
18                // also "pop" a potential leading \r
19                if n > 0 && buffer[n - 1] == b'\r' {
20                    n -= 1;
21                }
22            }
23
24            // pass back the byte slice
25            Some(Ok(&buffer[..n]))
26        }
27    }
28}