apple_security/os/macos/
transform.rs

1//! Transform support
2
3use core_foundation::base::{CFType, TCFType};
4use core_foundation::error::CFError;
5use core_foundation::string::CFString;
6use apple_security_sys::transform::*;
7use std::ptr;
8
9declare_TCFType! {
10    /// A type representing a transform.
11    SecTransform, SecTransformRef
12}
13impl_TCFType!(SecTransform, SecTransformRef, SecTransformGetTypeID);
14
15unsafe impl Sync for SecTransform {}
16unsafe impl Send for SecTransform {}
17
18impl SecTransform {
19    /// Sets an attribute of the transform.
20    pub fn set_attribute<T>(&mut self, key: &CFString, value: &T) -> Result<(), CFError>
21    where
22        T: TCFType,
23    {
24        unsafe {
25            let mut error = ptr::null_mut();
26            SecTransformSetAttribute(
27                self.0,
28                key.as_concrete_TypeRef(),
29                value.as_CFTypeRef(),
30                &mut error,
31            );
32            if !error.is_null() {
33                return Err(CFError::wrap_under_create_rule(error));
34            }
35
36            Ok(())
37        }
38    }
39
40    /// Executes the transform.
41    ///
42    /// The return type depends on the type of transform.
43    pub fn execute(&mut self) -> Result<CFType, CFError> {
44        unsafe {
45            let mut error = ptr::null_mut();
46            let result = SecTransformExecute(self.0, &mut error);
47            if result.is_null() {
48                return Err(CFError::wrap_under_create_rule(error));
49            }
50
51            Ok(CFType::wrap_under_create_rule(result))
52        }
53    }
54}