mirror of
https://github.com/mii443/RustySecrets.git
synced 2025-08-23 00:35:38 +00:00
Refactoring to allow for a more flexible API.
This commit is contained in:
@ -1,6 +1,4 @@
|
||||
pub use std::convert;
|
||||
pub use std::io::prelude::*;
|
||||
|
||||
use std::convert;
|
||||
use std::error;
|
||||
use std::fmt;
|
||||
use std::io;
|
||||
@ -14,8 +12,12 @@ pub struct Error {
|
||||
}
|
||||
|
||||
impl Error {
|
||||
/// Initializes a new error with a description and optional detail string.
|
||||
pub fn new(descr: &'static str, detail: Option<String>) -> Error {
|
||||
Error { descr: descr, detail: detail }
|
||||
Error {
|
||||
descr: descr,
|
||||
detail: detail,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -23,17 +25,21 @@ impl fmt::Display for Error {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
match self.detail {
|
||||
None => write!(f, "{}", self.descr),
|
||||
Some(ref detail) => write!(f, "{} ({})", self.descr, detail)
|
||||
Some(ref detail) => write!(f, "{} ({})", self.descr, detail),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl error::Error for Error {
|
||||
fn description(&self) -> &str { self.descr }
|
||||
fn cause(&self) -> Option<&error::Error> { None }
|
||||
fn description(&self) -> &str {
|
||||
self.descr
|
||||
}
|
||||
fn cause(&self) -> Option<&error::Error> {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
impl convert::From<Error> for io::Error {
|
||||
impl From<Error> for io::Error {
|
||||
fn from(me: Error) -> io::Error {
|
||||
io::Error::new(io::ErrorKind::Other, me)
|
||||
}
|
||||
@ -42,30 +48,47 @@ impl convert::From<Error> for io::Error {
|
||||
/// Returns an `io::Error` from description string and optional detail string.
|
||||
/// Particularly useful in `Result` expressions.
|
||||
pub fn other_io_err(descr: &'static str, detail: Option<String>) -> io::Error {
|
||||
convert::From::from(
|
||||
Error::new(descr, detail)
|
||||
)
|
||||
convert::From::from(Error::new(descr, detail))
|
||||
}
|
||||
|
||||
/// maps a `ParseIntError` to an `io::Error`
|
||||
pub fn pie2io(p: num::ParseIntError) -> io::Error {
|
||||
convert::From::from(
|
||||
Error::new("Integer parsing error", Some(p.to_string()))
|
||||
)
|
||||
convert::From::from(Error::new("Integer parsing error", Some(p.to_string())))
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_custom_error() {
|
||||
let desc = "Boring error description";
|
||||
let detail = "More of it";
|
||||
let ewd = Error::new(desc, Some(detail.to_string()));
|
||||
#[cfg(test)]
|
||||
mod tests_custom_err {
|
||||
use std::error;
|
||||
use custom_error;
|
||||
|
||||
assert_eq!(error::Error::description(&ewd), desc);
|
||||
match error::Error::cause(&ewd) {
|
||||
Some(_) => assert!(false),
|
||||
None => assert!(true),
|
||||
#[test]
|
||||
fn test_custom_error() {
|
||||
let desc = "Boring error description";
|
||||
let detail = "More of it";
|
||||
let ewd = custom_error::Error::new(desc, Some(detail.to_string()));
|
||||
|
||||
assert_eq!(error::Error::description(&ewd), desc);
|
||||
match error::Error::cause(&ewd) {
|
||||
Some(_) => assert!(false),
|
||||
None => assert!(true),
|
||||
}
|
||||
let _formated_err = format!("{}", ewd);
|
||||
let ewod = custom_error::Error::new(desc, None);
|
||||
let _formated_err = format!("{}", ewod);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests_std_err {
|
||||
use std::error::Error;
|
||||
use custom_error::pie2io;
|
||||
|
||||
#[test]
|
||||
fn test_parse_errors() {
|
||||
let nan = "2a".to_string();
|
||||
match nan.parse::<u8>().map_err(pie2io) {
|
||||
Ok(_) => assert!(false),
|
||||
Err(x) => assert_eq!("Integer parsing error", x.description()),
|
||||
}
|
||||
}
|
||||
let _formated_err = format!("{}", ewd);
|
||||
let ewod = Error::new(desc, None);
|
||||
let _formated_err = format!("{}", ewod);
|
||||
}
|
||||
|
Reference in New Issue
Block a user