glib/
match_info.rs

1// Take a look at the license at the top of the repository in the LICENSE file.
2
3use crate::{ffi, prelude::*, translate::*, GStr, Regex};
4use std::{marker::PhantomData, mem, ptr};
5
6#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
7#[repr(transparent)]
8pub struct MatchInfo<'input> {
9    inner: std::ptr::NonNull<ffi::GMatchInfo>,
10    _phantom: PhantomData<&'input GStr>,
11}
12
13impl Clone for MatchInfo<'_> {
14    fn clone(&self) -> Self {
15        unsafe {
16            ffi::g_match_info_ref(self.inner.as_ptr());
17        }
18        Self {
19            inner: self.inner,
20            _phantom: PhantomData,
21        }
22    }
23}
24
25impl Drop for MatchInfo<'_> {
26    fn drop(&mut self) {
27        unsafe {
28            ffi::g_match_info_unref(self.inner.as_ptr());
29        }
30    }
31}
32
33impl MatchInfo<'_> {
34    #[doc = "Return the inner pointer to the underlying C value."]
35    #[inline]
36    pub fn as_ptr(&self) -> *mut ffi::GMatchInfo {
37        self.inner.as_ptr()
38    }
39    #[doc = "Borrows the underlying C value."]
40    #[inline]
41    pub unsafe fn from_glib_ptr_borrow(ptr: &*mut ffi::GMatchInfo) -> &Self {
42        debug_assert_eq!(
43            std::mem::size_of::<Self>(),
44            std::mem::size_of::<crate::ffi::gpointer>()
45        );
46        debug_assert!(!ptr.is_null());
47        &*(ptr as *const *mut ffi::GMatchInfo as *const Self)
48    }
49}
50
51#[doc(hidden)]
52impl GlibPtrDefault for MatchInfo<'_> {
53    type GlibType = *mut ffi::GMatchInfo;
54}
55#[doc(hidden)]
56unsafe impl TransparentPtrType for MatchInfo<'_> {}
57
58#[doc(hidden)]
59impl<'a, 'input> ToGlibPtr<'a, *mut ffi::GMatchInfo> for MatchInfo<'input>
60where
61    'input: 'a,
62{
63    type Storage = PhantomData<&'a Self>;
64    #[inline]
65    fn to_glib_none(&'a self) -> Stash<'a, *mut ffi::GMatchInfo, Self> {
66        Stash(self.inner.as_ptr(), PhantomData)
67    }
68    #[inline]
69    fn to_glib_full(&self) -> *mut ffi::GMatchInfo {
70        let ptr = self.inner.as_ptr();
71        unsafe {
72            ffi::g_match_info_ref(ptr);
73        }
74        ptr
75    }
76}
77#[doc(hidden)]
78impl<'a, 'input> ToGlibPtr<'a, *const ffi::GMatchInfo> for MatchInfo<'input>
79where
80    'input: 'a,
81{
82    type Storage = PhantomData<&'a Self>;
83    #[inline]
84    fn to_glib_none(&'a self) -> Stash<'a, *const ffi::GMatchInfo, Self> {
85        Stash(self.inner.as_ptr(), PhantomData)
86    }
87    #[inline]
88    fn to_glib_full(&self) -> *const ffi::GMatchInfo {
89        let ptr = self.inner.as_ptr();
90        unsafe {
91            ffi::g_match_info_ref(ptr);
92        }
93        ptr
94    }
95}
96
97#[doc(hidden)]
98impl FromGlibPtrNone<*mut ffi::GMatchInfo> for MatchInfo<'_> {
99    #[inline]
100    unsafe fn from_glib_none(ptr: *mut ffi::GMatchInfo) -> Self {
101        debug_assert!(!ptr.is_null());
102        unsafe {
103            ffi::g_match_info_ref(ptr);
104            Self {
105                inner: ptr::NonNull::new_unchecked(ptr),
106                _phantom: PhantomData,
107            }
108        }
109    }
110}
111#[doc(hidden)]
112impl FromGlibPtrNone<*const ffi::GMatchInfo> for MatchInfo<'_> {
113    #[inline]
114    unsafe fn from_glib_none(ptr: *const ffi::GMatchInfo) -> Self {
115        Self::from_glib_none(ptr.cast_mut())
116    }
117}
118#[doc(hidden)]
119impl FromGlibPtrFull<*mut ffi::GMatchInfo> for MatchInfo<'_> {
120    #[inline]
121    unsafe fn from_glib_full(ptr: *mut ffi::GMatchInfo) -> Self {
122        debug_assert!(!ptr.is_null());
123        unsafe {
124            Self {
125                inner: ptr::NonNull::new_unchecked(ptr),
126                _phantom: PhantomData,
127            }
128        }
129    }
130}
131#[doc(hidden)]
132impl FromGlibPtrBorrow<*mut ffi::GMatchInfo> for MatchInfo<'_> {
133    #[inline]
134    unsafe fn from_glib_borrow(ptr: *mut ffi::GMatchInfo) -> Borrowed<Self> {
135        debug_assert!(!ptr.is_null());
136        unsafe {
137            Borrowed::new(Self {
138                inner: ptr::NonNull::new_unchecked(ptr),
139                _phantom: PhantomData,
140            })
141        }
142    }
143}
144#[doc(hidden)]
145impl FromGlibPtrBorrow<*const ffi::GMatchInfo> for MatchInfo<'_> {
146    #[inline]
147    unsafe fn from_glib_borrow(ptr: *const ffi::GMatchInfo) -> Borrowed<Self> {
148        from_glib_borrow::<_, Self>(ptr.cast_mut())
149    }
150}
151
152#[doc(hidden)]
153impl IntoGlibPtr<*mut ffi::GMatchInfo> for MatchInfo<'_> {
154    #[inline]
155    unsafe fn into_glib_ptr(self) -> *mut ffi::GMatchInfo {
156        let s = std::mem::ManuallyDrop::new(self);
157        ToGlibPtr::<*const ffi::GMatchInfo>::to_glib_none(&*s).0 as *mut _
158    }
159}
160#[doc(hidden)]
161impl IntoGlibPtr<*const ffi::GMatchInfo> for MatchInfo<'_> {
162    #[inline]
163    unsafe fn into_glib_ptr(self) -> *const ffi::GMatchInfo {
164        let s = std::mem::ManuallyDrop::new(self);
165        ToGlibPtr::<*const ffi::GMatchInfo>::to_glib_none(&*s).0 as *const _
166    }
167}
168impl StaticType for MatchInfo<'_> {
169    #[inline]
170    fn static_type() -> crate::types::Type {
171        unsafe { from_glib(ffi::g_match_info_get_type()) }
172    }
173}
174
175#[doc(hidden)]
176impl ValueType for MatchInfo<'static> {
177    type Type = Self;
178}
179
180#[doc(hidden)]
181impl crate::value::ValueTypeOptional for MatchInfo<'static> {}
182
183unsafe impl<'a, 'input: 'a> crate::value::FromValue<'a> for MatchInfo<'input> {
184    type Checker = crate::value::GenericValueTypeOrNoneChecker<Self>;
185
186    unsafe fn from_value(value: &'a crate::Value) -> Self {
187        let ptr = crate::gobject_ffi::g_value_dup_boxed(
188            crate::translate::ToGlibPtr::to_glib_none(value).0,
189        );
190        debug_assert!(!ptr.is_null());
191        <Self as crate::translate::FromGlibPtrFull<*mut ffi::GMatchInfo>>::from_glib_full(
192            ptr as *mut ffi::GMatchInfo,
193        )
194    }
195}
196#[doc(hidden)]
197unsafe impl<'a, 'input: 'a> crate::value::FromValue<'a> for &'a MatchInfo<'input> {
198    type Checker = crate::value::GenericValueTypeOrNoneChecker<Self>;
199
200    #[inline]
201    unsafe fn from_value(value: &'a crate::Value) -> Self {
202        let value = &*(value as *const crate::Value as *const crate::gobject_ffi::GValue);
203        <MatchInfo<'input>>::from_glib_ptr_borrow(
204            &*(&value.data[0].v_pointer as *const crate::ffi::gpointer
205                as *const *mut ffi::GMatchInfo),
206        )
207    }
208}
209impl ToValue for MatchInfo<'static> {
210    #[inline]
211    fn to_value(&self) -> crate::Value {
212        unsafe {
213            let mut value = crate::Value::from_type_unchecked(<Self as StaticType>::static_type());
214            crate::gobject_ffi::g_value_take_boxed(
215                crate::translate::ToGlibPtrMut::to_glib_none_mut(&mut value).0,
216                crate::translate::ToGlibPtr::<*mut ffi::GMatchInfo>::to_glib_full(self) as *mut _,
217            );
218            value
219        }
220    }
221
222    #[inline]
223    fn value_type(&self) -> crate::Type {
224        <Self as StaticType>::static_type()
225    }
226}
227
228impl From<MatchInfo<'static>> for crate::Value {
229    #[inline]
230    fn from(s: MatchInfo<'static>) -> Self {
231        unsafe {
232            let mut value = crate::Value::from_type_unchecked(
233                <MatchInfo<'static> as StaticType>::static_type(),
234            );
235            crate::gobject_ffi::g_value_take_boxed(
236                crate::translate::ToGlibPtrMut::to_glib_none_mut(&mut value).0,
237                crate::translate::IntoGlibPtr::<*mut ffi::GMatchInfo>::into_glib_ptr(s) as *mut _,
238            );
239            value
240        }
241    }
242}
243
244#[doc(hidden)]
245impl crate::value::ToValueOptional for MatchInfo<'static> {
246    #[inline]
247    fn to_value_optional(s: Option<&Self>) -> crate::Value {
248        let mut value = crate::Value::for_value_type::<Self>();
249        unsafe {
250            crate::gobject_ffi::g_value_take_boxed(
251                crate::translate::ToGlibPtrMut::to_glib_none_mut(&mut value).0,
252                crate::translate::ToGlibPtr::<*mut ffi::GMatchInfo>::to_glib_full(&s) as *mut _,
253            );
254        }
255
256        value
257    }
258}
259
260impl HasParamSpec for MatchInfo<'static> {
261    type ParamSpec = crate::ParamSpecBoxed;
262    type SetValue = Self;
263    type BuilderFn = fn(&str) -> crate::ParamSpecBoxedBuilder<Self>;
264
265    fn param_spec_builder() -> Self::BuilderFn {
266        Self::ParamSpec::builder
267    }
268}
269
270impl<'input> MatchInfo<'input> {
271    #[doc(alias = "g_match_info_fetch")]
272    pub fn fetch(&self, match_num: i32) -> Option<crate::GString> {
273        unsafe { from_glib_full(ffi::g_match_info_fetch(self.to_glib_none().0, match_num)) }
274    }
275
276    #[doc(alias = "g_match_info_fetch_all")]
277    pub fn fetch_all(&self) -> Vec<crate::GString> {
278        unsafe {
279            FromGlibPtrContainer::from_glib_full(ffi::g_match_info_fetch_all(self.to_glib_none().0))
280        }
281    }
282
283    #[doc(alias = "g_match_info_fetch_pos")]
284    pub fn fetch_pos(&self, match_num: i32) -> Option<(i32, i32)> {
285        unsafe {
286            let mut start_pos = std::mem::MaybeUninit::uninit();
287            let mut end_pos = std::mem::MaybeUninit::uninit();
288            let ret = from_glib(ffi::g_match_info_fetch_pos(
289                self.to_glib_none().0,
290                match_num,
291                start_pos.as_mut_ptr(),
292                end_pos.as_mut_ptr(),
293            ));
294            if ret {
295                Some((start_pos.assume_init(), end_pos.assume_init()))
296            } else {
297                None
298            }
299        }
300    }
301
302    #[doc(alias = "g_match_info_get_match_count")]
303    #[doc(alias = "get_match_count")]
304    pub fn match_count(&self) -> i32 {
305        unsafe { ffi::g_match_info_get_match_count(self.to_glib_none().0) }
306    }
307
308    #[doc(alias = "g_match_info_get_regex")]
309    #[doc(alias = "get_regex")]
310    pub fn regex(&self) -> Regex {
311        unsafe { from_glib_none(ffi::g_match_info_get_regex(self.to_glib_none().0)) }
312    }
313
314    #[doc(alias = "g_match_info_get_string")]
315    #[doc(alias = "get_string")]
316    pub fn string(&self) -> &'input crate::GStr {
317        unsafe { from_glib_none(ffi::g_match_info_get_string(self.to_glib_none().0)) }
318    }
319
320    #[doc(alias = "g_match_info_is_partial_match")]
321    pub fn is_partial_match(&self) -> bool {
322        unsafe { from_glib(ffi::g_match_info_is_partial_match(self.to_glib_none().0)) }
323    }
324
325    #[doc(alias = "g_match_info_matches")]
326    pub fn matches(&self) -> bool {
327        unsafe { from_glib(ffi::g_match_info_matches(self.to_glib_none().0)) }
328    }
329
330    #[doc(alias = "g_match_info_next")]
331    pub fn next(&self) -> Result<bool, crate::Error> {
332        unsafe {
333            let mut error = std::ptr::null_mut();
334            let is_ok = ffi::g_match_info_next(self.to_glib_none().0, &mut error);
335            if !error.is_null() {
336                Err(from_glib_full(error))
337            } else {
338                Ok(from_glib(is_ok))
339            }
340        }
341    }
342
343    #[doc(alias = "g_match_info_expand_references")]
344    pub fn expand_references(
345        &self,
346        string_to_expand: impl IntoGStr,
347    ) -> Result<Option<crate::GString>, crate::Error> {
348        string_to_expand.run_with_gstr(|string_to_expand| unsafe {
349            let mut error = ptr::null_mut();
350            let ret = ffi::g_match_info_expand_references(
351                self.to_glib_none().0,
352                string_to_expand.to_glib_none().0,
353                &mut error,
354            );
355            if error.is_null() {
356                Ok(from_glib_full(ret))
357            } else {
358                Err(from_glib_full(error))
359            }
360        })
361    }
362
363    #[doc(alias = "g_match_info_fetch_named")]
364    pub fn fetch_named(&self, name: impl IntoGStr) -> Option<crate::GString> {
365        name.run_with_gstr(|name| unsafe {
366            from_glib_full(ffi::g_match_info_fetch_named(
367                self.to_glib_none().0,
368                name.to_glib_none().0,
369            ))
370        })
371    }
372
373    #[doc(alias = "g_match_info_fetch_named_pos")]
374    pub fn fetch_named_pos(&self, name: impl IntoGStr) -> Option<(i32, i32)> {
375        name.run_with_gstr(|name| unsafe {
376            let mut start_pos = mem::MaybeUninit::uninit();
377            let mut end_pos = mem::MaybeUninit::uninit();
378            let ret = from_glib(ffi::g_match_info_fetch_named_pos(
379                self.to_glib_none().0,
380                name.to_glib_none().0,
381                start_pos.as_mut_ptr(),
382                end_pos.as_mut_ptr(),
383            ));
384            if ret {
385                Some((start_pos.assume_init(), end_pos.assume_init()))
386            } else {
387                None
388            }
389        })
390    }
391}