use std::fmt::{self, Debug};
use std::ops::Deref;
use hdf5_sys::h5p::{H5Pcreate, H5Pget_obj_track_times, H5Pset_obj_track_times};
use crate::globals::H5P_GROUP_CREATE;
use crate::internal_prelude::*;
#[repr(transparent)]
pub struct GroupCreate(Handle);
impl ObjectClass for GroupCreate {
const NAME: &'static str = "group create property list";
const VALID_TYPES: &'static [H5I_type_t] = &[H5I_GENPROP_LST];
fn from_handle(handle: Handle) -> Self {
Self(handle)
}
fn handle(&self) -> &Handle {
&self.0
}
fn validate(&self) -> Result<()> {
ensure!(
self.is_class(PropertyListClass::GroupCreate),
"expected group create property list, got {:?}",
self.class()
);
Ok(())
}
}
impl Clone for GroupCreate {
fn clone(&self) -> Self {
unsafe { self.deref().clone().cast_unchecked() }
}
}
impl Debug for GroupCreate {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let mut formatter = f.debug_struct("GroupCreate");
formatter.field("obj_track_times", &self.obj_track_times());
formatter.finish()
}
}
impl Deref for GroupCreate {
type Target = PropertyList;
fn deref(&self) -> &PropertyList {
unsafe { self.transmute() }
}
}
impl PartialEq for GroupCreate {
fn eq(&self, other: &Self) -> bool {
<PropertyList as PartialEq>::eq(self, other)
}
}
impl Eq for GroupCreate {}
#[derive(Clone, Debug, Default)]
pub struct GroupCreateBuilder {
obj_track_times: Option<bool>,
}
impl GroupCreateBuilder {
pub fn new() -> Self {
Self::default()
}
pub fn from_plist(plist: &GroupCreate) -> Result<Self> {
let mut builder = Self::default();
builder.obj_track_times(plist.get_obj_track_times()?);
Ok(builder)
}
pub fn obj_track_times(&mut self, track_times: bool) -> &mut Self {
self.obj_track_times = Some(track_times);
self
}
fn populate_plist(&self, id: hid_t) -> Result<()> {
if let Some(v) = self.obj_track_times {
h5try!(H5Pset_obj_track_times(id, hbool_t::from(v)));
}
Ok(())
}
pub fn apply(&self, plist: &mut GroupCreate) -> Result<()> {
h5lock!(self.populate_plist(plist.id()))
}
pub fn finish(&self) -> Result<GroupCreate> {
h5lock!({
let mut plist = GroupCreate::try_new()?;
self.apply(&mut plist).map(|()| plist)
})
}
}
impl GroupCreate {
pub fn try_new() -> Result<Self> {
Self::from_id(h5try!(H5Pcreate(*H5P_GROUP_CREATE)))
}
pub fn copy(&self) -> Self {
unsafe { self.deref().copy().cast_unchecked() }
}
pub fn build() -> GroupCreateBuilder {
GroupCreateBuilder::new()
}
#[doc(hidden)]
pub fn get_obj_track_times(&self) -> Result<bool> {
h5get!(H5Pget_obj_track_times(self.id()): hbool_t).map(|x| x > 0)
}
pub fn obj_track_times(&self) -> bool {
self.get_obj_track_times().unwrap_or(true)
}
}