1use crate::{Node, ffi};
7use glib::{
8 prelude::*,
9 signal::{SignalHandlerId, connect_raw},
10 translate::*,
11};
12use std::boxed::Box as Box_;
13
14glib::wrapper! {
15 #[doc(alias = "JsonGenerator")]
16 pub struct Generator(Object<ffi::JsonGenerator, ffi::JsonGeneratorClass>);
17
18 match fn {
19 type_ => || ffi::json_generator_get_type(),
20 }
21}
22
23impl Generator {
24 pub const NONE: Option<&'static Generator> = None;
25
26 #[doc(alias = "json_generator_new")]
27 pub fn new() -> Generator {
28 assert_initialized_main_thread!();
29 unsafe { from_glib_full(ffi::json_generator_new()) }
30 }
31
32 pub fn builder() -> GeneratorBuilder {
37 GeneratorBuilder::new()
38 }
39}
40
41impl Default for Generator {
42 fn default() -> Self {
43 Self::new()
44 }
45}
46
47#[must_use = "The builder must be built to be used"]
52pub struct GeneratorBuilder {
53 builder: glib::object::ObjectBuilder<'static, Generator>,
54}
55
56impl GeneratorBuilder {
57 fn new() -> Self {
58 Self {
59 builder: glib::object::Object::builder(),
60 }
61 }
62
63 pub fn indent(self, indent: u32) -> Self {
64 Self {
65 builder: self.builder.property("indent", indent),
66 }
67 }
68
69 pub fn indent_char(self, indent_char: u32) -> Self {
70 Self {
71 builder: self.builder.property("indent-char", indent_char),
72 }
73 }
74
75 pub fn pretty(self, pretty: bool) -> Self {
76 Self {
77 builder: self.builder.property("pretty", pretty),
78 }
79 }
80
81 pub fn root(self, root: &Node) -> Self {
82 Self {
83 builder: self.builder.property("root", root.clone()),
84 }
85 }
86
87 #[must_use = "Building the object from the builder is usually expensive and is not expected to have side effects"]
90 pub fn build(self) -> Generator {
91 assert_initialized_main_thread!();
92 self.builder.build()
93 }
94}
95
96pub trait GeneratorExt: IsA<Generator> + 'static {
97 #[doc(alias = "json_generator_get_indent")]
98 #[doc(alias = "get_indent")]
99 fn indent(&self) -> u32 {
100 unsafe { ffi::json_generator_get_indent(self.as_ref().to_glib_none().0) }
101 }
102
103 #[doc(alias = "json_generator_get_indent_char")]
104 #[doc(alias = "get_indent_char")]
105 #[doc(alias = "indent-char")]
106 fn indent_char(&self) -> char {
107 unsafe {
108 std::convert::TryFrom::try_from(ffi::json_generator_get_indent_char(
109 self.as_ref().to_glib_none().0,
110 ))
111 .expect("conversion from an invalid Unicode value attempted")
112 }
113 }
114
115 #[doc(alias = "json_generator_get_pretty")]
116 #[doc(alias = "get_pretty")]
117 #[doc(alias = "pretty")]
118 fn is_pretty(&self) -> bool {
119 unsafe {
120 from_glib(ffi::json_generator_get_pretty(
121 self.as_ref().to_glib_none().0,
122 ))
123 }
124 }
125
126 #[doc(alias = "json_generator_get_root")]
127 #[doc(alias = "get_root")]
128 fn root(&self) -> Option<Node> {
129 unsafe { from_glib_none(ffi::json_generator_get_root(self.as_ref().to_glib_none().0)) }
130 }
131
132 #[doc(alias = "json_generator_set_indent")]
133 #[doc(alias = "indent")]
134 fn set_indent(&self, indent_level: u32) {
135 unsafe {
136 ffi::json_generator_set_indent(self.as_ref().to_glib_none().0, indent_level);
137 }
138 }
139
140 #[doc(alias = "json_generator_set_indent_char")]
141 #[doc(alias = "indent-char")]
142 fn set_indent_char(&self, indent_char: char) {
143 unsafe {
144 ffi::json_generator_set_indent_char(
145 self.as_ref().to_glib_none().0,
146 indent_char.into_glib(),
147 );
148 }
149 }
150
151 #[doc(alias = "json_generator_set_pretty")]
152 #[doc(alias = "pretty")]
153 fn set_pretty(&self, is_pretty: bool) {
154 unsafe {
155 ffi::json_generator_set_pretty(self.as_ref().to_glib_none().0, is_pretty.into_glib());
156 }
157 }
158
159 #[doc(alias = "json_generator_set_root")]
160 #[doc(alias = "root")]
161 fn set_root(&self, node: &Node) {
162 unsafe {
163 ffi::json_generator_set_root(self.as_ref().to_glib_none().0, node.to_glib_none().0);
164 }
165 }
166
167 #[cfg(feature = "v1_10")]
168 #[cfg_attr(docsrs, doc(cfg(feature = "v1_10")))]
169 #[doc(alias = "json_generator_take_root")]
170 fn take_root(&self, node: Option<Node>) {
171 unsafe {
172 ffi::json_generator_take_root(self.as_ref().to_glib_none().0, node.into_glib_ptr());
173 }
174 }
175
176 #[doc(alias = "json_generator_to_data")]
177 fn to_data(&self) -> (glib::GString, usize) {
178 unsafe {
179 let mut length = std::mem::MaybeUninit::uninit();
180 let ret = from_glib_full(ffi::json_generator_to_data(
181 self.as_ref().to_glib_none().0,
182 length.as_mut_ptr(),
183 ));
184 (ret, length.assume_init())
185 }
186 }
187
188 #[doc(alias = "json_generator_to_file")]
189 fn to_file(&self, filename: impl AsRef<std::path::Path>) -> Result<(), glib::Error> {
190 unsafe {
191 let mut error = std::ptr::null_mut();
192 let is_ok = ffi::json_generator_to_file(
193 self.as_ref().to_glib_none().0,
194 filename.as_ref().to_glib_none().0,
195 &mut error,
196 );
197 debug_assert_eq!(is_ok == glib::ffi::GFALSE, !error.is_null());
198 if error.is_null() {
199 Ok(())
200 } else {
201 Err(from_glib_full(error))
202 }
203 }
204 }
205
206 #[doc(alias = "json_generator_to_stream")]
220 fn to_stream(
221 &self,
222 stream: &impl IsA<gio::OutputStream>,
223 cancellable: Option<&impl IsA<gio::Cancellable>>,
224 ) -> Result<(), glib::Error> {
225 unsafe {
226 let mut error = std::ptr::null_mut();
227 let is_ok = ffi::json_generator_to_stream(
228 self.as_ref().to_glib_none().0,
229 stream.as_ref().to_glib_none().0,
230 cancellable.map(|p| p.as_ref()).to_glib_none().0,
231 &mut error,
232 );
233 debug_assert_eq!(is_ok == glib::ffi::GFALSE, !error.is_null());
234 if error.is_null() {
235 Ok(())
236 } else {
237 Err(from_glib_full(error))
238 }
239 }
240 }
241
242 #[doc(alias = "indent")]
243 fn connect_indent_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
244 unsafe extern "C" fn notify_indent_trampoline<P: IsA<Generator>, F: Fn(&P) + 'static>(
245 this: *mut ffi::JsonGenerator,
246 _param_spec: glib::ffi::gpointer,
247 f: glib::ffi::gpointer,
248 ) {
249 let f: &F = &*(f as *const F);
250 f(Generator::from_glib_borrow(this).unsafe_cast_ref())
251 }
252 unsafe {
253 let f: Box_<F> = Box_::new(f);
254 connect_raw(
255 self.as_ptr() as *mut _,
256 c"notify::indent".as_ptr() as *const _,
257 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
258 notify_indent_trampoline::<Self, F> as *const (),
259 )),
260 Box_::into_raw(f),
261 )
262 }
263 }
264
265 #[doc(alias = "indent-char")]
266 fn connect_indent_char_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
267 unsafe extern "C" fn notify_indent_char_trampoline<
268 P: IsA<Generator>,
269 F: Fn(&P) + 'static,
270 >(
271 this: *mut ffi::JsonGenerator,
272 _param_spec: glib::ffi::gpointer,
273 f: glib::ffi::gpointer,
274 ) {
275 let f: &F = &*(f as *const F);
276 f(Generator::from_glib_borrow(this).unsafe_cast_ref())
277 }
278 unsafe {
279 let f: Box_<F> = Box_::new(f);
280 connect_raw(
281 self.as_ptr() as *mut _,
282 c"notify::indent-char".as_ptr() as *const _,
283 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
284 notify_indent_char_trampoline::<Self, F> as *const (),
285 )),
286 Box_::into_raw(f),
287 )
288 }
289 }
290
291 #[doc(alias = "pretty")]
292 fn connect_pretty_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
293 unsafe extern "C" fn notify_pretty_trampoline<P: IsA<Generator>, F: Fn(&P) + 'static>(
294 this: *mut ffi::JsonGenerator,
295 _param_spec: glib::ffi::gpointer,
296 f: glib::ffi::gpointer,
297 ) {
298 let f: &F = &*(f as *const F);
299 f(Generator::from_glib_borrow(this).unsafe_cast_ref())
300 }
301 unsafe {
302 let f: Box_<F> = Box_::new(f);
303 connect_raw(
304 self.as_ptr() as *mut _,
305 c"notify::pretty".as_ptr() as *const _,
306 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
307 notify_pretty_trampoline::<Self, F> as *const (),
308 )),
309 Box_::into_raw(f),
310 )
311 }
312 }
313
314 #[doc(alias = "root")]
315 fn connect_root_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
316 unsafe extern "C" fn notify_root_trampoline<P: IsA<Generator>, F: Fn(&P) + 'static>(
317 this: *mut ffi::JsonGenerator,
318 _param_spec: glib::ffi::gpointer,
319 f: glib::ffi::gpointer,
320 ) {
321 let f: &F = &*(f as *const F);
322 f(Generator::from_glib_borrow(this).unsafe_cast_ref())
323 }
324 unsafe {
325 let f: Box_<F> = Box_::new(f);
326 connect_raw(
327 self.as_ptr() as *mut _,
328 c"notify::root".as_ptr() as *const _,
329 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
330 notify_root_trampoline::<Self, F> as *const (),
331 )),
332 Box_::into_raw(f),
333 )
334 }
335 }
336}
337
338impl<O: IsA<Generator>> GeneratorExt for O {}