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")]
54 pub struct Generator(Object<ffi::JsonGenerator, ffi::JsonGeneratorClass>);
55
56 match fn {
57 type_ => || ffi::json_generator_get_type(),
58 }
59}
60
61impl Generator {
62 pub const NONE: Option<&'static Generator> = None;
63
64 #[doc(alias = "json_generator_new")]
73 pub fn new() -> Generator {
74 assert_initialized_main_thread!();
75 unsafe { from_glib_full(ffi::json_generator_new()) }
76 }
77
78 pub fn builder() -> GeneratorBuilder {
83 GeneratorBuilder::new()
84 }
85}
86
87impl Default for Generator {
88 fn default() -> Self {
89 Self::new()
90 }
91}
92
93#[must_use = "The builder must be built to be used"]
98pub struct GeneratorBuilder {
99 builder: glib::object::ObjectBuilder<'static, Generator>,
100}
101
102impl GeneratorBuilder {
103 fn new() -> Self {
104 Self {
105 builder: glib::object::Object::builder(),
106 }
107 }
108
109 pub fn indent(self, indent: u32) -> Self {
111 Self {
112 builder: self.builder.property("indent", indent),
113 }
114 }
115
116 pub fn indent_char(self, indent_char: u32) -> Self {
118 Self {
119 builder: self.builder.property("indent-char", indent_char),
120 }
121 }
122
123 pub fn pretty(self, pretty: bool) -> Self {
129 Self {
130 builder: self.builder.property("pretty", pretty),
131 }
132 }
133
134 pub fn root(self, root: &Node) -> Self {
137 Self {
138 builder: self.builder.property("root", root.clone()),
139 }
140 }
141
142 #[must_use = "Building the object from the builder is usually expensive and is not expected to have side effects"]
145 pub fn build(self) -> Generator {
146 assert_initialized_main_thread!();
147 self.builder.build()
148 }
149}
150
151pub trait GeneratorExt: IsA<Generator> + 'static {
157 #[doc(alias = "json_generator_get_indent")]
163 #[doc(alias = "get_indent")]
164 fn indent(&self) -> u32 {
165 unsafe { ffi::json_generator_get_indent(self.as_ref().to_glib_none().0) }
166 }
167
168 #[doc(alias = "json_generator_get_indent_char")]
174 #[doc(alias = "get_indent_char")]
175 #[doc(alias = "indent-char")]
176 fn indent_char(&self) -> char {
177 unsafe {
178 std::convert::TryFrom::try_from(ffi::json_generator_get_indent_char(
179 self.as_ref().to_glib_none().0,
180 ))
181 .expect("conversion from an invalid Unicode value attempted")
182 }
183 }
184
185 #[doc(alias = "json_generator_get_pretty")]
192 #[doc(alias = "get_pretty")]
193 #[doc(alias = "pretty")]
194 fn is_pretty(&self) -> bool {
195 unsafe {
196 from_glib(ffi::json_generator_get_pretty(
197 self.as_ref().to_glib_none().0,
198 ))
199 }
200 }
201
202 #[doc(alias = "json_generator_get_root")]
209 #[doc(alias = "get_root")]
210 fn root(&self) -> Option<Node> {
211 unsafe { from_glib_none(ffi::json_generator_get_root(self.as_ref().to_glib_none().0)) }
212 }
213
214 #[doc(alias = "json_generator_set_indent")]
219 #[doc(alias = "indent")]
220 fn set_indent(&self, indent_level: u32) {
221 unsafe {
222 ffi::json_generator_set_indent(self.as_ref().to_glib_none().0, indent_level);
223 }
224 }
225
226 #[doc(alias = "json_generator_set_indent_char")]
230 #[doc(alias = "indent-char")]
231 fn set_indent_char(&self, indent_char: char) {
232 unsafe {
233 ffi::json_generator_set_indent_char(
234 self.as_ref().to_glib_none().0,
235 indent_char.into_glib(),
236 );
237 }
238 }
239
240 #[doc(alias = "json_generator_set_pretty")]
248 #[doc(alias = "pretty")]
249 fn set_pretty(&self, is_pretty: bool) {
250 unsafe {
251 ffi::json_generator_set_pretty(self.as_ref().to_glib_none().0, is_pretty.into_glib());
252 }
253 }
254
255 #[doc(alias = "json_generator_set_root")]
263 #[doc(alias = "root")]
264 fn set_root(&self, node: &Node) {
265 unsafe {
266 ffi::json_generator_set_root(self.as_ref().to_glib_none().0, node.to_glib_none().0);
267 }
268 }
269
270 #[cfg(feature = "v1_10")]
277 #[cfg_attr(docsrs, doc(cfg(feature = "v1_10")))]
278 #[doc(alias = "json_generator_take_root")]
279 fn take_root(&self, node: Option<Node>) {
280 unsafe {
281 ffi::json_generator_take_root(self.as_ref().to_glib_none().0, node.into_glib_ptr());
282 }
283 }
284
285 #[doc(alias = "json_generator_to_data")]
296 fn to_data(&self) -> (glib::GString, usize) {
297 unsafe {
298 let mut length = std::mem::MaybeUninit::uninit();
299 let ret = from_glib_full(ffi::json_generator_to_data(
300 self.as_ref().to_glib_none().0,
301 length.as_mut_ptr(),
302 ));
303 (ret, length.assume_init())
304 }
305 }
306
307 #[doc(alias = "json_generator_to_file")]
319 fn to_file(&self, filename: impl AsRef<std::path::Path>) -> Result<(), glib::Error> {
320 unsafe {
321 let mut error = std::ptr::null_mut();
322 let is_ok = ffi::json_generator_to_file(
323 self.as_ref().to_glib_none().0,
324 filename.as_ref().to_glib_none().0,
325 &mut error,
326 );
327 debug_assert_eq!(is_ok == glib::ffi::GFALSE, !error.is_null());
328 if error.is_null() {
329 Ok(())
330 } else {
331 Err(from_glib_full(error))
332 }
333 }
334 }
335
336 #[doc(alias = "json_generator_to_stream")]
359 fn to_stream(
360 &self,
361 stream: &impl IsA<gio::OutputStream>,
362 cancellable: Option<&impl IsA<gio::Cancellable>>,
363 ) -> Result<(), glib::Error> {
364 unsafe {
365 let mut error = std::ptr::null_mut();
366 let is_ok = ffi::json_generator_to_stream(
367 self.as_ref().to_glib_none().0,
368 stream.as_ref().to_glib_none().0,
369 cancellable.map(|p| p.as_ref()).to_glib_none().0,
370 &mut error,
371 );
372 debug_assert_eq!(is_ok == glib::ffi::GFALSE, !error.is_null());
373 if error.is_null() {
374 Ok(())
375 } else {
376 Err(from_glib_full(error))
377 }
378 }
379 }
380
381 #[doc(alias = "indent")]
382 fn connect_indent_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
383 unsafe extern "C" fn notify_indent_trampoline<P: IsA<Generator>, F: Fn(&P) + 'static>(
384 this: *mut ffi::JsonGenerator,
385 _param_spec: glib::ffi::gpointer,
386 f: glib::ffi::gpointer,
387 ) {
388 let f: &F = &*(f as *const F);
389 f(Generator::from_glib_borrow(this).unsafe_cast_ref())
390 }
391 unsafe {
392 let f: Box_<F> = Box_::new(f);
393 connect_raw(
394 self.as_ptr() as *mut _,
395 c"notify::indent".as_ptr() as *const _,
396 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
397 notify_indent_trampoline::<Self, F> as *const (),
398 )),
399 Box_::into_raw(f),
400 )
401 }
402 }
403
404 #[doc(alias = "indent-char")]
405 fn connect_indent_char_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
406 unsafe extern "C" fn notify_indent_char_trampoline<
407 P: IsA<Generator>,
408 F: Fn(&P) + 'static,
409 >(
410 this: *mut ffi::JsonGenerator,
411 _param_spec: glib::ffi::gpointer,
412 f: glib::ffi::gpointer,
413 ) {
414 let f: &F = &*(f as *const F);
415 f(Generator::from_glib_borrow(this).unsafe_cast_ref())
416 }
417 unsafe {
418 let f: Box_<F> = Box_::new(f);
419 connect_raw(
420 self.as_ptr() as *mut _,
421 c"notify::indent-char".as_ptr() as *const _,
422 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
423 notify_indent_char_trampoline::<Self, F> as *const (),
424 )),
425 Box_::into_raw(f),
426 )
427 }
428 }
429
430 #[doc(alias = "pretty")]
431 fn connect_pretty_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
432 unsafe extern "C" fn notify_pretty_trampoline<P: IsA<Generator>, F: Fn(&P) + 'static>(
433 this: *mut ffi::JsonGenerator,
434 _param_spec: glib::ffi::gpointer,
435 f: glib::ffi::gpointer,
436 ) {
437 let f: &F = &*(f as *const F);
438 f(Generator::from_glib_borrow(this).unsafe_cast_ref())
439 }
440 unsafe {
441 let f: Box_<F> = Box_::new(f);
442 connect_raw(
443 self.as_ptr() as *mut _,
444 c"notify::pretty".as_ptr() as *const _,
445 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
446 notify_pretty_trampoline::<Self, F> as *const (),
447 )),
448 Box_::into_raw(f),
449 )
450 }
451 }
452
453 #[doc(alias = "root")]
454 fn connect_root_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
455 unsafe extern "C" fn notify_root_trampoline<P: IsA<Generator>, F: Fn(&P) + 'static>(
456 this: *mut ffi::JsonGenerator,
457 _param_spec: glib::ffi::gpointer,
458 f: glib::ffi::gpointer,
459 ) {
460 let f: &F = &*(f as *const F);
461 f(Generator::from_glib_borrow(this).unsafe_cast_ref())
462 }
463 unsafe {
464 let f: Box_<F> = Box_::new(f);
465 connect_raw(
466 self.as_ptr() as *mut _,
467 c"notify::root".as_ptr() as *const _,
468 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
469 notify_root_trampoline::<Self, F> as *const (),
470 )),
471 Box_::into_raw(f),
472 )
473 }
474 }
475}
476
477impl<O: IsA<Generator>> GeneratorExt for O {}