glib/
match_info.rs

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