Skip to main content

ocio_rs/
processor_metadata.rs

1use std::ffi::c_void;
2use std::ptr::NonNull;
3
4use crate::{cstr_from_mut, cstring, OcioError, Result};
5use ocio_sys;
6
7/// Technical metadata collected while OCIO builds a processor.
8///
9/// This tracks source file references and look names that contributed to a
10/// processor. It is distinct from [`crate::FormatMetadata`], which models CLF /
11/// CTF-style metadata trees.
12pub struct ProcessorMetadata {
13    pub(crate) handle: NonNull<c_void>,
14}
15
16/// Shared read-only native processor metadata.
17pub struct ConstProcessorMetadata {
18    pub(crate) handle: NonNull<c_void>,
19}
20impl ConstProcessorMetadata {
21    fn count(&self, looks: bool) -> Result<i32> {
22        crate::clear_last_error();
23        let value =
24            unsafe { ocio_sys::ocio_const_processor_metadata_count(self.handle.as_ptr(), looks) };
25        crate::ocio_call_status()?;
26        Ok(value)
27    }
28    fn entry(&self, looks: bool, index: i32) -> Result<Option<&std::ffi::CStr>> {
29        crate::clear_last_error();
30        let ptr = unsafe {
31            ocio_sys::ocio_const_processor_metadata_entry(self.handle.as_ptr(), looks, index)
32        };
33        crate::ocio_call_status()?;
34        Ok(if ptr.is_null() {
35            None
36        } else {
37            Some(unsafe { std::ffi::CStr::from_ptr(ptr) })
38        })
39    }
40    /// Number of source files.
41    pub fn num_files(&self) -> Result<i32> {
42        self.count(false)
43    }
44    /// Borrow a source file name.
45    pub fn file(&self, index: i32) -> Result<Option<&std::ffi::CStr>> {
46        self.entry(false, index)
47    }
48    /// Number of looks.
49    pub fn num_looks(&self) -> Result<i32> {
50        self.count(true)
51    }
52    /// Borrow a look name.
53    pub fn look(&self, index: i32) -> Result<Option<&std::ffi::CStr>> {
54        self.entry(true, index)
55    }
56    /// Copy entries into an independent editable metadata object.
57    pub fn create_editable_copy(&self) -> Result<ProcessorMetadata> {
58        crate::clear_last_error();
59        let handle = unsafe { ocio_sys::ocio_const_processor_metadata_copy(self.handle.as_ptr()) };
60        crate::handle_result(handle).map(|handle| ProcessorMetadata { handle })
61    }
62}
63impl Drop for ConstProcessorMetadata {
64    fn drop(&mut self) {
65        unsafe { ocio_sys::ocio_const_processor_metadata_destroy(self.handle.as_ptr()) }
66    }
67}
68
69impl ProcessorMetadata {
70    /// Create an empty metadata container.
71    pub fn create() -> Result<Self> {
72        crate::clear_last_error();
73        let handle = unsafe { ocio_sys::ocio_processor_metadata_create() };
74        crate::handle_result(handle).map(|handle| Self { handle })
75    }
76
77    /// Return the number of source files referenced by the processor metadata.
78    pub fn num_files(&self) -> i32 {
79        unsafe { ocio_sys::ocio_processor_metadata_get_num_files(self.handle.as_ptr()) }
80    }
81
82    /// Return one referenced source file by index.
83    pub fn file(&self, index: i32) -> Option<String> {
84        unsafe {
85            cstr_from_mut(ocio_sys::ocio_processor_metadata_get_file(
86                self.handle.as_ptr(),
87                index,
88            ))
89        }
90    }
91
92    /// Return the number of look names referenced by the processor metadata.
93    pub fn num_looks(&self) -> i32 {
94        unsafe { ocio_sys::ocio_processor_metadata_get_num_looks(self.handle.as_ptr()) }
95    }
96
97    /// Return one referenced look name by index.
98    pub fn look(&self, index: i32) -> Option<String> {
99        unsafe {
100            cstr_from_mut(ocio_sys::ocio_processor_metadata_get_look(
101                self.handle.as_ptr(),
102                index,
103            ))
104        }
105    }
106
107    /// Append a file reference to the metadata.
108    pub fn add_file(&self, file_name: impl AsRef<str>) -> Result<()> {
109        let file_name = cstring(file_name)?;
110        crate::clear_last_error();
111        unsafe {
112            ocio_sys::ocio_processor_metadata_add_file(
113                self.handle.as_ptr(),
114                file_name.as_ptr().cast(),
115            )
116        };
117        crate::ocio_call_status()
118    }
119
120    /// Append a look reference to the metadata.
121    pub fn add_look(&self, look: impl AsRef<str>) -> Result<()> {
122        let look = cstring(look)?;
123        crate::clear_last_error();
124        unsafe {
125            ocio_sys::ocio_processor_metadata_add_look(self.handle.as_ptr(), look.as_ptr().cast())
126        };
127        crate::ocio_call_status()
128    }
129}
130
131impl Drop for ProcessorMetadata {
132    fn drop(&mut self) {
133        unsafe { ocio_sys::ocio_processor_metadata_destroy(self.handle.as_ptr()) };
134    }
135}
136
137#[cfg(test)]
138mod tests {
139    use super::*;
140
141    #[test]
142    fn create_processor_metadata() {
143        assert!(ProcessorMetadata::create().is_ok());
144    }
145
146    #[test]
147    fn processor_metadata_mutation_no_crash() {
148        let metadata = ProcessorMetadata::create().unwrap();
149        let _ = metadata.num_files();
150        let _ = metadata.file(0);
151        let _ = metadata.num_looks();
152        let _ = metadata.look(0);
153        assert!(metadata.add_file("lut.clf").is_ok());
154        assert!(metadata.add_look("film").is_ok());
155    }
156}