wipe/src/error.rs

43 lines
834 B
Rust
Raw Normal View History

2022-07-29 12:50:04 +00:00
use std::fmt::{Debug, Formatter};
pub struct Error(String);
impl Debug for Error {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.write_str(&self.0)
}
}
impl From<std::io::Error> for Error {
fn from(err: std::io::Error) -> Self {
Error(err.to_string())
}
}
2022-08-01 12:03:02 +00:00
impl From<&str> for Error {
fn from(msg: &str) -> Self {
Error(msg.to_string())
}
}
2022-07-29 12:50:04 +00:00
#[cfg(test)]
mod test {
use super::*;
#[test]
fn from_std_io_error() {
let io_err = std::io::Error::new(std::io::ErrorKind::Other, "123");
let msg = io_err.to_string();
let err: Error = io_err.into();
assert_eq!(msg, format!("{:?}", err));
}
2022-08-01 12:03:02 +00:00
#[test]
fn from_ref_str() {
let err: Error = "123".into();
assert_eq!("123", format!("{:?}", err));
}
2022-07-29 12:50:04 +00:00
}