#[derive(Clone, Debug, Eq, PartialEq)]
pub enum ReadingError {
CouldNotReadFileSystem {
msg: String,
},
NoRowsProvided,
InvalidField {
msg: String,
},
InvalidRow {
msg: String,
},
ShapesDoNotMatch {
msg: String,
},
}
impl From<std::io::Error> for ReadingError {
fn from(io_error: std::io::Error) -> Self {
Self::CouldNotReadFileSystem {
msg: io_error.to_string(),
}
}
}
impl ReadingError {
pub fn message(&self) -> Option<&str> {
match self {
ReadingError::InvalidField { msg } => Some(msg),
ReadingError::InvalidRow { msg } => Some(msg),
ReadingError::CouldNotReadFileSystem { msg } => Some(msg),
ReadingError::ShapesDoNotMatch { msg } => Some(msg),
ReadingError::NoRowsProvided => None,
}
}
}
#[cfg(test)]
mod tests {
use super::ReadingError;
use std::io;
#[test]
fn reading_error_from_io_error() {
let _parsed_reading_error: ReadingError = ReadingError::from(io::Error::new(
io::ErrorKind::AlreadyExists,
"File already exists .",
));
}
#[test]
fn extract_message_from_reading_error() {
let error_content = "Path does not exist";
assert_eq!(
ReadingError::CouldNotReadFileSystem {
msg: String::from(error_content)
}
.message()
.expect("This error should contain a mesage"),
String::from(error_content)
)
}
}