use crate::error::{self, check, Result};
use crate::ffi;
use std::ffi::CString;
use std::ptr;
pub struct Input {
handle: ffi::rnp_input_t,
}
impl Input {
pub fn from_memory(bytes: &[u8]) -> Result<Self> {
let mut handle: ffi::rnp_input_t = ptr::null_mut();
unsafe {
check(ffi::rnp_input_from_memory(
&mut handle,
bytes.as_ptr(),
bytes.len(),
true, ))?;
}
if handle.is_null() {
return Err(error::Error::NullPointer);
}
Ok(Input { handle })
}
pub fn from_path(path: &str) -> Result<Self> {
let c = CString::new(path).map_err(|_| error::Error::PathNul)?;
let mut handle: ffi::rnp_input_t = ptr::null_mut();
unsafe {
check(ffi::rnp_input_from_path(&mut handle, c.as_ptr()))?;
}
if handle.is_null() {
return Err(error::Error::NullPointer);
}
Ok(Input { handle })
}
pub fn from_stdin() -> Result<Self> {
let mut handle: ffi::rnp_input_t = ptr::null_mut();
unsafe {
check(ffi::rnp_input_from_stdin(&mut handle))?;
}
if handle.is_null() {
return Err(error::Error::NullPointer);
}
Ok(Input { handle })
}
pub(crate) fn as_ptr(&self) -> ffi::rnp_input_t {
self.handle
}
}
impl Drop for Input {
fn drop(&mut self) {
if !self.handle.is_null() {
unsafe {
let _ = ffi::rnp_input_destroy(self.handle);
}
self.handle = ptr::null_mut();
}
}
}