use std::{io, path::PathBuf, str::FromStr};
use crate::{Input, Output};
#[derive(Debug, PartialEq, Eq, Clone)]
pub enum IoArg {
StdStream,
File(PathBuf),
}
impl IoArg {
pub fn is_file(&self) -> bool {
match self {
IoArg::StdStream => false,
IoArg::File(_) => true,
}
}
pub fn open_as_input(self) -> io::Result<Input> {
Input::new(self)
}
pub fn open_as_output(self) -> io::Result<Output> {
Output::new(self)
}
}
impl FromStr for IoArg {
type Err = std::convert::Infallible;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let out = match s {
"-" => IoArg::StdStream,
other => IoArg::File(other.into()),
};
Ok(out)
}
}
#[cfg(test)]
mod tests {
use super::IoArg;
#[test]
fn parsing() {
let actual: IoArg = "-".parse().unwrap();
assert_eq!(IoArg::StdStream, actual);
let actual: IoArg = "filename".parse().unwrap();
assert!(matches!(actual, IoArg::File(_)));
}
}