1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 |
- #![allow(dead_code)]
- use std::{fs, io};
- /// This can be changed to modify all the tool metadata all at once
- pub const VERSION: &'static str = "0.0";
- pub const AUTHOR: &'static str = "Getty Ritter <rrecutils@infinitenegativeutility.com>";
- // pub fn with_input_file<R>(spec: Option<&str>, f: impl FnOnce(&dyn io::Write) -> R) -> io::Result<R> {
- // match spec.unwrap_or("-") {
- // "-" => f(&io::BufReader::new(io::stdout())),
- // path => f(&io::BufReader::new(fs::File::open(path)?)),
- // }
- // }
- /// If this doesn't name a path, or if the path is `"-"`, then return
- /// a buffered reader from stdin; otherwise, attempt to open the file
- /// named by the path and return a buffered reader around it
- pub fn input_from_spec<'a>(spec: Option<&'a str>) -> io::Result<io::BufReader<Box<dyn io::Read>>> {
- match spec.unwrap_or("-") {
- "-" => Ok(io::BufReader::new(Box::new(io::stdin()))),
- path => {
- let f = fs::File::open(path)?;
- Ok(io::BufReader::new(Box::new(f)))
- }
- }
- }
- pub fn with_output_file<R>(
- spec: Option<&str>,
- f: impl FnOnce(&mut dyn io::Write) -> R,
- ) -> io::Result<R> {
- Ok(match spec.unwrap_or("-") {
- "-" => f(&mut io::stdout()),
- path => f(&mut fs::File::open(path)?),
- })
- }
- /// If this doesn't name a path, or if the path is `"-"`, then return
- /// a buffered writer to stdout; otherwise, attempt to open the file
- /// named by the path and return a writer around it
- pub fn output_from_spec<'a>(spec: Option<&'a str>) -> io::Result<Box<dyn io::Write>> {
- match spec.unwrap_or("-") {
- "-" => Ok(Box::new(io::stdout())),
- path => Ok(Box::new(fs::File::open(path)?)),
- }
- }
|