Skip to main content

glib/auto/
markup_parse_context.rs

1// This file was generated by gir (https://github.com/gtk-rs/gir)
2// from gir-files (https://github.com/gtk-rs/gir-files)
3// DO NOT EDIT
4
5use crate::{Error, ffi, translate::*};
6
7crate::wrapper! {
8    /// A parse context is used to parse a stream of bytes that
9    /// you expect to contain marked-up text.
10    ///
11    /// See g_markup_parse_context_new(), #GMarkupParser, and so
12    /// on for more details.
13    #[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
14    pub struct MarkupParseContext(Shared<ffi::GMarkupParseContext>);
15
16    match fn {
17        ref => |ptr| ffi::g_markup_parse_context_ref(ptr),
18        unref => |ptr| ffi::g_markup_parse_context_unref(ptr),
19        type_ => || ffi::g_markup_parse_context_get_type(),
20    }
21}
22
23impl MarkupParseContext {
24    //#[doc(alias = "g_markup_parse_context_new")]
25    //pub fn new(parser: /*Ignored*/&MarkupParser, flags: /*Ignored*/MarkupParseFlags, user_data: /*Unimplemented*/Option<Basic: Pointer>) -> MarkupParseContext {
26    //    unsafe { TODO: call ffi:g_markup_parse_context_new() }
27    //}
28
29    /// Signals to the #GMarkupParseContext that all data has been
30    /// fed into the parse context with g_markup_parse_context_parse().
31    ///
32    /// This function reports an error if the document isn't complete,
33    /// for example if elements are still open.
34    ///
35    /// # Returns
36    ///
37    /// [`true`] on success, [`false`] if an error was set
38    #[doc(alias = "g_markup_parse_context_end_parse")]
39    pub fn end_parse(&self) -> Result<(), crate::Error> {
40        unsafe {
41            let mut error = std::ptr::null_mut();
42            let is_ok = ffi::g_markup_parse_context_end_parse(self.to_glib_none().0, &mut error);
43            debug_assert_eq!(is_ok == crate::ffi::GFALSE, !error.is_null());
44            if error.is_null() {
45                Ok(())
46            } else {
47                Err(from_glib_full(error))
48            }
49        }
50    }
51
52    /// Retrieves the name of the currently open element.
53    ///
54    /// If called from the start_element or end_element handlers this will
55    /// give the element_name as passed to those functions. For the parent
56    /// elements, see g_markup_parse_context_get_element_stack().
57    ///
58    /// # Returns
59    ///
60    /// the name of the currently open element, or [`None`]
61    #[doc(alias = "g_markup_parse_context_get_element")]
62    #[doc(alias = "get_element")]
63    pub fn element(&self) -> crate::GString {
64        unsafe {
65            from_glib_none(ffi::g_markup_parse_context_get_element(
66                self.to_glib_none().0,
67            ))
68        }
69    }
70
71    /// Retrieves the element stack from the internal state of the parser.
72    ///
73    /// The returned #GSList is a list of strings where the first item is
74    /// the currently open tag (as would be returned by
75    /// g_markup_parse_context_get_element()) and the next item is its
76    /// immediate parent.
77    ///
78    /// This function is intended to be used in the start_element and
79    /// end_element handlers where g_markup_parse_context_get_element()
80    /// would merely return the name of the element that is being
81    /// processed.
82    ///
83    /// # Returns
84    ///
85    /// the element stack, which must not be modified
86    #[doc(alias = "g_markup_parse_context_get_element_stack")]
87    #[doc(alias = "get_element_stack")]
88    pub fn element_stack(&self) -> Vec<crate::GString> {
89        unsafe {
90            FromGlibPtrContainer::from_glib_none(ffi::g_markup_parse_context_get_element_stack(
91                self.to_glib_none().0,
92            ))
93        }
94    }
95
96    /// Retrieves the current offset from the beginning of the document,
97    /// in bytes.
98    ///
99    /// The information is meant to accompany the values returned by
100    /// [`position()`][Self::position()], and comes with the
101    /// same accuracy guarantees.
102    ///
103    /// # Returns
104    ///
105    /// the offset
106    #[cfg(feature = "v2_88")]
107    #[cfg_attr(docsrs, doc(cfg(feature = "v2_88")))]
108    #[doc(alias = "g_markup_parse_context_get_offset")]
109    #[doc(alias = "get_offset")]
110    pub fn offset(&self) -> usize {
111        unsafe { ffi::g_markup_parse_context_get_offset(self.to_glib_none().0) }
112    }
113
114    /// Retrieves the current line number and the number of the character on
115    /// that line. Intended for use in error messages; there are no strict
116    /// semantics for what constitutes the "current" line number other than
117    /// "the best number we could come up with for error messages."
118    ///
119    /// # Returns
120    ///
121    ///
122    /// ## `line_number`
123    /// return location for a line number, or [`None`]
124    ///
125    /// ## `char_number`
126    /// return location for a char-on-line number, or [`None`]
127    #[doc(alias = "g_markup_parse_context_get_position")]
128    #[doc(alias = "get_position")]
129    pub fn position(&self) -> (i32, i32) {
130        unsafe {
131            let mut line_number = std::mem::MaybeUninit::uninit();
132            let mut char_number = std::mem::MaybeUninit::uninit();
133            ffi::g_markup_parse_context_get_position(
134                self.to_glib_none().0,
135                line_number.as_mut_ptr(),
136                char_number.as_mut_ptr(),
137            );
138            (line_number.assume_init(), char_number.assume_init())
139        }
140    }
141
142    /// Retrieves the start position of the current start or end tag.
143    ///
144    /// This function can be used in the `start_element` or `end_element`
145    /// callbacks to obtain location information for error reporting.
146    ///
147    /// Note that @line_number and @char_number are intended for human
148    /// readable error messages and are therefore 1-based and in Unicode
149    /// characters. @offset on the other hand is meant for programmatic
150    /// use, and thus is 0-based and in bytes.
151    ///
152    /// The information is meant to accompany the values returned by
153    /// [`position()`][Self::position()], and comes with the
154    /// same accuracy guarantees.
155    ///
156    /// # Returns
157    ///
158    ///
159    /// ## `line_number`
160    /// return location for the line number
161    ///
162    /// ## `char_number`
163    /// return location for the character number
164    ///
165    /// ## `offset`
166    /// return location for offset from the beginning of the document
167    #[cfg(feature = "v2_88")]
168    #[cfg_attr(docsrs, doc(cfg(feature = "v2_88")))]
169    #[doc(alias = "g_markup_parse_context_get_tag_start")]
170    #[doc(alias = "get_tag_start")]
171    pub fn tag_start(&self) -> (usize, usize, usize) {
172        unsafe {
173            let mut line_number = std::mem::MaybeUninit::uninit();
174            let mut char_number = std::mem::MaybeUninit::uninit();
175            let mut offset = std::mem::MaybeUninit::uninit();
176            ffi::g_markup_parse_context_get_tag_start(
177                self.to_glib_none().0,
178                line_number.as_mut_ptr(),
179                char_number.as_mut_ptr(),
180                offset.as_mut_ptr(),
181            );
182            (
183                line_number.assume_init(),
184                char_number.assume_init(),
185                offset.assume_init(),
186            )
187        }
188    }
189
190    /// Feed some data to the #GMarkupParseContext.
191    ///
192    /// The data need not be valid UTF-8; an error will be signaled if
193    /// it's invalid. The data need not be an entire document; you can
194    /// feed a document into the parser incrementally, via multiple calls
195    /// to this function. Typically, as you receive data from a network
196    /// connection or file, you feed each received chunk of data into this
197    /// function, aborting the process if an error occurs. Once an error
198    /// is reported, no further data may be fed to the #GMarkupParseContext;
199    /// all errors are fatal.
200    /// ## `text`
201    /// chunk of text to parse
202    /// ## `text_len`
203    /// length of @text in bytes
204    ///
205    /// # Returns
206    ///
207    /// [`false`] if an error occurred, [`true`] on success
208    #[doc(alias = "g_markup_parse_context_parse")]
209    pub fn parse(&self, text: &str) -> Result<(), crate::Error> {
210        let text_len = text.len() as _;
211        unsafe {
212            let mut error = std::ptr::null_mut();
213            let is_ok = ffi::g_markup_parse_context_parse(
214                self.to_glib_none().0,
215                text.to_glib_none().0,
216                text_len,
217                &mut error,
218            );
219            debug_assert_eq!(is_ok == crate::ffi::GFALSE, !error.is_null());
220            if error.is_null() {
221                Ok(())
222            } else {
223                Err(from_glib_full(error))
224            }
225        }
226    }
227
228    //#[doc(alias = "g_markup_parse_context_pop")]
229    //pub fn pop(&self) -> /*Unimplemented*/Option<Basic: Pointer> {
230    //    unsafe { TODO: call ffi:g_markup_parse_context_pop() }
231    //}
232
233    //#[doc(alias = "g_markup_parse_context_push")]
234    //pub fn push(&self, parser: /*Ignored*/&MarkupParser, user_data: /*Unimplemented*/Option<Basic: Pointer>) {
235    //    unsafe { TODO: call ffi:g_markup_parse_context_push() }
236    //}
237}