warn_exit.rs 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. //
  2. // imag - the personal information management suite for the commandline
  3. // Copyright (C) 2015, 2016 Matthias Beyer <mail@beyermatthias.de> and contributors
  4. //
  5. // This library is free software; you can redistribute it and/or
  6. // modify it under the terms of the GNU Lesser General Public
  7. // License as published by the Free Software Foundation; version
  8. // 2.1 of the License.
  9. //
  10. // This library is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. // Lesser General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU Lesser General Public
  16. // License along with this library; if not, write to the Free Software
  17. // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. //
  19. /// This function prints the string `s` via the `warn!()` macro and then exits with the code `code`
  20. /// as status.
  21. ///
  22. /// Convenience function to be used in matches to remove one scope:
  23. ///
  24. /// ```ignore
  25. /// use libimagutil::warn_exit::warn_exit;
  26. ///
  27. /// let r: Result<i32, i32> = Err(1);
  28. /// match r {
  29. /// Err(e) => warn_exit("Warning!", 42),
  30. /// Ok(num) => { /* ... */ }
  31. /// }
  32. /// ```
  33. ///
  34. pub fn warn_exit(s: &str, code: i32) -> ! {
  35. use std::process::exit;
  36. warn!("{}", s);
  37. exit(code);
  38. }