use std::fs::File;
use std::io::{self, Read};

pub enum Source {
    Stdin,
    File(File),
}

impl Source {
    pub fn open(path: Option<&str>) -> io::Result<Self> {
        match path {
            None | Some("-") => Ok(Source::Stdin),
            Some(p) => File::open(p).map(Source::File),
        }
    }
}

impl Read for Source {
    fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
        match self {
            Source::Stdin => io::stdin().read(buf),
            Source::File(f) => f.read(buf),
        }
    }
}
