use crate::{Node, ffi};
use glib::{
prelude::*,
signal::{SignalHandlerId, connect_raw},
translate::*,
};
use std::boxed::Box as Box_;
glib::wrapper! {
#[doc(alias = "JsonGenerator")]
pub struct Generator(Object<ffi::JsonGenerator, ffi::JsonGeneratorClass>);
match fn {
type_ => || ffi::json_generator_get_type(),
}
}
impl Generator {
pub const NONE: Option<&'static Generator> = None;
#[doc(alias = "json_generator_new")]
pub fn new() -> Generator {
assert_initialized_main_thread!();
unsafe { from_glib_full(ffi::json_generator_new()) }
}
pub fn builder() -> GeneratorBuilder {
GeneratorBuilder::new()
}
}
impl Default for Generator {
fn default() -> Self {
Self::new()
}
}
#[must_use = "The builder must be built to be used"]
pub struct GeneratorBuilder {
builder: glib::object::ObjectBuilder<'static, Generator>,
}
impl GeneratorBuilder {
fn new() -> Self {
Self {
builder: glib::object::Object::builder(),
}
}
pub fn indent(self, indent: u32) -> Self {
Self {
builder: self.builder.property("indent", indent),
}
}
pub fn indent_char(self, indent_char: u32) -> Self {
Self {
builder: self.builder.property("indent-char", indent_char),
}
}
pub fn pretty(self, pretty: bool) -> Self {
Self {
builder: self.builder.property("pretty", pretty),
}
}
pub fn root(self, root: &Node) -> Self {
Self {
builder: self.builder.property("root", root.clone()),
}
}
#[must_use = "Building the object from the builder is usually expensive and is not expected to have side effects"]
pub fn build(self) -> Generator {
assert_initialized_main_thread!();
self.builder.build()
}
}
pub trait GeneratorExt: IsA<Generator> + 'static {
#[doc(alias = "json_generator_get_indent")]
#[doc(alias = "get_indent")]
fn indent(&self) -> u32 {
unsafe { ffi::json_generator_get_indent(self.as_ref().to_glib_none().0) }
}
#[doc(alias = "json_generator_get_indent_char")]
#[doc(alias = "get_indent_char")]
#[doc(alias = "indent-char")]
fn indent_char(&self) -> char {
unsafe {
std::convert::TryFrom::try_from(ffi::json_generator_get_indent_char(
self.as_ref().to_glib_none().0,
))
.expect("conversion from an invalid Unicode value attempted")
}
}
#[doc(alias = "json_generator_get_pretty")]
#[doc(alias = "get_pretty")]
#[doc(alias = "pretty")]
fn is_pretty(&self) -> bool {
unsafe {
from_glib(ffi::json_generator_get_pretty(
self.as_ref().to_glib_none().0,
))
}
}
#[doc(alias = "json_generator_get_root")]
#[doc(alias = "get_root")]
fn root(&self) -> Option<Node> {
unsafe { from_glib_none(ffi::json_generator_get_root(self.as_ref().to_glib_none().0)) }
}
#[doc(alias = "json_generator_set_indent")]
#[doc(alias = "indent")]
fn set_indent(&self, indent_level: u32) {
unsafe {
ffi::json_generator_set_indent(self.as_ref().to_glib_none().0, indent_level);
}
}
#[doc(alias = "json_generator_set_indent_char")]
#[doc(alias = "indent-char")]
fn set_indent_char(&self, indent_char: char) {
unsafe {
ffi::json_generator_set_indent_char(
self.as_ref().to_glib_none().0,
indent_char.into_glib(),
);
}
}
#[doc(alias = "json_generator_set_pretty")]
#[doc(alias = "pretty")]
fn set_pretty(&self, is_pretty: bool) {
unsafe {
ffi::json_generator_set_pretty(self.as_ref().to_glib_none().0, is_pretty.into_glib());
}
}
#[doc(alias = "json_generator_set_root")]
#[doc(alias = "root")]
fn set_root(&self, node: &Node) {
unsafe {
ffi::json_generator_set_root(self.as_ref().to_glib_none().0, node.to_glib_none().0);
}
}
#[cfg(feature = "v1_10")]
#[cfg_attr(docsrs, doc(cfg(feature = "v1_10")))]
#[doc(alias = "json_generator_take_root")]
fn take_root(&self, node: Option<Node>) {
unsafe {
ffi::json_generator_take_root(self.as_ref().to_glib_none().0, node.into_glib_ptr());
}
}
#[doc(alias = "json_generator_to_data")]
fn to_data(&self) -> (glib::GString, usize) {
unsafe {
let mut length = std::mem::MaybeUninit::uninit();
let ret = from_glib_full(ffi::json_generator_to_data(
self.as_ref().to_glib_none().0,
length.as_mut_ptr(),
));
(ret, length.assume_init())
}
}
#[doc(alias = "json_generator_to_file")]
fn to_file(&self, filename: impl AsRef<std::path::Path>) -> Result<(), glib::Error> {
unsafe {
let mut error = std::ptr::null_mut();
let is_ok = ffi::json_generator_to_file(
self.as_ref().to_glib_none().0,
filename.as_ref().to_glib_none().0,
&mut error,
);
debug_assert_eq!(is_ok == glib::ffi::GFALSE, !error.is_null());
if error.is_null() {
Ok(())
} else {
Err(from_glib_full(error))
}
}
}
#[doc(alias = "json_generator_to_stream")]
fn to_stream(
&self,
stream: &impl IsA<gio::OutputStream>,
cancellable: Option<&impl IsA<gio::Cancellable>>,
) -> Result<(), glib::Error> {
unsafe {
let mut error = std::ptr::null_mut();
let is_ok = ffi::json_generator_to_stream(
self.as_ref().to_glib_none().0,
stream.as_ref().to_glib_none().0,
cancellable.map(|p| p.as_ref()).to_glib_none().0,
&mut error,
);
debug_assert_eq!(is_ok == glib::ffi::GFALSE, !error.is_null());
if error.is_null() {
Ok(())
} else {
Err(from_glib_full(error))
}
}
}
#[doc(alias = "indent")]
fn connect_indent_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
unsafe extern "C" fn notify_indent_trampoline<P: IsA<Generator>, F: Fn(&P) + 'static>(
this: *mut ffi::JsonGenerator,
_param_spec: glib::ffi::gpointer,
f: glib::ffi::gpointer,
) {
let f: &F = &*(f as *const F);
f(Generator::from_glib_borrow(this).unsafe_cast_ref())
}
unsafe {
let f: Box_<F> = Box_::new(f);
connect_raw(
self.as_ptr() as *mut _,
c"notify::indent".as_ptr() as *const _,
Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
notify_indent_trampoline::<Self, F> as *const (),
)),
Box_::into_raw(f),
)
}
}
#[doc(alias = "indent-char")]
fn connect_indent_char_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
unsafe extern "C" fn notify_indent_char_trampoline<
P: IsA<Generator>,
F: Fn(&P) + 'static,
>(
this: *mut ffi::JsonGenerator,
_param_spec: glib::ffi::gpointer,
f: glib::ffi::gpointer,
) {
let f: &F = &*(f as *const F);
f(Generator::from_glib_borrow(this).unsafe_cast_ref())
}
unsafe {
let f: Box_<F> = Box_::new(f);
connect_raw(
self.as_ptr() as *mut _,
c"notify::indent-char".as_ptr() as *const _,
Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
notify_indent_char_trampoline::<Self, F> as *const (),
)),
Box_::into_raw(f),
)
}
}
#[doc(alias = "pretty")]
fn connect_pretty_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
unsafe extern "C" fn notify_pretty_trampoline<P: IsA<Generator>, F: Fn(&P) + 'static>(
this: *mut ffi::JsonGenerator,
_param_spec: glib::ffi::gpointer,
f: glib::ffi::gpointer,
) {
let f: &F = &*(f as *const F);
f(Generator::from_glib_borrow(this).unsafe_cast_ref())
}
unsafe {
let f: Box_<F> = Box_::new(f);
connect_raw(
self.as_ptr() as *mut _,
c"notify::pretty".as_ptr() as *const _,
Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
notify_pretty_trampoline::<Self, F> as *const (),
)),
Box_::into_raw(f),
)
}
}
#[doc(alias = "root")]
fn connect_root_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
unsafe extern "C" fn notify_root_trampoline<P: IsA<Generator>, F: Fn(&P) + 'static>(
this: *mut ffi::JsonGenerator,
_param_spec: glib::ffi::gpointer,
f: glib::ffi::gpointer,
) {
let f: &F = &*(f as *const F);
f(Generator::from_glib_borrow(this).unsafe_cast_ref())
}
unsafe {
let f: Box_<F> = Box_::new(f);
connect_raw(
self.as_ptr() as *mut _,
c"notify::root".as_ptr() as *const _,
Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
notify_root_trampoline::<Self, F> as *const (),
)),
Box_::into_raw(f),
)
}
}
}
impl<O: IsA<Generator>> GeneratorExt for O {}