1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
//! Transform support

use core_foundation::base::{CFType, TCFType};
use core_foundation::error::CFError;
use core_foundation::string::CFString;
use security_framework_sys::transform::*;
use std::ptr;

declare_TCFType! {
    /// A type representing a transform.
    SecTransform, SecTransformRef
}
impl_TCFType!(SecTransform, SecTransformRef, SecTransformGetTypeID);

unsafe impl Sync for SecTransform {}
unsafe impl Send for SecTransform {}

impl SecTransform {
    /// Sets an attribute of the transform.
    pub fn set_attribute<T>(&mut self, key: &CFString, value: &T) -> Result<(), CFError>
    where
        T: TCFType,
    {
        unsafe {
            let mut error = ptr::null_mut();
            SecTransformSetAttribute(
                self.0,
                key.as_concrete_TypeRef(),
                value.as_CFTypeRef(),
                &mut error,
            );
            if !error.is_null() {
                return Err(CFError::wrap_under_create_rule(error));
            }

            Ok(())
        }
    }

    /// Executes the transform.
    ///
    /// The return type depends on the type of transform.
    pub fn execute(&mut self) -> Result<CFType, CFError> {
        unsafe {
            let mut error = ptr::null_mut();
            let result = SecTransformExecute(self.0, &mut error);
            if result.is_null() {
                return Err(CFError::wrap_under_create_rule(error));
            }

            Ok(CFType::wrap_under_create_rule(result))
        }
    }
}