PageRenderTime 50ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/src/rustbook/error.rs

https://github.com/paulstansifer/rust
Rust | 36 lines | 19 code | 7 blank | 10 comment | 2 complexity | 12cec25069d1b3c17147e03cfd5cb68e MD5 | raw file
Possible License(s): 0BSD, Apache-2.0, MIT, AGPL-1.0
  1. // Copyright 2014 The Rust Project Developers. See the COPYRIGHT
  2. // file at the top-level directory of this distribution and at
  3. // http://rust-lang.org/COPYRIGHT.
  4. //
  5. // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
  6. // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
  7. // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
  8. // option. This file may not be copied, modified, or distributed
  9. // except according to those terms.
  10. //! Error handling utilities. WIP.
  11. use std::error::Error;
  12. use std::fmt;
  13. pub type CliError = Box<Error + 'static>;
  14. pub type CliResult<T> = Result<T, CliError>;
  15. pub type CommandError = Box<Error + 'static>;
  16. pub type CommandResult<T> = Result<T, CommandError>;
  17. pub fn err(s: &str) -> CliError {
  18. #[derive(Debug)]
  19. struct E(String);
  20. impl Error for E {
  21. fn description(&self) -> &str { &self.0 }
  22. }
  23. impl fmt::Display for E {
  24. fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
  25. self.0.fmt(f)
  26. }
  27. }
  28. Box::new(E(s.to_string()))
  29. }