mirror of
https://github.com/dnlbauer/xdrfile.git
synced 2026-09-10 22:25:30 +00:00
Refactored ErrorCode as Error.source()
This commit is contained in:
@@ -86,46 +86,34 @@ impl From<std::ffi::NulError> 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 ErrorTask::*;
|
use ErrorTask::*;
|
||||||
match (&self.task, &self.code) {
|
match &self.task {
|
||||||
(OpenFile(path, mode), _) => write!(
|
OpenFile(path, mode) => write!(
|
||||||
f,
|
f,
|
||||||
"Failed to open file at {path:?} with mode {mode:?}",
|
"Failed to open file at {path:?} with mode {mode:?}",
|
||||||
path = path,
|
path = path,
|
||||||
mode = mode
|
mode = mode
|
||||||
),
|
),
|
||||||
(ReadNumAtoms, Some(code)) => write!(
|
ReadNumAtoms => write!(f, "Failed to read atom number from trajectory"),
|
||||||
f,
|
Read => write!(f, "Failed to read trajectory"),
|
||||||
"Failed to read atom number from trajectory: C API returned error code {}",
|
Write => write!(f, "Failed to write trajectory"),
|
||||||
code
|
Flush => write!(f, "Failed to flush trajectory"),
|
||||||
),
|
ToCString(_) => write!(
|
||||||
(ReadNumAtoms, None) => write!(f, "Failed to read atom number from trajectory"),
|
|
||||||
(Read, Some(code)) => write!(
|
|
||||||
f,
|
|
||||||
"Failed to read trajectory: C API returned error code {}",
|
|
||||||
code
|
|
||||||
),
|
|
||||||
(Read, None) => write!(f, "Failed to read trajectory"),
|
|
||||||
(Write, Some(code)) => write!(
|
|
||||||
f,
|
|
||||||
"Failed to write trajectory: C API returned error code {}",
|
|
||||||
code
|
|
||||||
),
|
|
||||||
(Write, None) => write!(f, "Failed to write trajectory"),
|
|
||||||
(Flush, Some(code)) => write!(
|
|
||||||
f,
|
|
||||||
"Failed to flush trajectory: C API returned error code {}",
|
|
||||||
code
|
|
||||||
),
|
|
||||||
(ToCString(_), _) => write!(
|
|
||||||
f,
|
f,
|
||||||
"Path cannot be converted to a C string because it has a null byte"
|
"Path cannot be converted to a C string because it has a null byte"
|
||||||
),
|
),
|
||||||
(Flush, None) => write!(f, "Failed to flush trajectory"),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl std::error::Error for Error {}
|
impl std::error::Error for Error {
|
||||||
|
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
|
||||||
|
if let Some(e) = &self.code {
|
||||||
|
Some(e)
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq)]
|
#[derive(Debug, Clone, PartialEq)]
|
||||||
pub enum ErrorCode {
|
pub enum ErrorCode {
|
||||||
@@ -180,13 +168,14 @@ impl From<c_abi::xdrfile::BindgenTy1> for ErrorCode {
|
|||||||
impl std::fmt::Display for ErrorCode {
|
impl std::fmt::Display for ErrorCode {
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
if let Self::UnmatchedCode(i) = self {
|
if let Self::UnmatchedCode(i) = self {
|
||||||
write!(f, "{}", i)
|
write!(f, "C API returned error code {}", i)
|
||||||
} else {
|
} else {
|
||||||
write!(f, "{:?}", self)
|
write!(f, "C API returned error code {:?}", self)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl std::error::Error for ErrorCode {}
|
||||||
pub type Result<T, E = Error> = std::result::Result<T, E>;
|
pub type Result<T, E = Error> = std::result::Result<T, E>;
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
|||||||
Reference in New Issue
Block a user