mirror of
https://github.com/dnlbauer/xdrfile.git
synced 2026-09-10 22:25:30 +00:00
move check_code to lib
This commit is contained in:
@@ -58,21 +58,6 @@ impl Error {
|
||||
pub fn is_eof(&self) -> bool {
|
||||
self.code().map_or(false, |e| e.is_eof())
|
||||
}
|
||||
|
||||
/// Convert an error code and output value from a C call to a Result
|
||||
///
|
||||
/// `code` should be an integer return code returned from the C API. `value` should be the
|
||||
/// function's output, which is generally either `()` or one of its arguments. If `code`
|
||||
/// indicates the function returned successfully, the value is returned; otherwise, the
|
||||
/// code is converted into the appropriate `Error`.
|
||||
pub fn check_code<T>(code: impl Into<ErrorCode>, value: T, task: ErrorTask) -> Result<T, Self> {
|
||||
let code: ErrorCode = code.into();
|
||||
if let ErrorCode::ExdrOk = code {
|
||||
Ok(value)
|
||||
} else {
|
||||
Err(Self::CApiError { code, task })
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl std::error::Error for Error {
|
||||
@@ -85,6 +70,13 @@ impl std::error::Error for Error {
|
||||
}
|
||||
}
|
||||
|
||||
impl From<(ErrorCode, ErrorTask)> for Error {
|
||||
fn from(value: (ErrorCode, ErrorTask)) -> Self {
|
||||
let (code, task) = value;
|
||||
Self::CApiError { code, task }
|
||||
}
|
||||
}
|
||||
|
||||
impl From<std::ffi::NulError> for Error {
|
||||
fn from(err: std::ffi::NulError) -> Self {
|
||||
Self::NullInStr(err)
|
||||
|
||||
Reference in New Issue
Block a user