You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

40 lines
895 B

use std::{
io::{
self,
Read,
},
error::Error,
marker::{
Send, Sync,
},
};
/// Read whole stdin
pub fn stdin() -> io::Result<String>
{
let mut buffer = String::new();
io::stdin().read_to_string(&mut buffer)?;
Ok(buffer)
}
/// Read each line with lambda
pub fn stdin_lines<E, F: FnMut(&str) -> Result<bool, E>>(mut callback: F) -> io::Result<usize>
where E: Into<Box<dyn Error + Send + Sync>>
{
let mut lines=0;
let mut input = String::new();
let stdin = io::stdin();
while match stdin.read_line(&mut input) {
Ok(0) => return Ok(lines),
Err(e) => return Err(e),
Ok(v) if input.as_bytes()[v-1] == b'\n' => callback(&input[..v]).or_else(|e| Err(io::Error::new(io::ErrorKind::Other, e)))?,
Ok(_) => callback(&input[..]).or_else(|e| Err(io::Error::new(io::ErrorKind::Other, e)))?,
} {
input.clear();
lines+=1;
}
Ok(lines)
}