glib/
match_info.rs

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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
// Take a look at the license at the top of the repository in the LICENSE file.

use crate::{ffi, prelude::*, translate::*, GStr, Regex};
use std::{marker::PhantomData, mem, ptr};

/// A GMatchInfo is an opaque struct used to return information about
/// matches.
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(transparent)]
pub struct MatchInfo<'input> {
    inner: std::ptr::NonNull<ffi::GMatchInfo>,
    _phantom: PhantomData<&'input GStr>,
}

impl Clone for MatchInfo<'_> {
    fn clone(&self) -> Self {
        unsafe {
            ffi::g_match_info_ref(self.inner.as_ptr());
        }
        Self {
            inner: self.inner,
            _phantom: PhantomData,
        }
    }
}

impl Drop for MatchInfo<'_> {
    fn drop(&mut self) {
        unsafe {
            ffi::g_match_info_unref(self.inner.as_ptr());
        }
    }
}

impl MatchInfo<'_> {
    #[doc = "Return the inner pointer to the underlying C value."]
    #[inline]
    pub fn as_ptr(&self) -> *mut ffi::GMatchInfo {
        self.inner.as_ptr()
    }
    #[doc = "Borrows the underlying C value."]
    #[inline]
    pub unsafe fn from_glib_ptr_borrow(ptr: &*mut ffi::GMatchInfo) -> &Self {
        debug_assert_eq!(
            std::mem::size_of::<Self>(),
            std::mem::size_of::<crate::ffi::gpointer>()
        );
        debug_assert!(!ptr.is_null());
        &*(ptr as *const *mut ffi::GMatchInfo as *const Self)
    }
}

#[doc(hidden)]
impl<'input> GlibPtrDefault for MatchInfo<'input> {
    type GlibType = *mut ffi::GMatchInfo;
}
#[doc(hidden)]
unsafe impl<'input> TransparentPtrType for MatchInfo<'input> {}

#[doc(hidden)]
impl<'a, 'input> ToGlibPtr<'a, *mut ffi::GMatchInfo> for MatchInfo<'input>
where
    'input: 'a,
{
    type Storage = PhantomData<&'a Self>;
    #[inline]
    fn to_glib_none(&'a self) -> Stash<'a, *mut ffi::GMatchInfo, Self> {
        Stash(self.inner.as_ptr(), PhantomData)
    }
    #[inline]
    fn to_glib_full(&self) -> *mut ffi::GMatchInfo {
        let ptr = self.inner.as_ptr();
        unsafe {
            ffi::g_match_info_ref(ptr);
        }
        ptr
    }
}
#[doc(hidden)]
impl<'a, 'input> ToGlibPtr<'a, *const ffi::GMatchInfo> for MatchInfo<'input>
where
    'input: 'a,
{
    type Storage = PhantomData<&'a Self>;
    #[inline]
    fn to_glib_none(&'a self) -> Stash<'a, *const ffi::GMatchInfo, Self> {
        Stash(self.inner.as_ptr(), PhantomData)
    }
    #[inline]
    fn to_glib_full(&self) -> *const ffi::GMatchInfo {
        let ptr = self.inner.as_ptr();
        unsafe {
            ffi::g_match_info_ref(ptr);
        }
        ptr
    }
}

#[doc(hidden)]
impl<'input> FromGlibPtrNone<*mut ffi::GMatchInfo> for MatchInfo<'input> {
    #[inline]
    unsafe fn from_glib_none(ptr: *mut ffi::GMatchInfo) -> Self {
        debug_assert!(!ptr.is_null());
        unsafe {
            ffi::g_match_info_ref(ptr);
            Self {
                inner: ptr::NonNull::new_unchecked(ptr),
                _phantom: PhantomData,
            }
        }
    }
}
#[doc(hidden)]
impl<'input> FromGlibPtrNone<*const ffi::GMatchInfo> for MatchInfo<'input> {
    #[inline]
    unsafe fn from_glib_none(ptr: *const ffi::GMatchInfo) -> Self {
        Self::from_glib_none(ptr.cast_mut())
    }
}
#[doc(hidden)]
impl<'input> FromGlibPtrFull<*mut ffi::GMatchInfo> for MatchInfo<'input> {
    #[inline]
    unsafe fn from_glib_full(ptr: *mut ffi::GMatchInfo) -> Self {
        debug_assert!(!ptr.is_null());
        unsafe {
            Self {
                inner: ptr::NonNull::new_unchecked(ptr),
                _phantom: PhantomData,
            }
        }
    }
}
#[doc(hidden)]
impl<'input> FromGlibPtrBorrow<*mut ffi::GMatchInfo> for MatchInfo<'input> {
    #[inline]
    unsafe fn from_glib_borrow(ptr: *mut ffi::GMatchInfo) -> Borrowed<Self> {
        debug_assert!(!ptr.is_null());
        unsafe {
            Borrowed::new(Self {
                inner: ptr::NonNull::new_unchecked(ptr),
                _phantom: PhantomData,
            })
        }
    }
}
#[doc(hidden)]
impl<'input> FromGlibPtrBorrow<*const ffi::GMatchInfo> for MatchInfo<'input> {
    #[inline]
    unsafe fn from_glib_borrow(ptr: *const ffi::GMatchInfo) -> Borrowed<Self> {
        from_glib_borrow::<_, Self>(ptr.cast_mut())
    }
}

#[doc(hidden)]
impl<'input> IntoGlibPtr<*mut ffi::GMatchInfo> for MatchInfo<'input> {
    #[inline]
    unsafe fn into_glib_ptr(self) -> *mut ffi::GMatchInfo {
        let s = std::mem::ManuallyDrop::new(self);
        ToGlibPtr::<*const ffi::GMatchInfo>::to_glib_none(&*s).0 as *mut _
    }
}
#[doc(hidden)]
impl<'input> IntoGlibPtr<*const ffi::GMatchInfo> for MatchInfo<'input> {
    #[inline]
    unsafe fn into_glib_ptr(self) -> *const ffi::GMatchInfo {
        let s = std::mem::ManuallyDrop::new(self);
        ToGlibPtr::<*const ffi::GMatchInfo>::to_glib_none(&*s).0 as *const _
    }
}
impl<'input> StaticType for MatchInfo<'input> {
    #[inline]
    fn static_type() -> crate::types::Type {
        unsafe { from_glib(ffi::g_match_info_get_type()) }
    }
}

#[doc(hidden)]
impl ValueType for MatchInfo<'static> {
    type Type = Self;
}

#[doc(hidden)]
impl crate::value::ValueTypeOptional for MatchInfo<'static> {}

unsafe impl<'a, 'input: 'a> crate::value::FromValue<'a> for MatchInfo<'input> {
    type Checker = crate::value::GenericValueTypeOrNoneChecker<Self>;

    unsafe fn from_value(value: &'a crate::Value) -> Self {
        let ptr = crate::gobject_ffi::g_value_dup_boxed(
            crate::translate::ToGlibPtr::to_glib_none(value).0,
        );
        debug_assert!(!ptr.is_null());
        <Self as crate::translate::FromGlibPtrFull<*mut ffi::GMatchInfo>>::from_glib_full(
            ptr as *mut ffi::GMatchInfo,
        )
    }
}
#[doc(hidden)]
unsafe impl<'a, 'input: 'a> crate::value::FromValue<'a> for &'a MatchInfo<'input> {
    type Checker = crate::value::GenericValueTypeOrNoneChecker<Self>;

    #[inline]
    unsafe fn from_value(value: &'a crate::Value) -> Self {
        let value = &*(value as *const crate::Value as *const crate::gobject_ffi::GValue);
        <MatchInfo<'input>>::from_glib_ptr_borrow(
            &*(&value.data[0].v_pointer as *const crate::ffi::gpointer
                as *const *mut ffi::GMatchInfo),
        )
    }
}
impl ToValue for MatchInfo<'static> {
    #[inline]
    fn to_value(&self) -> crate::Value {
        unsafe {
            let mut value = crate::Value::from_type_unchecked(<Self as StaticType>::static_type());
            crate::gobject_ffi::g_value_take_boxed(
                crate::translate::ToGlibPtrMut::to_glib_none_mut(&mut value).0,
                crate::translate::ToGlibPtr::<*mut ffi::GMatchInfo>::to_glib_full(self) as *mut _,
            );
            value
        }
    }

    #[inline]
    fn value_type(&self) -> crate::Type {
        <Self as StaticType>::static_type()
    }
}

impl From<MatchInfo<'static>> for crate::Value {
    #[inline]
    fn from(s: MatchInfo<'static>) -> Self {
        unsafe {
            let mut value = crate::Value::from_type_unchecked(
                <MatchInfo<'static> as StaticType>::static_type(),
            );
            crate::gobject_ffi::g_value_take_boxed(
                crate::translate::ToGlibPtrMut::to_glib_none_mut(&mut value).0,
                crate::translate::IntoGlibPtr::<*mut ffi::GMatchInfo>::into_glib_ptr(s) as *mut _,
            );
            value
        }
    }
}

#[doc(hidden)]
impl crate::value::ToValueOptional for MatchInfo<'static> {
    #[inline]
    fn to_value_optional(s: Option<&Self>) -> crate::Value {
        let mut value = crate::Value::for_value_type::<Self>();
        unsafe {
            crate::gobject_ffi::g_value_take_boxed(
                crate::translate::ToGlibPtrMut::to_glib_none_mut(&mut value).0,
                crate::translate::ToGlibPtr::<*mut ffi::GMatchInfo>::to_glib_full(&s) as *mut _,
            );
        }

        value
    }
}

impl HasParamSpec for MatchInfo<'static> {
    type ParamSpec = crate::ParamSpecBoxed;
    type SetValue = Self;
    type BuilderFn = fn(&str) -> crate::ParamSpecBoxedBuilder<Self>;

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

impl<'input> MatchInfo<'input> {
    #[doc(alias = "g_match_info_fetch")]
    pub fn fetch(&self, match_num: i32) -> Option<crate::GString> {
        unsafe { from_glib_full(ffi::g_match_info_fetch(self.to_glib_none().0, match_num)) }
    }

    #[doc(alias = "g_match_info_fetch_all")]
    pub fn fetch_all(&self) -> Vec<crate::GString> {
        unsafe {
            FromGlibPtrContainer::from_glib_full(ffi::g_match_info_fetch_all(self.to_glib_none().0))
        }
    }

    #[doc(alias = "g_match_info_fetch_pos")]
    pub fn fetch_pos(&self, match_num: i32) -> Option<(i32, i32)> {
        unsafe {
            let mut start_pos = std::mem::MaybeUninit::uninit();
            let mut end_pos = std::mem::MaybeUninit::uninit();
            let ret = from_glib(ffi::g_match_info_fetch_pos(
                self.to_glib_none().0,
                match_num,
                start_pos.as_mut_ptr(),
                end_pos.as_mut_ptr(),
            ));
            if ret {
                Some((start_pos.assume_init(), end_pos.assume_init()))
            } else {
                None
            }
        }
    }

    #[doc(alias = "g_match_info_get_match_count")]
    #[doc(alias = "get_match_count")]
    pub fn match_count(&self) -> i32 {
        unsafe { ffi::g_match_info_get_match_count(self.to_glib_none().0) }
    }

    #[doc(alias = "g_match_info_get_regex")]
    #[doc(alias = "get_regex")]
    pub fn regex(&self) -> Regex {
        unsafe { from_glib_none(ffi::g_match_info_get_regex(self.to_glib_none().0)) }
    }

    #[doc(alias = "g_match_info_get_string")]
    #[doc(alias = "get_string")]
    pub fn string(&self) -> &'input crate::GStr {
        unsafe { from_glib_none(ffi::g_match_info_get_string(self.to_glib_none().0)) }
    }

    #[doc(alias = "g_match_info_is_partial_match")]
    pub fn is_partial_match(&self) -> bool {
        unsafe { from_glib(ffi::g_match_info_is_partial_match(self.to_glib_none().0)) }
    }

    #[doc(alias = "g_match_info_matches")]
    pub fn matches(&self) -> bool {
        unsafe { from_glib(ffi::g_match_info_matches(self.to_glib_none().0)) }
    }

    #[doc(alias = "g_match_info_next")]
    pub fn next(&self) -> Result<bool, crate::Error> {
        unsafe {
            let mut error = std::ptr::null_mut();
            let is_ok = ffi::g_match_info_next(self.to_glib_none().0, &mut error);
            if !error.is_null() {
                Err(from_glib_full(error))
            } else {
                Ok(from_glib(is_ok))
            }
        }
    }

    #[doc(alias = "g_match_info_expand_references")]
    pub fn expand_references(
        &self,
        string_to_expand: impl IntoGStr,
    ) -> Result<Option<crate::GString>, crate::Error> {
        string_to_expand.run_with_gstr(|string_to_expand| unsafe {
            let mut error = ptr::null_mut();
            let ret = ffi::g_match_info_expand_references(
                self.to_glib_none().0,
                string_to_expand.to_glib_none().0,
                &mut error,
            );
            if error.is_null() {
                Ok(from_glib_full(ret))
            } else {
                Err(from_glib_full(error))
            }
        })
    }

    #[doc(alias = "g_match_info_fetch_named")]
    pub fn fetch_named(&self, name: impl IntoGStr) -> Option<crate::GString> {
        name.run_with_gstr(|name| unsafe {
            from_glib_full(ffi::g_match_info_fetch_named(
                self.to_glib_none().0,
                name.to_glib_none().0,
            ))
        })
    }

    #[doc(alias = "g_match_info_fetch_named_pos")]
    pub fn fetch_named_pos(&self, name: impl IntoGStr) -> Option<(i32, i32)> {
        name.run_with_gstr(|name| unsafe {
            let mut start_pos = mem::MaybeUninit::uninit();
            let mut end_pos = mem::MaybeUninit::uninit();
            let ret = from_glib(ffi::g_match_info_fetch_named_pos(
                self.to_glib_none().0,
                name.to_glib_none().0,
                start_pos.as_mut_ptr(),
                end_pos.as_mut_ptr(),
            ));
            if ret {
                Some((start_pos.assume_init(), end_pos.assume_init()))
            } else {
                None
            }
        })
    }
}