1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
//! A pattern that often occurs when writing command-line utilities
//! is that one wants to open a file when a filename argument is
//! provided or read/write from/to stdin/stdout otherwise. Unfortunatlely,
//! this is more work in Rust than it should be.
//!
//! The `stdinout` crate provides a small wrapper that makes it easier
//! to handle this scenario.
//!
//! For reading from a file or the standard input:
//!
//! ```rust,ignore
//! let input = Input::from(matches.free.get(0));
//! let reader = or_exit(input.buf_read());
//!
//! for line in reader.lines() {
//!     // Use 'line'
//! }
//! ```
//!
//! For writing to a file or the standard output:
//!
//! ```rust,ignore
//! let output = Output::from(args.get(1));
//!
//! // Get an object that implements the Write trait.
//! let write = output.write().unwrap();
//! ```

use std::fs::File;
use std::path::PathBuf;
use std::io;
use std::io::{Read, BufRead, BufReader, Write};

pub struct InputReader<'a>(Box<BufRead + 'a>);

impl<'a> Read for InputReader<'a> {
    fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
        self.0.read(buf)
    }
}

impl<'a> BufRead for InputReader<'a> {
    fn fill_buf(&mut self) -> io::Result<&[u8]> {
        self.0.fill_buf()
    }

    fn consume(&mut self, amt: usize) {
        self.0.consume(amt)
    }
}

pub enum Input {
    Stdin(io::Stdin),
    File(PathBuf),
}

impl Input {
    pub fn from<P>(path: Option<P>) -> Self
        where P: Into<PathBuf>
    {
        match path {
            Some(path) => Input::File(path.into()),
            None => Input::Stdin(io::stdin()),
        }
    }

    pub fn buf_read(&self) -> io::Result<InputReader> {
        match self {
            &Input::Stdin(ref stdin) => Result::Ok(InputReader(Box::new(stdin.lock()))),
            &Input::File(ref path) => {
                File::open(path).map(BufReader::new).map(Box::new).map(|r| InputReader(r))
            }
        }
    }
}

pub enum Output {
    Stdout(io::Stdout),
    File(PathBuf),
}

impl Output {
    pub fn from<P>(path: Option<P>) -> Self
        where P: Into<PathBuf>
    {
        match path {
            Some(path) => Output::File(path.into()),
            None => Output::Stdout(io::stdout()),
        }
    }

    pub fn write<'a>(&'a self) -> io::Result<Box<Write + 'a>> {
        match self {
            &Output::Stdout(ref stdout) => Result::Ok(Box::new(stdout.lock())),
            &Output::File(ref path) => Result::Ok(Box::new(try!(File::create(path)))),
        }
    }
}