common.rs 1.2 KB

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