mirror of
https://github.com/dnlbauer/xdrfile.git
synced 2026-09-10 14:15:31 +00:00
code cleanup
This commit is contained in:
@@ -2,6 +2,7 @@ use crate::c_abi;
|
|||||||
use crate::FileMode;
|
use crate::FileMode;
|
||||||
use crate::Frame;
|
use crate::Frame;
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
|
use std::error::Error as StdError;
|
||||||
|
|
||||||
/// Error type for the xdrfile library
|
/// Error type for the xdrfile library
|
||||||
#[derive(Debug, Clone, PartialEq)]
|
#[derive(Debug, Clone, PartialEq)]
|
||||||
@@ -21,7 +22,7 @@ pub enum Error {
|
|||||||
path: PathBuf,
|
path: PathBuf,
|
||||||
mode: FileMode,
|
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,
|
InvalidOsStr,
|
||||||
/// A path could not be converted to &CStr because it had a null byte
|
/// A path could not be converted to &CStr because it had a null byte
|
||||||
NullInStr(std::ffi::NulError),
|
NullInStr(std::ffi::NulError),
|
||||||
@@ -31,7 +32,7 @@ pub enum Error {
|
|||||||
impl Error {
|
impl Error {
|
||||||
/// Get the error code returned by the C API, if any
|
/// Get the error code returned by the C API, if any
|
||||||
pub fn code(&self) -> Option<ErrorCode> {
|
pub fn code(&self) -> Option<ErrorCode> {
|
||||||
use std::error::Error as _;
|
// use std::error::Error as _;
|
||||||
if let Error::CApiError { code, .. } = self {
|
if let Error::CApiError { code, .. } = self {
|
||||||
Some(*code)
|
Some(*code)
|
||||||
} else if let Some(e) = self.source() {
|
} 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
|
/// Get the task being attempted when the C API returned an error, if any
|
||||||
pub fn task(&self) -> Option<ErrorTask> {
|
pub fn task(&self) -> Option<ErrorTask> {
|
||||||
use std::error::Error as _;
|
// use std::error::Error as _;
|
||||||
if let Error::CApiError { task, .. } = self {
|
if let Error::CApiError { task, .. } = self {
|
||||||
Some(*task)
|
Some(*task)
|
||||||
} else if let Some(e) = self.source() {
|
} else if let Some(e) = self.source() {
|
||||||
@@ -76,10 +77,9 @@ 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 {
|
||||||
NullInStr(err) => Some(err),
|
Error::NullInStr(err) => Some(err),
|
||||||
CouldNotCheckNAtoms(err) => Some(err.as_ref()),
|
Error::CouldNotCheckNAtoms(err) => Some(err.as_ref()),
|
||||||
_ => None,
|
_ => 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
|
/// The task being attempted when the C API returns an error
|
||||||
|
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||||
pub enum ErrorTask {
|
pub enum ErrorTask {
|
||||||
/// The number of atoms was being read from a file
|
/// The number of atoms was being read from a file
|
||||||
ReadNumAtoms,
|
ReadNumAtoms,
|
||||||
@@ -164,8 +164,8 @@ impl std::fmt::Display for ErrorTask {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq, Copy)]
|
|
||||||
/// Error codes returned from the C API
|
/// Error codes returned from the C API
|
||||||
|
#[derive(Debug, Clone, PartialEq, Copy)]
|
||||||
pub enum ErrorCode {
|
pub enum ErrorCode {
|
||||||
/// No error, C API returned successfully
|
/// No error, C API returned successfully
|
||||||
ExdrOk,
|
ExdrOk,
|
||||||
@@ -202,10 +202,7 @@ pub enum ErrorCode {
|
|||||||
impl ErrorCode {
|
impl ErrorCode {
|
||||||
/// True if the error is an end of file error, false otherwise
|
/// True if the error is an end of file error, false otherwise
|
||||||
pub fn is_eof(&self) -> bool {
|
pub fn is_eof(&self) -> bool {
|
||||||
match self {
|
matches!(self, Self::ExdrEndOfFile)
|
||||||
Self::ExdrEndOfFile => true,
|
|
||||||
_ => false,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
15
src/lib.rs
15
src/lib.rs
@@ -101,8 +101,8 @@ impl FileMode {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn path_to_cstring(path: impl AsRef<Path>) -> Result<CString> {
|
fn path_to_cstring(path: impl AsRef<Path>) -> Result<CString> {
|
||||||
let s = path.as_ref().to_str().ok_or_else(|| Error::InvalidOsStr)?;
|
let s = path.as_ref().to_str().ok_or(Error::InvalidOsStr)?;
|
||||||
CString::new(s).map_err(|e| Error::from(e))
|
CString::new(s).map_err(Error::from)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A safe wrapper around the c implementation of an XDRFile
|
/// A safe wrapper around the c implementation of an XDRFile
|
||||||
@@ -135,7 +135,10 @@ impl XDRFile {
|
|||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
// Something went wrong. But the C api does not tell us what
|
// 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);
|
let result = xtc_traj.read(&mut frame);
|
||||||
if let Err(e) = result {
|
if let Err(e) = result {
|
||||||
assert!(if let Error::WrongSizeFrame { .. } = e {
|
assert!(matches!(e, Error::WrongSizeFrame { .. }));
|
||||||
true
|
|
||||||
} else {
|
|
||||||
false
|
|
||||||
});
|
|
||||||
} else {
|
} else {
|
||||||
panic!("A read with an incorrectly sized frame should not succeed")
|
panic!("A read with an incorrectly sized frame should not succeed")
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user