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