mirror of
https://github.com/dnlbauer/xdrfile.git
synced 2026-09-10 14:15:31 +00:00
Cleaned up errors a bit
This commit is contained in:
@@ -18,7 +18,7 @@ pub enum ErrorTask {
|
||||
Flush,
|
||||
/// A path was being converted to a CString
|
||||
ToCString,
|
||||
/// Unknown task
|
||||
/// Placeholder until a task can be provided
|
||||
UnknownTask,
|
||||
}
|
||||
|
||||
@@ -74,12 +74,23 @@ impl Error {
|
||||
}
|
||||
}
|
||||
|
||||
/// Change the task of the error
|
||||
///
|
||||
/// Unless the current task is `UnknownTask`, the current error will be
|
||||
/// set as the source
|
||||
pub fn with_task(self, task: ErrorTask) -> Self {
|
||||
Self {
|
||||
kind: self.kind.clone(),
|
||||
task,
|
||||
source: Some(Box::new(self)),
|
||||
}
|
||||
let kind;
|
||||
let source;
|
||||
|
||||
if let ErrorTask::UnknownTask = self.task {
|
||||
kind = self.kind;
|
||||
source = None
|
||||
} else {
|
||||
kind = self.kind.clone();
|
||||
source = Some(Box::new(self))
|
||||
};
|
||||
|
||||
Self { kind, task, source }
|
||||
}
|
||||
|
||||
/// Convert an error code and output value from a C call to a Result
|
||||
@@ -112,38 +123,6 @@ impl<K: Into<ErrorKind>> From<K> for Error {
|
||||
}
|
||||
}
|
||||
|
||||
impl From<std::ffi::NulError> for ErrorKind {
|
||||
fn from(err: std::ffi::NulError) -> Self {
|
||||
Self::NullInStr(err)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<(&Path, FileMode)> for ErrorKind {
|
||||
fn from(value: (&Path, FileMode)) -> Self {
|
||||
let (path, mode) = value;
|
||||
ErrorKind::CouldNotOpen {
|
||||
path: path.to_owned(),
|
||||
mode,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<(&Frame, usize)> for ErrorKind {
|
||||
fn from(value: (&Frame, usize)) -> Self {
|
||||
let (frame, num_atoms) = value;
|
||||
ErrorKind::WrongSizeFrame {
|
||||
expected: num_atoms,
|
||||
found: frame.coords.len(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<ErrorCode> for ErrorKind {
|
||||
fn from(code: ErrorCode) -> Self {
|
||||
ErrorKind::ErrorCode(code)
|
||||
}
|
||||
}
|
||||
|
||||
impl std::fmt::Display for Error {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, "{task}: {kind}", task = self.task, kind = self.kind)
|
||||
@@ -178,6 +157,38 @@ pub enum ErrorKind {
|
||||
NullInStr(std::ffi::NulError),
|
||||
}
|
||||
|
||||
impl From<std::ffi::NulError> for ErrorKind {
|
||||
fn from(err: std::ffi::NulError) -> Self {
|
||||
Self::NullInStr(err)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<(&Path, FileMode)> for ErrorKind {
|
||||
fn from(value: (&Path, FileMode)) -> Self {
|
||||
let (path, mode) = value;
|
||||
ErrorKind::CouldNotOpen {
|
||||
path: path.to_owned(),
|
||||
mode,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<(&Frame, usize)> for ErrorKind {
|
||||
fn from(value: (&Frame, usize)) -> Self {
|
||||
let (frame, num_atoms) = value;
|
||||
ErrorKind::WrongSizeFrame {
|
||||
expected: num_atoms,
|
||||
found: frame.coords.len(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<ErrorCode> for ErrorKind {
|
||||
fn from(code: ErrorCode) -> Self {
|
||||
ErrorKind::ErrorCode(code)
|
||||
}
|
||||
}
|
||||
|
||||
impl std::fmt::Display for ErrorKind {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
use ErrorKind::*;
|
||||
|
||||
Reference in New Issue
Block a user