remove use statements in methods

This commit is contained in:
daniel
2020-11-16 11:56:10 +01:00
parent ee7a266bb7
commit 864ccf628d

View File

@@ -57,16 +57,15 @@ impl Error {
impl std::error::Error for Error { impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
use Error::*;
match &self { match &self {
InvalidOsStr(err) => { Error::InvalidOsStr(err) => {
if let Some(err) = err { if let Some(err) = err {
Some(err) Some(err)
} else { } else {
None None
} }
}, },
CouldNotCheckNAtoms(err) => Some(err.as_ref()), Error::CouldNotCheckNAtoms(err) => Some(err.as_ref()),
_ => None, _ => None,
} }
} }
@@ -101,27 +100,26 @@ impl From<(&Frame, usize)> for Error {
impl std::fmt::Display for Error { impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
use Error::*;
match self { match self {
CApiError { code, task } => write!( Error::CApiError { code, task } => write!(
f, f,
"Error while {task}: C API returned error code {code}", "Error while {task}: C API returned error code {code}",
task = task, task = task,
code = code code = code
), ),
WrongSizeFrame { expected, found } => write!( Error::WrongSizeFrame { expected, found } => write!(
f, f,
"Expected frame of size {:?}, found {:?}", "Expected frame of size {:?}, found {:?}",
expected, found expected, found
), ),
CouldNotOpen { path, mode } => { Error::CouldNotOpen { path, mode } => {
write!(f, "Could not open file at {:?} in mode {:?}", path, mode) write!(f, "Could not open file at {:?} in mode {:?}", path, mode)
} }
InvalidOsStr(_) => write!(f, "Cannot convert path to CString."), Error::InvalidOsStr(_) => write!(f, "Cannot convert path to CString."),
CouldNotCheckNAtoms(_) => { Error::CouldNotCheckNAtoms(_) => {
write!(f, "Failed to read number of atoms in trajectory file") write!(f, "Failed to read number of atoms in trajectory file")
} }
OutOfRange { Error::OutOfRange {
name, name,
task, task,
value, value,
@@ -155,13 +153,12 @@ pub enum ErrorTask {
impl std::fmt::Display for ErrorTask { impl std::fmt::Display for ErrorTask {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
use ErrorTask::*;
match &self { match &self {
ReadNumAtoms => write!(f, "reading atom number from trajectory"), ErrorTask::ReadNumAtoms => write!(f, "reading atom number from trajectory"),
Read => write!(f, "reading trajectory"), ErrorTask::Read => write!(f, "reading trajectory"),
Write => write!(f, "writing trajectory"), ErrorTask::Write => write!(f, "writing trajectory"),
Flush => write!(f, "flushing trajectory"), ErrorTask::Flush => write!(f, "flushing trajectory"),
Seek => write!(f, "seeking in trajectory"), ErrorTask::Seek => write!(f, "seeking in trajectory"),
} }
} }
} }
@@ -249,32 +246,31 @@ mod tests {
#[test] #[test]
fn test_is_eof() { fn test_is_eof() {
use Error::CApiError; let error = Error::CApiError {
let error = CApiError {
code: c_abi::xdrfile::exdrENDOFFILE.into(), code: c_abi::xdrfile::exdrENDOFFILE.into(),
task: ErrorTask::Read, task: ErrorTask::Read,
}; };
assert!(error.is_eof()); assert!(error.is_eof());
let error = CApiError { let error = Error::CApiError {
code: ErrorCode::ExdrEndOfFile, code: ErrorCode::ExdrEndOfFile,
task: ErrorTask::Read, task: ErrorTask::Read,
}; };
assert!(error.is_eof()); assert!(error.is_eof());
let error = CApiError { let error = Error::CApiError {
code: (c_abi::xdrfile::exdrENDOFFILE + 1).into(), code: (c_abi::xdrfile::exdrENDOFFILE + 1).into(),
task: ErrorTask::Read, task: ErrorTask::Read,
}; };
assert!(!error.is_eof()); assert!(!error.is_eof());
let error = CApiError { let error = Error::CApiError {
code: 0.into(), code: 0.into(),
task: ErrorTask::Read, task: ErrorTask::Read,
}; };
assert!(!error.is_eof()); assert!(!error.is_eof());
let error = CApiError { let error = Error::CApiError {
code: 255.into(), code: 255.into(),
task: ErrorTask::Read, task: ErrorTask::Read,
}; };