mirror of
https://github.com/dnlbauer/xdrfile.git
synced 2026-09-10 22:25:30 +00:00
Cleaned up errors a bit
This commit is contained in:
@@ -18,7 +18,7 @@ pub enum ErrorTask {
|
|||||||
Flush,
|
Flush,
|
||||||
/// A path was being converted to a CString
|
/// A path was being converted to a CString
|
||||||
ToCString,
|
ToCString,
|
||||||
/// Unknown task
|
/// Placeholder until a task can be provided
|
||||||
UnknownTask,
|
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 {
|
pub fn with_task(self, task: ErrorTask) -> Self {
|
||||||
Self {
|
let kind;
|
||||||
kind: self.kind.clone(),
|
let source;
|
||||||
task,
|
|
||||||
source: Some(Box::new(self)),
|
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
|
/// 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 {
|
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 {
|
||||||
write!(f, "{task}: {kind}", task = self.task, kind = self.kind)
|
write!(f, "{task}: {kind}", task = self.task, kind = self.kind)
|
||||||
@@ -178,6 +157,38 @@ pub enum ErrorKind {
|
|||||||
NullInStr(std::ffi::NulError),
|
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 {
|
impl std::fmt::Display for ErrorKind {
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
use ErrorKind::*;
|
use ErrorKind::*;
|
||||||
|
|||||||
Reference in New Issue
Block a user