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 line number and the number of the character on
97 /// that line. Intended for use in error messages; there are no strict
98 /// semantics for what constitutes the "current" line number other than
99 /// "the best number we could come up with for error messages."
100 ///
101 /// # Returns
102 ///
103 ///
104 /// ## `line_number`
105 /// return location for a line number, or [`None`]
106 ///
107 /// ## `char_number`
108 /// return location for a char-on-line number, or [`None`]
109 #[doc(alias = "g_markup_parse_context_get_position")]
110 #[doc(alias = "get_position")]
111 pub fn position(&self) -> (i32, i32) {
112 unsafe {
113 let mut line_number = std::mem::MaybeUninit::uninit();
114 let mut char_number = std::mem::MaybeUninit::uninit();
115 ffi::g_markup_parse_context_get_position(
116 self.to_glib_none().0,
117 line_number.as_mut_ptr(),
118 char_number.as_mut_ptr(),
119 );
120 (line_number.assume_init(), char_number.assume_init())
121 }
122 }
123
124 /// Feed some data to the #GMarkupParseContext.
125 ///
126 /// The data need not be valid UTF-8; an error will be signaled if
127 /// it's invalid. The data need not be an entire document; you can
128 /// feed a document into the parser incrementally, via multiple calls
129 /// to this function. Typically, as you receive data from a network
130 /// connection or file, you feed each received chunk of data into this
131 /// function, aborting the process if an error occurs. Once an error
132 /// is reported, no further data may be fed to the #GMarkupParseContext;
133 /// all errors are fatal.
134 /// ## `text`
135 /// chunk of text to parse
136 /// ## `text_len`
137 /// length of @text in bytes
138 ///
139 /// # Returns
140 ///
141 /// [`false`] if an error occurred, [`true`] on success
142 #[doc(alias = "g_markup_parse_context_parse")]
143 pub fn parse(&self, text: &str) -> Result<(), crate::Error> {
144 let text_len = text.len() as _;
145 unsafe {
146 let mut error = std::ptr::null_mut();
147 let is_ok = ffi::g_markup_parse_context_parse(
148 self.to_glib_none().0,
149 text.to_glib_none().0,
150 text_len,
151 &mut error,
152 );
153 debug_assert_eq!(is_ok == crate::ffi::GFALSE, !error.is_null());
154 if error.is_null() {
155 Ok(())
156 } else {
157 Err(from_glib_full(error))
158 }
159 }
160 }
161
162 //#[doc(alias = "g_markup_parse_context_pop")]
163 //pub fn pop(&self) -> /*Unimplemented*/Option<Basic: Pointer> {
164 // unsafe { TODO: call ffi:g_markup_parse_context_pop() }
165 //}
166
167 //#[doc(alias = "g_markup_parse_context_push")]
168 //pub fn push(&self, parser: /*Ignored*/&MarkupParser, user_data: /*Unimplemented*/Option<Basic: Pointer>) {
169 // unsafe { TODO: call ffi:g_markup_parse_context_push() }
170 //}
171}