You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

39 lines
731 B

use std::{ffi::CStr, os::raw::c_char};
#[derive(Debug)]
pub struct ErrNoProperty {
name: String,
}
impl std::fmt::Display for ErrNoProperty {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{} not available", self.name)
}
}
impl std::error::Error for ErrNoProperty {}
pub fn ensure_properties<T, F>(
props: &[T],
names: &[*const c_char],
func: F,
) -> Result<(), ErrNoProperty>
where
F: Fn(&T) -> *const c_char,
{
for name in names {
let name = unsafe { CStr::from_ptr(*name) };
let any =
props.iter().any(|x| unsafe { CStr::from_ptr(func(x)) == name });
if !any {
return Err(ErrNoProperty {
name: name.to_string_lossy().to_string(),
});
}
}
Ok(())
}
// EOF