feat: add skip option

resolves #16 and #87
This commit is contained in:
Mario Reder authored and David Peter committed 2020-05-13 18:55:24 +02:00
1 parent 6f73f4c0f0
commit 244f71691d
3 files changed
+67 -9

No files matched your search

+38
View File
@@ -0,0 +1,38 @@
use std::cell::RefCell;
use std::fs;
use std::io::{self, Read, Seek, SeekFrom};
pub enum Input<'a> {
File(fs::File),
Stdin(RefCell<io::StdinLock<'a>>),
}
impl<'a> Read for Input<'a> {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
match *self {
Input::File(ref mut file) => file.read(buf),
Input::Stdin(ref mut stdin) => stdin.borrow_mut().read(buf),
}
}
}
impl<'a> Seek for Input<'a> {
fn seek(&mut self, pos: SeekFrom) -> io::Result<u64> {
match *self {
Input::File(ref mut file) => file.seek(pos),
Input::Stdin(_) => Err(io::Error::new(
io::ErrorKind::Other,
"not supported by stdin-input",
)),
}
}
}
impl<'a> Input<'a> {
pub fn into_inner(self) -> Box<dyn Read + 'a> {
match self {
Self::File(file) => Box::new(file),
Self::Stdin(stdin) => Box::new(stdin.into_inner()),
}
}
}