main.rs 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. extern crate exa;
  2. use exa::Exa;
  3. use std::ffi::OsString;
  4. use std::env::args_os;
  5. use std::io::{stdout, stderr, Write, ErrorKind};
  6. use std::process::exit;
  7. fn main() {
  8. let args: Vec<OsString> = args_os().skip(1).collect();
  9. match Exa::new(args.iter(), &mut stdout()) {
  10. Ok(mut exa) => {
  11. match exa.run() {
  12. Ok(exit_status) => exit(exit_status),
  13. Err(e) => {
  14. match e.kind() {
  15. ErrorKind::BrokenPipe => exit(exits::SUCCESS),
  16. _ => {
  17. writeln!(stderr(), "{}", e).unwrap();
  18. exit(exits::RUNTIME_ERROR);
  19. },
  20. };
  21. }
  22. };
  23. },
  24. Err(ref e) if e.is_error() => {
  25. writeln!(stderr(), "{}", e).unwrap();
  26. exit(exits::OPTIONS_ERROR);
  27. },
  28. Err(ref e) => {
  29. writeln!(stdout(), "{}", e).unwrap();
  30. exit(exits::SUCCESS);
  31. },
  32. };
  33. }
  34. extern crate libc;
  35. #[allow(trivial_numeric_casts)]
  36. mod exits {
  37. use libc::{self, c_int};
  38. pub const SUCCESS: c_int = libc::EXIT_SUCCESS;
  39. pub const RUNTIME_ERROR: c_int = libc::EXIT_FAILURE;
  40. pub const OPTIONS_ERROR: c_int = 3 as c_int;
  41. }