move check_code to lib

This commit is contained in:
daniel
2020-11-10 17:22:43 +01:00
parent 56a15b7a41
commit 7a22031bee
2 changed files with 63 additions and 27 deletions

View File

@@ -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)