Skip to main content

libobs_wrapper\data\properties\types/
number.rs

1use getters0::Getters;
2
3use crate::data::properties::ObsNumberType;
4
5#[derive(Debug, Getters, Clone)]
6#[skip_new]
7pub struct ObsNumberProperty<T>
8where
9    T: Clone + Copy + std::fmt::Debug,
10{
11    name: String,
12    description: Option<String>,
13    min: T,
14    max: T,
15    step: T,
16    suffix: String,
17    number_type: ObsNumberType,
18}
19
20macro_rules! impl_from_property {
21    ($n_type: ident, $obs_number_name: ident) => {
22        paste::paste! {
23            impl TryFrom<super::PropertyCreationInfo> for ObsNumberProperty<[<$n_type>]> {
24                type Error = crate::utils::ObsError;
25
26                fn try_from(
27                    crate::data::properties::PropertyCreationInfo {
28                        name,
29                        description,
30                        pointer,
31                        runtime,
32                    }: crate::data::properties::PropertyCreationInfo,
33                ) -> Result<Self, Self::Error> {
34                    $crate::run_with_obs!(runtime, (pointer), move || {
35                        use crate::data::properties::ObsNumberType;
36
37                        let min = unsafe {
38                            // SAFETY: PropertyCreationInfo borrows an unchanged parent tree
39                            // retained by
40                            // property_ptr_to_struct throughout this inline actor conversion.
41                            libobs::[<obs_property_ $obs_number_name _min>](pointer.0)
42                        };
43
44                        let max = unsafe {
45                            // SAFETY: PropertyCreationInfo borrows an unchanged parent tree
46                            // retained by
47                            // property_ptr_to_struct throughout this inline actor conversion.
48                            libobs::[<obs_property_ $obs_number_name _max>](pointer.0)
49                        };
50
51                        let step = unsafe {
52                            // SAFETY: PropertyCreationInfo borrows an unchanged parent tree
53                            // retained by
54                            // property_ptr_to_struct throughout this inline actor conversion.
55                            libobs::[<obs_property_ $obs_number_name _step>](pointer.0)
56                        };
57
58                        let suffix = unsafe {
59                            // SAFETY: PropertyCreationInfo borrows an unchanged parent tree
60                            // retained by
61                            // property_ptr_to_struct throughout this inline actor conversion.
62                            libobs::[<obs_property_ $obs_number_name _suffix>](pointer.0)
63                        };
64
65                        let suffix = if suffix.is_null() {
66                            String::new()
67                        } else {
68                            let suffix = unsafe {
69                                // SAFETY: The getter returns a tree-owned NUL-terminated string,
70                                // copied
71                                // while the parent tree is still retained and unchanged on the actor.
72                                std::ffi::CStr::from_ptr(suffix)
73                            };
74
75                            let suffix = suffix.to_str().unwrap_or_default();
76                            suffix.to_string()
77                        };
78
79                        let number_type = unsafe {
80                            // SAFETY: PropertyCreationInfo borrows an unchanged parent tree
81                            // retained by
82                            // property_ptr_to_struct throughout this inline actor conversion.
83                            libobs::[<obs_property_ $obs_number_name _type >](pointer.0)
84                        };
85
86                        let number_type = crate::macros::enum_from_number!(ObsNumberType, number_type)
87                            .ok_or_else(|| crate::utils::ObsError::EnumConversionError(format!(
88                                "ObsNumberType for property {}",
89                                name
90                            )))?;
91
92                        Ok(ObsNumberProperty {
93                            name,
94                            description,
95                            min,
96                            max,
97                            step,
98                            suffix,
99                            number_type,
100                        })
101                    })?
102                }
103            }
104        }
105    };
106}
107
108impl_from_property!(i32, int);
109impl_from_property!(f64, float);