refact: Make require_attrs_select* error handling regular
(cherry picked from commit de6c6cbd462202405bc787fed02dee249cf16973)
This commit is contained in:
parent
61efb9b79f
commit
c16a9b0595
2 changed files with 56 additions and 32 deletions
|
|
@ -35,17 +35,20 @@ impl Context {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
pub fn clear(&self) {
|
||||
unsafe {
|
||||
raw::set_err_msg(
|
||||
self.inner.as_ptr(),
|
||||
raw::NIX_OK.try_into().unwrap(),
|
||||
b"\0".as_ptr() as *const i8,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn check_err_and_clear(&self) -> Result<()> {
|
||||
let r = self.check_err();
|
||||
if r.is_err() {
|
||||
unsafe {
|
||||
// TODO (https://github.com/NixOS/nix/pull/10910): raw::err_clear
|
||||
raw::set_err_msg(
|
||||
self.inner.as_ptr(),
|
||||
raw::NIX_OK.try_into().unwrap(),
|
||||
b"\0".as_ptr() as *const i8,
|
||||
);
|
||||
}
|
||||
self.clear();
|
||||
}
|
||||
r
|
||||
}
|
||||
|
|
@ -59,6 +62,19 @@ impl Context {
|
|||
Ok(t)
|
||||
}
|
||||
|
||||
pub fn check_one_call_or_key_none<T, F: FnOnce(*mut raw::c_context) -> T>(
|
||||
&self,
|
||||
f: F,
|
||||
) -> Result<Option<T>> {
|
||||
let t = f(self.ptr());
|
||||
if self.is_key_error() {
|
||||
self.clear();
|
||||
return Ok(None);
|
||||
}
|
||||
self.check_err_and_clear()?;
|
||||
Ok(Some(t))
|
||||
}
|
||||
|
||||
/// NIX_ERR_KEY is returned when e.g. an attribute is missing. Return true if the error is of this type.
|
||||
pub fn is_key_error(&self) -> bool {
|
||||
unsafe { raw::err_code(self.inner.as_ptr()) == raw::NIX_ERR_KEY }
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue