From 56a15b7a4161fa476b9fdafcb73ef3a4a05f1c6e Mon Sep 17 00:00:00 2001 From: daniel Date: Tue, 10 Nov 2020 16:21:29 +0100 Subject: [PATCH] code cleanup --- src/errors.rs | 21 +++++++++------------ src/lib.rs | 15 +++++++-------- 2 files changed, 16 insertions(+), 20 deletions(-) diff --git a/src/errors.rs b/src/errors.rs index cf5d1cd..99cb1ba 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -2,6 +2,7 @@ use crate::c_abi; use crate::FileMode; use crate::Frame; use std::path::{Path, PathBuf}; +use std::error::Error as StdError; /// Error type for the xdrfile library #[derive(Debug, Clone, PartialEq)] @@ -21,7 +22,7 @@ pub enum Error { path: PathBuf, mode: FileMode, }, - /// A path could not be converted to &OsStr, probably because it is invalid unicode + /// A path could not be converted to &OsStr InvalidOsStr, /// A path could not be converted to &CStr because it had a null byte NullInStr(std::ffi::NulError), @@ -31,7 +32,7 @@ pub enum Error { impl Error { /// Get the error code returned by the C API, if any pub fn code(&self) -> Option { - use std::error::Error as _; + // use std::error::Error as _; if let Error::CApiError { code, .. } = self { Some(*code) } else if let Some(e) = self.source() { @@ -43,7 +44,7 @@ impl Error { /// Get the task being attempted when the C API returned an error, if any pub fn task(&self) -> Option { - use std::error::Error as _; + // use std::error::Error as _; if let Error::CApiError { task, .. } = self { Some(*task) } else if let Some(e) = self.source() { @@ -76,10 +77,9 @@ impl Error { impl std::error::Error for Error { fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { - use Error::*; match &self { - NullInStr(err) => Some(err), - CouldNotCheckNAtoms(err) => Some(err.as_ref()), + Error::NullInStr(err) => Some(err), + Error::CouldNotCheckNAtoms(err) => Some(err.as_ref()), _ => None, } } @@ -139,8 +139,8 @@ impl std::fmt::Display for Error { } } -#[derive(Debug, Clone, Copy, PartialEq)] /// The task being attempted when the C API returns an error +#[derive(Debug, Clone, Copy, PartialEq)] pub enum ErrorTask { /// The number of atoms was being read from a file ReadNumAtoms, @@ -164,8 +164,8 @@ impl std::fmt::Display for ErrorTask { } } -#[derive(Debug, Clone, PartialEq, Copy)] /// Error codes returned from the C API +#[derive(Debug, Clone, PartialEq, Copy)] pub enum ErrorCode { /// No error, C API returned successfully ExdrOk, @@ -202,10 +202,7 @@ pub enum ErrorCode { impl ErrorCode { /// True if the error is an end of file error, false otherwise pub fn is_eof(&self) -> bool { - match self { - Self::ExdrEndOfFile => true, - _ => false, - } + matches!(self, Self::ExdrEndOfFile) } } diff --git a/src/lib.rs b/src/lib.rs index 66b9f99..2cc3a96 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -101,8 +101,8 @@ impl FileMode { } fn path_to_cstring(path: impl AsRef) -> Result { - let s = path.as_ref().to_str().ok_or_else(|| Error::InvalidOsStr)?; - CString::new(s).map_err(|e| Error::from(e)) + let s = path.as_ref().to_str().ok_or(Error::InvalidOsStr)?; + CString::new(s).map_err(Error::from) } /// A safe wrapper around the c implementation of an XDRFile @@ -135,7 +135,10 @@ impl XDRFile { }) } else { // Something went wrong. But the C api does not tell us what - Err(Error::from((path, filemode))) + Err(Error::CouldNotOpen { + path: path.to_owned(), + mode: filemode + }) } } } @@ -500,11 +503,7 @@ mod tests { let result = xtc_traj.read(&mut frame); if let Err(e) = result { - assert!(if let Error::WrongSizeFrame { .. } = e { - true - } else { - false - }); + assert!(matches!(e, Error::WrongSizeFrame { .. })); } else { panic!("A read with an incorrectly sized frame should not succeed") }