1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/// Normalizes the return of `read_line()` in the context of a CLI application
pub fn fix_new_line(mut line: String) -> std::io::Result<String> {
    if !line.ends_with('\n') {
        return Err(std::io::Error::new(
            std::io::ErrorKind::UnexpectedEof,
            "unexpected end of file",
        ));
    }

    // Remove the \n from the line.
    line.pop();

    // Remove the \r from the line if present
    if line.ends_with('\r') {
        line.pop();
    }

    Ok(line)
}