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 start and end positions of an attribute assignment
53 /// in a start tag.
54 ///
55 /// This function can be used in the `start_element` callback to
56 /// obtain location information for error reporting.
57 ///
58 /// Calling it outside of the `start_element` callback
59 /// has undefined results.
60 ///
61 /// Note that @line_number and @char_number are intended for human
62 /// readable error messages and are therefore 1-based and in Unicode
63 /// characters. @offset on the other hand is meant for programmatic
64 /// use, and thus is 0-based and in bytes.
65 ///
66 /// The information is meant to accompany the values returned by
67 /// [`position()`][Self::position()], and comes with the
68 /// same accuracy guarantees.
69 /// ## `attr`
70 /// the index of the attribute to query
71 ///
72 /// # Returns
73 ///
74 ///
75 /// ## `start_lines`
76 /// return location for the line number of the attribute assignment start
77 ///
78 /// ## `start_chars`
79 /// return location for the character number of the attribute assignment start
80 ///
81 /// ## `start_offset`
82 /// return location for offset of the attribute assignment
83 ///
84 /// ## `end_lines`
85 /// return location for the line number of the attribute assignment end
86 ///
87 /// ## `end_chars`
88 /// return location for the character number of the attribute assignment end
89 ///
90 /// ## `end_offset`
91 /// return location for offset of the attribute assignment end
92 #[cfg(feature = "v2_90")]
93 #[cfg_attr(docsrs, doc(cfg(feature = "v2_90")))]
94 #[doc(alias = "g_markup_parse_context_get_attribute_position")]
95 #[doc(alias = "get_attribute_position")]
96 pub fn attribute_position(&self, attr: u32) -> (usize, usize, usize, usize, usize, usize) {
97 unsafe {
98 let mut start_lines = std::mem::MaybeUninit::uninit();
99 let mut start_chars = std::mem::MaybeUninit::uninit();
100 let mut start_offset = std::mem::MaybeUninit::uninit();
101 let mut end_lines = std::mem::MaybeUninit::uninit();
102 let mut end_chars = std::mem::MaybeUninit::uninit();
103 let mut end_offset = std::mem::MaybeUninit::uninit();
104 ffi::g_markup_parse_context_get_attribute_position(
105 self.to_glib_none().0,
106 attr,
107 start_lines.as_mut_ptr(),
108 start_chars.as_mut_ptr(),
109 start_offset.as_mut_ptr(),
110 end_lines.as_mut_ptr(),
111 end_chars.as_mut_ptr(),
112 end_offset.as_mut_ptr(),
113 );
114 (
115 start_lines.assume_init(),
116 start_chars.assume_init(),
117 start_offset.assume_init(),
118 end_lines.assume_init(),
119 end_chars.assume_init(),
120 end_offset.assume_init(),
121 )
122 }
123 }
124
125 /// Retrieves the name of the currently open element.
126 ///
127 /// If called from the start_element or end_element handlers this will
128 /// give the element_name as passed to those functions. For the parent
129 /// elements, see g_markup_parse_context_get_element_stack().
130 ///
131 /// # Returns
132 ///
133 /// the name of the currently open element, or [`None`]
134 #[doc(alias = "g_markup_parse_context_get_element")]
135 #[doc(alias = "get_element")]
136 pub fn element(&self) -> crate::GString {
137 unsafe {
138 from_glib_none(ffi::g_markup_parse_context_get_element(
139 self.to_glib_none().0,
140 ))
141 }
142 }
143
144 /// Retrieves the element stack from the internal state of the parser.
145 ///
146 /// The returned #GSList is a list of strings where the first item is
147 /// the currently open tag (as would be returned by
148 /// g_markup_parse_context_get_element()) and the next item is its
149 /// immediate parent.
150 ///
151 /// This function is intended to be used in the start_element and
152 /// end_element handlers where g_markup_parse_context_get_element()
153 /// would merely return the name of the element that is being
154 /// processed.
155 ///
156 /// # Returns
157 ///
158 /// the element stack, which must not be modified
159 #[doc(alias = "g_markup_parse_context_get_element_stack")]
160 #[doc(alias = "get_element_stack")]
161 pub fn element_stack(&self) -> Vec<crate::GString> {
162 unsafe {
163 FromGlibPtrContainer::from_glib_none(ffi::g_markup_parse_context_get_element_stack(
164 self.to_glib_none().0,
165 ))
166 }
167 }
168
169 /// Retrieves the current offset from the beginning of the document,
170 /// in bytes.
171 ///
172 /// The information is meant to accompany the values returned by
173 /// [`position()`][Self::position()], and comes with the
174 /// same accuracy guarantees.
175 ///
176 /// # Returns
177 ///
178 /// the offset
179 #[cfg(feature = "v2_88")]
180 #[cfg_attr(docsrs, doc(cfg(feature = "v2_88")))]
181 #[doc(alias = "g_markup_parse_context_get_offset")]
182 #[doc(alias = "get_offset")]
183 pub fn offset(&self) -> usize {
184 unsafe { ffi::g_markup_parse_context_get_offset(self.to_glib_none().0) }
185 }
186
187 /// Retrieves the current line number and the number of the character on
188 /// that line. Intended for use in error messages; there are no strict
189 /// semantics for what constitutes the "current" line number other than
190 /// "the best number we could come up with for error messages."
191 ///
192 /// # Returns
193 ///
194 ///
195 /// ## `line_number`
196 /// return location for a line number, or [`None`]
197 ///
198 /// ## `char_number`
199 /// return location for a char-on-line number, or [`None`]
200 #[doc(alias = "g_markup_parse_context_get_position")]
201 #[doc(alias = "get_position")]
202 pub fn position(&self) -> (i32, i32) {
203 unsafe {
204 let mut line_number = std::mem::MaybeUninit::uninit();
205 let mut char_number = std::mem::MaybeUninit::uninit();
206 ffi::g_markup_parse_context_get_position(
207 self.to_glib_none().0,
208 line_number.as_mut_ptr(),
209 char_number.as_mut_ptr(),
210 );
211 (line_number.assume_init(), char_number.assume_init())
212 }
213 }
214
215 /// Retrieves the start position of the current start or end tag.
216 ///
217 /// This function can be used in the `start_element` or `end_element`
218 /// callbacks to obtain location information for error reporting.
219 ///
220 /// Calling it outside of these callbacks has undefined results.
221 ///
222 /// Note that @line_number and @char_number are intended for human
223 /// readable error messages and are therefore 1-based and in Unicode
224 /// characters. @offset on the other hand is meant for programmatic
225 /// use, and thus is 0-based and in bytes.
226 ///
227 /// The information is meant to accompany the values returned by
228 /// [`position()`][Self::position()], and comes with the
229 /// same accuracy guarantees.
230 ///
231 /// # Returns
232 ///
233 ///
234 /// ## `line_number`
235 /// return location for the line number
236 ///
237 /// ## `char_number`
238 /// return location for the character number
239 ///
240 /// ## `offset`
241 /// return location for offset from the beginning of the document
242 #[cfg(feature = "v2_88")]
243 #[cfg_attr(docsrs, doc(cfg(feature = "v2_88")))]
244 #[doc(alias = "g_markup_parse_context_get_tag_start")]
245 #[doc(alias = "get_tag_start")]
246 pub fn tag_start(&self) -> (usize, usize, usize) {
247 unsafe {
248 let mut line_number = std::mem::MaybeUninit::uninit();
249 let mut char_number = std::mem::MaybeUninit::uninit();
250 let mut offset = std::mem::MaybeUninit::uninit();
251 ffi::g_markup_parse_context_get_tag_start(
252 self.to_glib_none().0,
253 line_number.as_mut_ptr(),
254 char_number.as_mut_ptr(),
255 offset.as_mut_ptr(),
256 );
257 (
258 line_number.assume_init(),
259 char_number.assume_init(),
260 offset.assume_init(),
261 )
262 }
263 }
264
265 /// Feed some data to the #GMarkupParseContext.
266 ///
267 /// The data need not be valid UTF-8; an error will be signaled if
268 /// it's invalid. The data need not be an entire document; you can
269 /// feed a document into the parser incrementally, via multiple calls
270 /// to this function. Typically, as you receive data from a network
271 /// connection or file, you feed each received chunk of data into this
272 /// function, aborting the process if an error occurs. Once an error
273 /// is reported, no further data may be fed to the #GMarkupParseContext;
274 /// all errors are fatal.
275 /// ## `text`
276 /// chunk of text to parse
277 /// ## `text_len`
278 /// length of @text in bytes
279 ///
280 /// # Returns
281 ///
282 /// [`false`] if an error occurred, [`true`] on success
283 #[doc(alias = "g_markup_parse_context_parse")]
284 pub fn parse(&self, text: &str) -> Result<(), crate::Error> {
285 let text_len = text.len() as _;
286 unsafe {
287 let mut error = std::ptr::null_mut();
288 let is_ok = ffi::g_markup_parse_context_parse(
289 self.to_glib_none().0,
290 text.to_glib_none().0,
291 text_len,
292 &mut error,
293 );
294 debug_assert_eq!(is_ok == crate::ffi::GFALSE, !error.is_null());
295 if error.is_null() {
296 Ok(())
297 } else {
298 Err(from_glib_full(error))
299 }
300 }
301 }
302
303 //#[doc(alias = "g_markup_parse_context_pop")]
304 //pub fn pop(&self) -> /*Unimplemented*/Option<Basic: Pointer> {
305 // unsafe { TODO: call ffi:g_markup_parse_context_pop() }
306 //}
307
308 //#[doc(alias = "g_markup_parse_context_push")]
309 //pub fn push(&self, parser: /*Ignored*/&MarkupParser, user_data: /*Unimplemented*/Option<Basic: Pointer>) {
310 // unsafe { TODO: call ffi:g_markup_parse_context_push() }
311 //}
312}