use std::io::{Read, Write};
use std::path::Path;
use std::{fs, io};
use colored::Colorize;
pub trait LogStyle: ToString {
fn announce(&self) -> colored::ColoredString { self.to_string().bold().bright_blue() }
fn announcer(&self) -> colored::ColoredString { self.to_string().italic().bright_blue() }
fn action(&self) -> colored::ColoredString { self.to_string().bold().yellow() }
fn progress(&self) -> colored::ColoredString { self.to_string().bold().green() }
fn ended(&self) -> colored::ColoredString { self.to_string().bold().bright_green() }
fn actor(&self) -> colored::ColoredString { self.to_string().italic().bright_green() }
fn amount(&self) -> colored::ColoredString { self.to_string().bold().bright_yellow() }
fn addr(&self) -> colored::ColoredString { self.to_string().bold().bright_yellow() }
fn err(&self) -> colored::ColoredString { self.to_string().bold().bright_red() }
fn err_details(&self) -> colored::ColoredString { self.to_string().bold().red() }
}
impl<T> LogStyle for T where T: ToString {}
pub fn open_file_or_stdin(filename: Option<impl AsRef<Path>>) -> Result<Box<dyn Read>, io::Error> {
Ok(match filename {
Some(filename) => {
let file = fs::File::open(filename)?;
Box::new(file)
}
None => Box::new(io::stdin()),
})
}
pub fn open_file_or_stdout(
filename: Option<impl AsRef<Path>>,
) -> Result<Box<dyn Write>, io::Error> {
Ok(match filename {
Some(filename) => {
let file = fs::File::create(filename)?;
Box::new(file)
}
None => Box::new(io::stdout()),
})
}
pub fn read_file_or_stdin(filename: Option<impl AsRef<Path>>) -> Result<Vec<u8>, io::Error> {
let mut reader = open_file_or_stdin(filename)?;
let mut buf = Vec::new();
reader.read_to_end(&mut buf)?;
Ok(buf)
}
pub fn write_file_or_stdout(
data: impl AsRef<[u8]>,
filename: Option<impl AsRef<Path>>,
) -> Result<(), io::Error> {
let mut writer = open_file_or_stdout(filename)?;
writer.write_all(data.as_ref())
}