common.rs 1.1 KB

123456789101112131415161718192021222324252627282930313233
  1. use std::{fs,io};
  2. pub const VERSION: &'static str = "0.0";
  3. pub const AUTHOR: &'static str =
  4. "Getty Ritter <rrecutils@infinitenegativeutility.com>";
  5. /// If this doesn't name a path, or if the path is `"-"`, then return
  6. /// a buffered reader from stdin; otherwise, attempt to open the file
  7. /// named by the path and return a buffered reader around it
  8. pub fn input_from_spec<'a>(
  9. spec: Option<&'a str>
  10. ) -> io::Result<io::BufReader<Box<io::Read>>> {
  11. match spec.unwrap_or("-") {
  12. "-" => Ok(io::BufReader::new(Box::new(io::stdin()))),
  13. path => {
  14. let f = fs::File::open(path)?;
  15. Ok(io::BufReader::new(Box::new(f)))
  16. }
  17. }
  18. }
  19. /// If this doesn't name a path, or if the path is `"-"`, then return
  20. /// a buffered writer to stdout; otherwise, attempt to open the file
  21. /// named by the path and return a writer around it
  22. pub fn output_from_spec<'a>(
  23. spec: Option<&'a str>
  24. ) -> io::Result<Box<io::Write>>
  25. {
  26. match spec.unwrap_or("-") {
  27. "-" => Ok(Box::new(io::stdout())),
  28. path => Ok(Box::new(fs::File::open(path)?)),
  29. }
  30. }