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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
use crate::internal_prelude::*;

/// Representation of a property backed by an i32.
pub struct IntProperty<'a> {
    handle: syz_Handle,
    pub(crate) property: i32,
    _pd: PhantomData<&'a ()>,
}

impl<'a> IntProperty<'a> {
    pub(crate) fn new(handle: &impl ToSyzHandle, property: i32) -> IntProperty {
        IntProperty {
            handle: handle.to_syz_handle(),
            property,
            _pd: Default::default(),
        }
    }

    pub fn get(&self) -> Result<i32> {
        let mut out = 0;
        check_error(unsafe { syz_getI(&mut out as *mut i32, self.handle, self.property) })?;
        return Ok(out);
    }

    pub fn set(&self, value: i32) -> Result<()> {
        check_error(unsafe { syz_setI(self.handle, self.property, value) })
    }
}

/// A property backed by a Synthizer enum.
pub struct EnumProperty<'a, T> {
    pub(crate) iprop: IntProperty<'a>,
    _pd: std::marker::PhantomData<&'a T>,
}

impl<'a, T: I32TransmutableEnum> EnumProperty<'a, T> {
    pub(crate) fn new(handle: &impl ToSyzHandle, property: i32) -> EnumProperty<T> {
        EnumProperty {
            iprop: IntProperty::new(handle, property),
            _pd: Default::default(),
        }
    }

    pub fn get(&self) -> Result<T> {
        let out = self.iprop.get()?;
        Ok(unsafe { T::from_i32(out) })
    }

    pub fn set(&self, value: T) -> Result<()> {
        self.iprop.set(value.as_i32())
    }
}

pub struct BoolProperty<'a> {
    pub(crate) iprop: IntProperty<'a>,
}

impl<'a> BoolProperty<'a> {
    pub(crate) fn new(handle: &impl ToSyzHandle, property: i32) -> BoolProperty {
        BoolProperty {
            iprop: IntProperty::new(handle, property),
        }
    }

    pub fn get(&self) -> Result<bool> {
        Ok(self.iprop.get()? != 0)
    }

    pub fn set(&self, value: bool) -> Result<()> {
        self.iprop.set(value as i32)
    }
}

pub struct DoubleProperty<'a> {
    handle: syz_Handle,
    pub(crate) property: i32,
    _pd: PhantomData<&'a ()>,
}

impl<'a> DoubleProperty<'a> {
    pub(crate) fn new(handle: &impl ToSyzHandle, property: i32) -> DoubleProperty {
        DoubleProperty {
            handle: handle.to_syz_handle(),
            property,
            _pd: Default::default(),
        }
    }

    pub fn get(&self) -> Result<f64> {
        let mut out = 0.0;
        check_error(unsafe { syz_getD(&mut out as *mut f64, self.handle, self.property) })?;
        Ok(out)
    }

    pub fn set(&self, value: f64) -> Result<()> {
        check_error(unsafe { syz_setD(self.handle, self.property, value) })
    }
}

pub struct Double3Property<'a> {
    handle: syz_Handle,
    pub(crate) property: i32,
    _pd: PhantomData<&'a ()>,
}

impl<'a> Double3Property<'a> {
    pub(crate) fn new(handle: &impl ToSyzHandle, property: i32) -> Double3Property {
        Double3Property {
            handle: handle.to_syz_handle(),
            property,
            _pd: Default::default(),
        }
    }

    pub fn get(&self) -> Result<(f64, f64, f64)> {
        let mut o1 = 0.0;
        let mut o2 = 0.0;
        let mut o3 = 0.0;
        check_error(unsafe {
            syz_getD3(
                &mut o1 as *mut f64,
                &mut o2 as *mut f64,
                &mut o3 as *mut f64,
                self.handle,
                self.property,
            )
        })?;
        Ok((o1, o2, o3))
    }

    pub fn set(&self, values: (f64, f64, f64)) -> Result<()> {
        check_error(unsafe { syz_setD3(self.handle, self.property, values.0, values.1, values.2) })
    }
}

pub struct Double6Property<'a> {
    handle: syz_Handle,
    pub(crate) property: i32,
    _pd: PhantomData<&'a ()>,
}

impl<'a> Double6Property<'a> {
    pub(crate) fn new(handle: &impl ToSyzHandle, property: i32) -> Double6Property {
        Double6Property {
            handle: handle.to_syz_handle(),
            property,
            _pd: Default::default(),
        }
    }

    pub fn get(&self) -> Result<(f64, f64, f64, f64, f64, f64)> {
        let mut o1 = 0.0;
        let mut o2 = 0.0;
        let mut o3 = 0.0;
        let mut o4 = 0.0;
        let mut o5 = 0.0;
        let mut o6 = 0.0;
        check_error(unsafe {
            syz_getD6(
                &mut o1 as *mut f64,
                &mut o2 as *mut f64,
                &mut o3 as *mut f64,
                &mut o4 as *mut f64,
                &mut o5 as *mut f64,
                &mut o6 as *mut f64,
                self.handle,
                self.property,
            )
        })?;
        Ok((o1, o2, o3, o4, o5, o6))
    }

    pub fn set(&self, values: (f64, f64, f64, f64, f64, f64)) -> Result<()> {
        check_error(unsafe {
            syz_setD6(
                self.handle,
                self.property,
                values.0,
                values.1,
                values.2,
                values.3,
                values.4,
                values.5,
            )
        })
    }
}

pub struct ObjectProperty<'a> {
    handle: syz_Handle,
    pub(crate) property: i32,
    _pd: PhantomData<&'a ()>,
}

impl<'a> ObjectProperty<'a> {
    pub(crate) fn new(handle: &impl ToSyzHandle, property: i32) -> ObjectProperty {
        ObjectProperty {
            handle: handle.to_syz_handle(),
            property,
            _pd: Default::default(),
        }
    }

    pub fn set(&self, handle: &impl ToSyzHandle) -> Result<()> {
        check_error(unsafe { syz_setO(self.handle, self.property, handle.to_syz_handle()) })
    }
}

/// trait to let clear_properties etc. for automation work on any property.
pub(crate) mod syz_property {
    pub trait SyzProperty {
        fn as_i32(&self) -> i32;
    }
}
pub(crate) use syz_property::*;

impl<'a> SyzProperty for IntProperty<'a> {
    fn as_i32(&self) -> i32 {
        self.property
    }
}

impl<'a> SyzProperty for BoolProperty<'a> {
    fn as_i32(&self) -> i32 {
        self.iprop.as_i32()
    }
}

impl<'a, T> SyzProperty for EnumProperty<'a, T> {
    fn as_i32(&self) -> i32 {
        self.iprop.as_i32()
    }
}

impl<'a> SyzProperty for DoubleProperty<'a> {
    fn as_i32(&self) -> i32 {
        self.property
    }
}

impl<'a> SyzProperty for Double3Property<'a> {
    fn as_i32(&self) -> i32 {
        self.property
    }
}

impl<'a> SyzProperty for Double6Property<'a> {
    fn as_i32(&self) -> i32 {
        self.property
    }
}

impl<'a> SyzProperty for ObjectProperty<'a> {
    fn as_i32(&self) -> i32 {
        self.property
    }
}