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