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::{ffi, translate::*, Error};
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 /// Feed some data to the #GMarkupParseContext.
143 ///
144 /// The data need not be valid UTF-8; an error will be signaled if
145 /// it's invalid. The data need not be an entire document; you can
146 /// feed a document into the parser incrementally, via multiple calls
147 /// to this function. Typically, as you receive data from a network
148 /// connection or file, you feed each received chunk of data into this
149 /// function, aborting the process if an error occurs. Once an error
150 /// is reported, no further data may be fed to the #GMarkupParseContext;
151 /// all errors are fatal.
152 /// ## `text`
153 /// chunk of text to parse
154 /// ## `text_len`
155 /// length of @text in bytes
156 ///
157 /// # Returns
158 ///
159 /// [`false`] if an error occurred, [`true`] on success
160 #[doc(alias = "g_markup_parse_context_parse")]
161 pub fn parse(&self, text: &str) -> Result<(), crate::Error> {
162 let text_len = text.len() as _;
163 unsafe {
164 let mut error = std::ptr::null_mut();
165 let is_ok = ffi::g_markup_parse_context_parse(
166 self.to_glib_none().0,
167 text.to_glib_none().0,
168 text_len,
169 &mut error,
170 );
171 debug_assert_eq!(is_ok == crate::ffi::GFALSE, !error.is_null());
172 if error.is_null() {
173 Ok(())
174 } else {
175 Err(from_glib_full(error))
176 }
177 }
178 }
179
180 //#[doc(alias = "g_markup_parse_context_pop")]
181 //pub fn pop(&self) -> /*Unimplemented*/Option<Basic: Pointer> {
182 // unsafe { TODO: call ffi:g_markup_parse_context_pop() }
183 //}
184
185 //#[doc(alias = "g_markup_parse_context_push")]
186 //pub fn push(&self, parser: /*Ignored*/&MarkupParser, user_data: /*Unimplemented*/Option<Basic: Pointer>) {
187 // unsafe { TODO: call ffi:g_markup_parse_context_push() }
188 //}
189}