Skip to main content

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#[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        unsafe {
43            debug_assert_eq!(
44                std::mem::size_of::<Self>(),
45                std::mem::size_of::<crate::ffi::gpointer>()
46            );
47            debug_assert!(!ptr.is_null());
48            &*(ptr as *const *mut ffi::GMatchInfo as *const Self)
49        }
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        unsafe { 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        unsafe { 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        unsafe {
190            let ptr = crate::gobject_ffi::g_value_dup_boxed(
191                crate::translate::ToGlibPtr::to_glib_none(value).0,
192            );
193            debug_assert!(!ptr.is_null());
194            <Self as crate::translate::FromGlibPtrFull<*mut ffi::GMatchInfo>>::from_glib_full(
195                ptr as *mut ffi::GMatchInfo,
196            )
197        }
198    }
199}
200#[doc(hidden)]
201unsafe impl<'a, 'input: 'a> crate::value::FromValue<'a> for &'a MatchInfo<'input> {
202    type Checker = crate::value::GenericValueTypeOrNoneChecker<Self>;
203
204    #[inline]
205    unsafe fn from_value(value: &'a crate::Value) -> Self {
206        unsafe {
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    }
214}
215impl ToValue for MatchInfo<'static> {
216    #[inline]
217    fn to_value(&self) -> crate::Value {
218        unsafe {
219            let mut value = crate::Value::from_type_unchecked(<Self as StaticType>::static_type());
220            crate::gobject_ffi::g_value_take_boxed(
221                crate::translate::ToGlibPtrMut::to_glib_none_mut(&mut value).0,
222                crate::translate::ToGlibPtr::<*mut ffi::GMatchInfo>::to_glib_full(self) as *mut _,
223            );
224            value
225        }
226    }
227
228    #[inline]
229    fn value_type(&self) -> crate::Type {
230        <Self as StaticType>::static_type()
231    }
232}
233
234impl From<MatchInfo<'static>> for crate::Value {
235    #[inline]
236    fn from(s: MatchInfo<'static>) -> Self {
237        unsafe {
238            let mut value = crate::Value::from_type_unchecked(
239                <MatchInfo<'static> as StaticType>::static_type(),
240            );
241            crate::gobject_ffi::g_value_take_boxed(
242                crate::translate::ToGlibPtrMut::to_glib_none_mut(&mut value).0,
243                crate::translate::IntoGlibPtr::<*mut ffi::GMatchInfo>::into_glib_ptr(s) as *mut _,
244            );
245            value
246        }
247    }
248}
249
250#[doc(hidden)]
251impl crate::value::ToValueOptional for MatchInfo<'static> {
252    #[inline]
253    fn to_value_optional(s: Option<&Self>) -> crate::Value {
254        let mut value = crate::Value::for_value_type::<Self>();
255        unsafe {
256            crate::gobject_ffi::g_value_take_boxed(
257                crate::translate::ToGlibPtrMut::to_glib_none_mut(&mut value).0,
258                crate::translate::ToGlibPtr::<*mut ffi::GMatchInfo>::to_glib_full(&s) as *mut _,
259            );
260        }
261
262        value
263    }
264}
265
266impl HasParamSpec for MatchInfo<'static> {
267    type ParamSpec = crate::ParamSpecBoxed;
268    type SetValue = Self;
269    type BuilderFn = fn(&str) -> crate::ParamSpecBoxedBuilder<Self>;
270
271    fn param_spec_builder() -> Self::BuilderFn {
272        Self::ParamSpec::builder
273    }
274}
275
276impl<'input> MatchInfo<'input> {
277    #[doc(alias = "g_match_info_fetch")]
278    pub fn fetch(&self, match_num: i32) -> Option<crate::GString> {
279        unsafe { from_glib_full(ffi::g_match_info_fetch(self.to_glib_none().0, match_num)) }
280    }
281
282    #[doc(alias = "g_match_info_fetch_all")]
283    pub fn fetch_all(&self) -> Vec<crate::GString> {
284        unsafe {
285            FromGlibPtrContainer::from_glib_full(ffi::g_match_info_fetch_all(self.to_glib_none().0))
286        }
287    }
288
289    #[doc(alias = "g_match_info_fetch_pos")]
290    pub fn fetch_pos(&self, match_num: i32) -> Option<(i32, i32)> {
291        unsafe {
292            let mut start_pos = std::mem::MaybeUninit::uninit();
293            let mut end_pos = std::mem::MaybeUninit::uninit();
294            let ret = from_glib(ffi::g_match_info_fetch_pos(
295                self.to_glib_none().0,
296                match_num,
297                start_pos.as_mut_ptr(),
298                end_pos.as_mut_ptr(),
299            ));
300            if ret {
301                Some((start_pos.assume_init(), end_pos.assume_init()))
302            } else {
303                None
304            }
305        }
306    }
307
308    #[doc(alias = "g_match_info_get_match_count")]
309    #[doc(alias = "get_match_count")]
310    pub fn match_count(&self) -> i32 {
311        unsafe { ffi::g_match_info_get_match_count(self.to_glib_none().0) }
312    }
313
314    #[doc(alias = "g_match_info_get_regex")]
315    #[doc(alias = "get_regex")]
316    pub fn regex(&self) -> Regex {
317        unsafe { from_glib_none(ffi::g_match_info_get_regex(self.to_glib_none().0)) }
318    }
319
320    #[doc(alias = "g_match_info_get_string")]
321    #[doc(alias = "get_string")]
322    pub fn string(&self) -> &'input crate::GStr {
323        unsafe { from_glib_none(ffi::g_match_info_get_string(self.to_glib_none().0)) }
324    }
325
326    #[doc(alias = "g_match_info_is_partial_match")]
327    pub fn is_partial_match(&self) -> bool {
328        unsafe { from_glib(ffi::g_match_info_is_partial_match(self.to_glib_none().0)) }
329    }
330
331    #[doc(alias = "g_match_info_matches")]
332    pub fn matches(&self) -> bool {
333        unsafe { from_glib(ffi::g_match_info_matches(self.to_glib_none().0)) }
334    }
335
336    #[doc(alias = "g_match_info_next")]
337    pub fn next(&self) -> Result<bool, crate::Error> {
338        unsafe {
339            let mut error = std::ptr::null_mut();
340            let is_ok = ffi::g_match_info_next(self.to_glib_none().0, &mut error);
341            if !error.is_null() {
342                Err(from_glib_full(error))
343            } else {
344                Ok(from_glib(is_ok))
345            }
346        }
347    }
348
349    #[doc(alias = "g_match_info_expand_references")]
350    pub fn expand_references(
351        &self,
352        string_to_expand: impl IntoGStr,
353    ) -> Result<Option<crate::GString>, crate::Error> {
354        string_to_expand.run_with_gstr(|string_to_expand| unsafe {
355            let mut error = ptr::null_mut();
356            let ret = ffi::g_match_info_expand_references(
357                self.to_glib_none().0,
358                string_to_expand.to_glib_none().0,
359                &mut error,
360            );
361            if error.is_null() {
362                Ok(from_glib_full(ret))
363            } else {
364                Err(from_glib_full(error))
365            }
366        })
367    }
368
369    #[doc(alias = "g_match_info_fetch_named")]
370    pub fn fetch_named(&self, name: impl IntoGStr) -> Option<crate::GString> {
371        name.run_with_gstr(|name| unsafe {
372            from_glib_full(ffi::g_match_info_fetch_named(
373                self.to_glib_none().0,
374                name.to_glib_none().0,
375            ))
376        })
377    }
378
379    #[doc(alias = "g_match_info_fetch_named_pos")]
380    pub fn fetch_named_pos(&self, name: impl IntoGStr) -> Option<(i32, i32)> {
381        name.run_with_gstr(|name| unsafe {
382            let mut start_pos = mem::MaybeUninit::uninit();
383            let mut end_pos = mem::MaybeUninit::uninit();
384            let ret = from_glib(ffi::g_match_info_fetch_named_pos(
385                self.to_glib_none().0,
386                name.to_glib_none().0,
387                start_pos.as_mut_ptr(),
388                end_pos.as_mut_ptr(),
389            ));
390            if ret {
391                Some((start_pos.assume_init(), end_pos.assume_init()))
392            } else {
393                None
394            }
395        })
396    }
397}