use crate::context::{Context, KeyringFormat};
use crate::error::{self, check, Result};
use crate::ffi;
use crate::ops::Input;
use std::ffi::CString;
use std::ptr;
use super::{Key, KeyIdentifier, LoadSaveFlags};
impl Context {
pub fn find_key(&self, id: KeyIdentifier<'_>) -> Result<Option<Key<'_>>> {
let type_c = CString::new(id.type_str()).unwrap();
let value_c = CString::new(id.value_str()).map_err(|_| error::Error::NulByte)?;
let mut handle: ffi::rnp_key_handle_t = ptr::null_mut();
unsafe {
check(ffi::rnp_locate_key(
self.ffi,
type_c.as_ptr(),
value_c.as_ptr(),
&mut handle,
))?;
}
if handle.is_null() {
Ok(None)
} else {
Ok(Some(Key::from_handle(handle)))
}
}
pub fn load_keys(
&self,
format: KeyringFormat,
bytes: &[u8],
flags: LoadSaveFlags,
) -> Result<()> {
let fmt_c = CString::new(format.as_str()).unwrap();
let input = Input::from_memory(bytes)?;
unsafe {
check(ffi::rnp_load_keys(
self.ffi,
fmt_c.as_ptr(),
input.as_ptr(),
flags.bits(),
))
}
}
}