Skip to main content

systemconfiguration/
property_list.rs

1use crate::{bridge, error::Result, ffi, SystemConfigurationError};
2
3#[derive(Clone, Debug)]
4/// Wraps `CFPropertyListRef` values used by SystemConfiguration APIs.
5pub struct PropertyList {
6    raw: bridge::OwnedHandle,
7}
8
9impl PropertyList {
10    /// Wraps `SCPropertyListFromString`.
11    pub fn from_string(value: &str) -> Result<Self> {
12        let value = bridge::cstring(value, "sc_property_list_from_string")?;
13        let raw = unsafe { ffi::core::sc_property_list_from_string(value.as_ptr()) };
14        let raw = bridge::owned_handle_or_last("sc_property_list_from_string", raw)?;
15        Ok(Self { raw })
16    }
17
18    /// Wraps `SCPropertyListFromBool`.
19    pub fn from_bool(value: bool) -> Self {
20        let raw = unsafe { ffi::core::sc_property_list_from_bool(u8::from(value)) };
21        let raw = bridge::owned_handle_or_last("sc_property_list_from_bool", raw)
22            .expect("sc_property_list_from_bool returned null");
23        Self { raw }
24    }
25
26    /// Wraps `SCPropertyListFromJSON`.
27    pub fn from_json(value: &str) -> Result<Self> {
28        let value = bridge::cstring(value, "sc_property_list_from_json")?;
29        let raw = unsafe { ffi::core::sc_property_list_from_json(value.as_ptr()) };
30        let raw = bridge::owned_handle_or_last("sc_property_list_from_json", raw)?;
31        Ok(Self { raw })
32    }
33
34    /// Wraps `SCPropertyListCopyDescription`.
35    pub fn description(&self) -> Result<String> {
36        let raw = unsafe { ffi::core::sc_property_list_copy_description(self.raw.as_ptr()) };
37        bridge::take_optional_string(raw).ok_or_else(|| {
38            SystemConfigurationError::null(
39                "sc_property_list_copy_description",
40                "bridge returned null property-list description",
41            )
42        })
43    }
44
45    pub(crate) fn as_ptr(&self) -> bridge::RawHandle {
46        self.raw.as_ptr()
47    }
48
49    pub(crate) fn from_owned_handle(raw: bridge::OwnedHandle) -> Self {
50        Self { raw }
51    }
52}