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: The caller must have ensured that the pointer is valid
39                            libobs::[<obs_property_ $obs_number_name _min>](pointer.0)
40                        };
41
42                        let max = unsafe {
43                            // Safety: The caller must have ensured that the pointer is valid
44                            libobs::[<obs_property_ $obs_number_name _max>](pointer.0)
45                        };
46
47                        let step = unsafe {
48                            // Safety: The caller must have ensured that the pointer is valid
49                            libobs::[<obs_property_ $obs_number_name _step>](pointer.0)
50                        };
51
52                        let suffix = unsafe {
53                            // Safety: The caller must have ensured that the pointer is valid
54                            libobs::[<obs_property_ $obs_number_name _suffix>](pointer.0)
55                        };
56
57                        let suffix = if suffix.is_null() {
58                            String::new()
59                        } else {
60                            let suffix = unsafe {
61                                // Safety: Safe because of we did a null check
62                                std::ffi::CStr::from_ptr(suffix)
63                            };
64
65                            let suffix = suffix.to_str().unwrap_or_default();
66                            suffix.to_string()
67                        };
68
69                        let number_type = unsafe {
70                            // Safety: The caller must have ensured that the pointer is valid
71                            libobs::[<obs_property_ $obs_number_name _type >](pointer.0)
72                        };
73
74                        let number_type = crate::macros::enum_from_number!(ObsNumberType, number_type);
75
76                        if number_type.is_none() {
77                            return Err(crate::utils::ObsError::EnumConversionError(format!(
78                                "ObsNumberType for property {}",
79                                name
80                            )));
81                        }
82
83                        Ok(ObsNumberProperty {
84                            name,
85                            description,
86                            min,
87                            max,
88                            step,
89                            suffix,
90                            number_type: number_type.unwrap(),
91                        })
92                    })?
93                }
94            }
95        }
96    };
97}
98
99impl_from_property!(i32, int);
100impl_from_property!(f64, float);