Browse Source

Remove bad input abstraction in favor of boxed trait

Getty Ritter 6 years ago
parent
commit
71ee2b2e16
1 changed files with 9 additions and 39 deletions
  1. 9 39
      src/tools/common.rs

+ 9 - 39
src/tools/common.rs

@@ -4,54 +4,24 @@ pub const VERSION: &'static str = "0.0";
 pub const AUTHOR: &'static str =
     "Getty Ritter <rrecutils@infinitenegativeutility.com>";
 
-pub enum Input {
-    Stdin(io::BufReader<io::Stdin>),
-    File(io::BufReader<fs::File>),
-}
-
-impl io::Read for Input {
-    fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
-        match self {
-            &mut Input::Stdin(ref mut stdin) =>
-                stdin.read(buf),
-            &mut Input::File(ref mut file) =>
-                file.read(buf),
-        }
-    }
-}
-
-impl io::BufRead for Input {
-    fn fill_buf(&mut self) -> io::Result<&[u8]> {
-        match self {
-            &mut Input::Stdin(ref mut stdin) =>
-                stdin.fill_buf(),
-            &mut Input::File(ref mut file) =>
-                file.fill_buf(),
-        }
-    }
-
-    fn consume(&mut self, amt: usize) {
-        match self {
-            &mut Input::Stdin(ref mut stdin) =>
-                stdin.consume(amt),
-            &mut Input::File(ref mut file) =>
-                file.consume(amt),
-        }
-    }
-}
-
+/// If this doesn't name a path, or if the path is `"-"`, then return
+/// a buffered reader from stdin; otherwise, attempt to open the file
+/// named by the path and return a buffered reader around it
 pub fn input_from_spec<'a>(
     spec: Option<&'a str>
-) -> io::Result<Input> {
+) -> io::Result<io::BufReader<Box<io::Read>>> {
     match spec.unwrap_or("-") {
-        "-" => Ok(Input::Stdin(io::BufReader::new(io::stdin()))),
+        "-" => Ok(io::BufReader::new(Box::new(io::stdin()))),
         path => {
             let f = fs::File::open(path)?;
-            Ok(Input::File(io::BufReader::new(f)))
+            Ok(io::BufReader::new(Box::new(f)))
         }
     }
 }
 
+/// If this doesn't name a path, or if the path is `"-"`, then return
+/// a buffered writer to stdout; otherwise, attempt to open the file
+/// named by the path and return a writer around it
 pub fn output_from_spec<'a>(
     spec: Option<&'a str>
 ) -> io::Result<Box<io::Write>>