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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
// Take a look at the license at the top of the repository in the LICENSE file.

use std::marker::PhantomData;

use crate::{Expression, ParamSpecExpression};

use glib::{
    gobject_ffi, prelude::*, shared::Shared, translate::*, IntoGStr, IntoOptionalGStr, ParamSpec,
    StaticType, Value,
};

impl glib::HasParamSpec for Expression {
    type ParamSpec = ParamSpecExpression;

    type SetValue = Expression;

    type BuilderFn = for<'a> fn(&'a str) -> ParamSpecExpressionBuilder<'a>;

    fn param_spec_builder() -> Self::BuilderFn {
        Self::ParamSpec::builder
    }
}

impl std::fmt::Debug for ParamSpecExpression {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        f.write_str("ParamSpecExpression")
    }
}

impl std::ops::Deref for ParamSpecExpression {
    type Target = ParamSpec;

    #[inline]
    fn deref(&self) -> &Self::Target {
        unsafe { &*(self as *const ParamSpecExpression as *const ParamSpec) }
    }
}

unsafe impl glib::ParamSpecType for ParamSpecExpression {}

#[doc(hidden)]
impl<'a> ToGlibPtr<'a, *const gobject_ffi::GParamSpec> for ParamSpecExpression {
    type Storage = PhantomData<&'a Shared<ffi::GtkParamSpecExpression, ParamSpecExpression>>;

    #[inline]
    fn to_glib_none(&'a self) -> Stash<'a, *const gobject_ffi::GParamSpec, Self> {
        let stash = ToGlibPtr::<*const ffi::GtkParamSpecExpression>::to_glib_none(self);
        Stash(stash.0 as *const _, stash.1)
    }

    #[inline]
    fn to_glib_full(&self) -> *const gobject_ffi::GParamSpec {
        ToGlibPtr::<*const ffi::GtkParamSpecExpression>::to_glib_full(self) as *const _
    }
}

#[doc(hidden)]
impl<'a> ToGlibPtr<'a, *mut gobject_ffi::GParamSpec> for ParamSpecExpression {
    type Storage = PhantomData<&'a Shared<ffi::GtkParamSpecExpression, ParamSpecExpression>>;

    #[inline]
    fn to_glib_none(&'a self) -> Stash<'a, *mut gobject_ffi::GParamSpec, Self> {
        let stash = ToGlibPtr::<*mut ffi::GtkParamSpecExpression>::to_glib_none(self);
        Stash(stash.0 as *mut _, stash.1)
    }

    #[inline]
    fn to_glib_full(&self) -> *mut gobject_ffi::GParamSpec {
        ToGlibPtr::<*mut ffi::GtkParamSpecExpression>::to_glib_full(self) as *mut _
    }
}

#[doc(hidden)]
impl IntoGlibPtr<*mut gobject_ffi::GParamSpec> for ParamSpecExpression {
    #[inline]
    unsafe fn into_glib_ptr(self) -> *mut gobject_ffi::GParamSpec {
        let s = std::mem::ManuallyDrop::new(self);
        s.to_glib_none().0
    }
}

#[doc(hidden)]
impl IntoGlibPtr<*const gobject_ffi::GParamSpec> for ParamSpecExpression {
    #[inline]
    unsafe fn into_glib_ptr(self) -> *const gobject_ffi::GParamSpec {
        let s = std::mem::ManuallyDrop::new(self);
        s.to_glib_none().0
    }
}

#[doc(hidden)]
impl FromGlibPtrFull<*mut gobject_ffi::GParamSpec> for ParamSpecExpression {
    #[inline]
    unsafe fn from_glib_full(ptr: *mut gobject_ffi::GParamSpec) -> Self {
        from_glib_full(ptr as *mut ffi::GtkParamSpecExpression)
    }
}

impl ParamSpecExpression {
    #[allow(clippy::new_ret_no_self)]
    #[doc(alias = "gtk_param_spec_expression")]
    #[deprecated = "Use builder() instead"]
    pub fn new(
        name: impl IntoGStr,
        nick: impl IntoOptionalGStr,
        blurb: impl IntoOptionalGStr,
        flags: glib::ParamFlags,
    ) -> ParamSpec {
        assert_initialized_main_thread!();
        unsafe {
            name.run_with_gstr(|name| {
                nick.run_with_gstr(|nick| {
                    blurb.run_with_gstr(|blurb| {
                        from_glib_none(ffi::gtk_param_spec_expression(
                            name.as_ptr(),
                            nick.to_glib_none().0,
                            blurb.to_glib_none().0,
                            flags.into_glib(),
                        ))
                    })
                })
            })
        }
    }

    // rustdoc-stripper-ignore-next
    /// Creates a new builder-pattern struct instance to construct [`ParamSpecExpression`] objects.
    ///
    /// This method returns an instance of [`ParamSpecExpressionBuilder`](crate::builders::ParamSpecExpressionBuilder) which can be used to create [`ParamSpecExpression`] objects.
    pub fn builder(name: &str) -> ParamSpecExpressionBuilder {
        assert_initialized_main_thread!();
        ParamSpecExpressionBuilder::new(name)
    }

    pub fn upcast(self) -> ParamSpec {
        unsafe {
            from_glib_full(
                IntoGlibPtr::<*mut _>::into_glib_ptr(self) as *mut gobject_ffi::GParamSpec
            )
        }
    }

    pub fn upcast_ref(&self) -> &ParamSpec {
        self
    }
}

#[derive(Default)]
#[must_use]
// rustdoc-stripper-ignore-next
/// A [builder-pattern] type to construct [`ParamSpecExpression`] objects.
///
/// [builder-pattern]: https://doc.rust-lang.org/1.0.0/style/ownership/builders.html
pub struct ParamSpecExpressionBuilder<'a> {
    name: &'a str,
    nick: Option<&'a str>,
    blurb: Option<&'a str>,
    flags: glib::ParamFlags,
}

impl<'a> ParamSpecBuilderExt<'a> for ParamSpecExpressionBuilder<'a> {
    // rustdoc-stripper-ignore-next
    /// Default: `self.name`
    fn set_nick(&mut self, nick: Option<&'a str>) {
        self.nick = nick;
    }

    // rustdoc-stripper-ignore-next
    /// Default: `self.name`
    fn set_blurb(&mut self, blurb: Option<&'a str>) {
        self.blurb = blurb;
    }

    // rustdoc-stripper-ignore-next
    /// Default: `glib::ParamFlags::READWRITE`
    fn set_flags(&mut self, flags: glib::ParamFlags) {
        self.flags = flags;
    }

    // rustdoc-stripper-ignore-next
    /// Implementation detail.
    fn current_flags(&self) -> glib::ParamFlags {
        self.flags
    }
}

impl<'a> ParamSpecExpressionBuilder<'a> {
    fn new(name: &'a str) -> Self {
        assert_initialized_main_thread!();
        Self {
            name,
            ..Default::default()
        }
    }

    #[must_use]
    // rustdoc-stripper-ignore-next
    /// Build the [`ParamSpecExpression`].
    pub fn build(self) -> ParamSpec {
        ParamSpecExpression::new(self.name, self.nick, self.blurb, self.flags)
    }
}

#[doc(hidden)]
impl glib::value::ValueType for ParamSpecExpression {
    type Type = ParamSpecExpression;
}

#[doc(hidden)]
impl glib::value::ValueTypeOptional for ParamSpecExpression {}

#[doc(hidden)]
unsafe impl<'a> glib::value::FromValue<'a> for ParamSpecExpression {
    type Checker = glib::value::GenericValueTypeOrNoneChecker<Self>;

    #[inline]
    unsafe fn from_value(value: &'a Value) -> Self {
        let ptr = gobject_ffi::g_value_dup_param(value.to_glib_none().0);
        debug_assert!(!ptr.is_null());
        from_glib_full(ptr as *mut gobject_ffi::GParamSpec)
    }
}

#[doc(hidden)]
impl glib::value::ToValue for ParamSpecExpression {
    #[inline]
    fn to_value(&self) -> Value {
        unsafe {
            let mut value = Value::for_value_type::<Self>();
            gobject_ffi::g_value_take_param(
                value.to_glib_none_mut().0,
                ToGlibPtr::<*mut _>::to_glib_full(self) as *mut _,
            );
            value
        }
    }

    #[inline]
    fn value_type(&self) -> glib::Type {
        Self::static_type()
    }
}

#[doc(hidden)]
impl glib::value::ToValueOptional for ParamSpecExpression {
    #[inline]
    fn to_value_optional(s: Option<&Self>) -> Value {
        assert_initialized_main_thread!();
        let mut value = Value::for_value_type::<Self>();
        unsafe {
            gobject_ffi::g_value_take_param(
                value.to_glib_none_mut().0,
                ToGlibPtr::<*mut _>::to_glib_full(&s) as *mut _,
            );
        }

        value
    }
}

#[doc(hidden)]
impl From<ParamSpecExpression> for Value {
    fn from(s: ParamSpecExpression) -> Self {
        assert_initialized_main_thread!();
        unsafe {
            let mut value = Value::from_type(ParamSpecExpression::static_type());
            gobject_ffi::g_value_take_param(value.to_glib_none_mut().0, s.into_glib_ptr());
            value
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate as gtk4;

    #[test]
    fn paramspec_expression() {
        let pspec = ParamSpecExpression::new(
            "expression",
            None::<&str>,
            None::<&str>,
            glib::ParamFlags::CONSTRUCT_ONLY | glib::ParamFlags::READABLE,
        );

        let expr_pspec = pspec.downcast::<ParamSpecExpression>();
        assert!(expr_pspec.is_ok());
    }

    #[test]
    fn paramspec_expression_builder() {
        let pspec = ParamSpecExpression::builder("expression")
            .construct_only()
            .read_only()
            .build();

        assert_eq!(
            pspec.flags(),
            glib::ParamFlags::CONSTRUCT_ONLY | glib::ParamFlags::READABLE
        );
    }
}