loghawk/
input_reader.rs

1use std::{
2    fs::File, io::{stdin, Read, Stdin}
3};
4
5use clio::ClioPath;
6
7pub enum InputReader {
8    Stdin(Stdin),
9    File(File),
10}
11
12impl Read for InputReader {
13    fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
14        match self {
15            InputReader::Stdin(r) => r.read(buf),
16            InputReader::File(r) => r.read(buf),
17        }
18    }
19}
20
21impl TryFrom<&ClioPath> for InputReader {
22    type Error = anyhow::Error;
23
24    fn try_from(input: &ClioPath) -> Result<Self, Self::Error> {
25        if input.is_std() {
26            Ok(Self::Stdin(stdin()))
27        } else {
28            Ok(Self::File(File::open(input.path())?))
29        }
30    }
31}