use crate::{Array, Node, Object, ffi};
use glib::{
object::ObjectType as _,
prelude::*,
signal::{SignalHandlerId, connect_raw},
translate::*,
};
use std::{boxed::Box as Box_, pin::Pin};
glib::wrapper! {
#[doc(alias = "JsonParser")]
pub struct Parser(Object<ffi::JsonParser, ffi::JsonParserClass>);
match fn {
type_ => || ffi::json_parser_get_type(),
}
}
impl Parser {
pub const NONE: Option<&'static Parser> = None;
#[doc(alias = "json_parser_new")]
pub fn new() -> Parser {
assert_initialized_main_thread!();
unsafe { from_glib_full(ffi::json_parser_new()) }
}
#[cfg(feature = "v1_2")]
#[cfg_attr(docsrs, doc(cfg(feature = "v1_2")))]
#[doc(alias = "json_parser_new_immutable")]
pub fn new_immutable() -> Parser {
assert_initialized_main_thread!();
unsafe { from_glib_full(ffi::json_parser_new_immutable()) }
}
pub fn builder() -> ParserBuilder {
ParserBuilder::new()
}
}
impl Default for Parser {
fn default() -> Self {
Self::new()
}
}
#[must_use = "The builder must be built to be used"]
pub struct ParserBuilder {
builder: glib::object::ObjectBuilder<'static, Parser>,
}
impl ParserBuilder {
fn new() -> Self {
Self {
builder: glib::object::Object::builder(),
}
}
#[cfg(feature = "v1_2")]
#[cfg_attr(docsrs, doc(cfg(feature = "v1_2")))]
pub fn immutable(self, immutable: bool) -> Self {
Self {
builder: self.builder.property("immutable", immutable),
}
}
#[cfg(feature = "v1_10")]
#[cfg_attr(docsrs, doc(cfg(feature = "v1_10")))]
pub fn strict(self, strict: bool) -> Self {
Self {
builder: self.builder.property("strict", strict),
}
}
#[must_use = "Building the object from the builder is usually expensive and is not expected to have side effects"]
pub fn build(self) -> Parser {
assert_initialized_main_thread!();
self.builder.build()
}
}
pub trait ParserExt: IsA<Parser> + 'static {
#[doc(alias = "json_parser_get_current_line")]
#[doc(alias = "get_current_line")]
fn current_line(&self) -> u32 {
unsafe { ffi::json_parser_get_current_line(self.as_ref().to_glib_none().0) }
}
#[doc(alias = "json_parser_get_current_pos")]
#[doc(alias = "get_current_pos")]
fn current_pos(&self) -> u32 {
unsafe { ffi::json_parser_get_current_pos(self.as_ref().to_glib_none().0) }
}
#[doc(alias = "json_parser_get_root")]
#[doc(alias = "get_root")]
fn root(&self) -> Option<Node> {
unsafe { from_glib_none(ffi::json_parser_get_root(self.as_ref().to_glib_none().0)) }
}
#[cfg(feature = "v1_10")]
#[cfg_attr(docsrs, doc(cfg(feature = "v1_10")))]
#[doc(alias = "json_parser_get_strict")]
#[doc(alias = "get_strict")]
#[doc(alias = "strict")]
fn is_strict(&self) -> bool {
unsafe { from_glib(ffi::json_parser_get_strict(self.as_ref().to_glib_none().0)) }
}
#[doc(alias = "json_parser_has_assignment")]
fn has_assignment(&self) -> Option<glib::GString> {
unsafe {
let variable_name = std::ptr::null_mut();
let ret = from_glib(ffi::json_parser_has_assignment(
self.as_ref().to_glib_none().0,
variable_name,
));
if ret {
Some(from_glib_none(*variable_name))
} else {
None
}
}
}
#[doc(alias = "json_parser_load_from_data")]
fn load_from_data(&self, data: &str) -> Result<(), glib::Error> {
let length = data.len() as _;
unsafe {
let mut error = std::ptr::null_mut();
let is_ok = ffi::json_parser_load_from_data(
self.as_ref().to_glib_none().0,
data.to_glib_none().0,
length,
&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_parser_load_from_file")]
fn load_from_file(&self, filename: impl AsRef<std::path::Path>) -> Result<(), glib::Error> {
unsafe {
let mut error = std::ptr::null_mut();
let is_ok = ffi::json_parser_load_from_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))
}
}
}
#[cfg(feature = "v1_6")]
#[cfg_attr(docsrs, doc(cfg(feature = "v1_6")))]
#[doc(alias = "json_parser_load_from_mapped_file")]
fn load_from_mapped_file(
&self,
filename: impl AsRef<std::path::Path>,
) -> Result<(), glib::Error> {
unsafe {
let mut error = std::ptr::null_mut();
let is_ok = ffi::json_parser_load_from_mapped_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_parser_load_from_stream")]
fn load_from_stream(
&self,
stream: &impl IsA<gio::InputStream>,
cancellable: Option<&impl IsA<gio::Cancellable>>,
) -> Result<(), glib::Error> {
unsafe {
let mut error = std::ptr::null_mut();
let is_ok = ffi::json_parser_load_from_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 = "json_parser_load_from_stream_async")]
fn load_from_stream_async<P: FnOnce(Result<(), glib::Error>) + 'static>(
&self,
stream: &impl IsA<gio::InputStream>,
cancellable: Option<&impl IsA<gio::Cancellable>>,
callback: P,
) {
let main_context = glib::MainContext::ref_thread_default();
let is_main_context_owner = main_context.is_owner();
let has_acquired_main_context = (!is_main_context_owner)
.then(|| main_context.acquire().ok())
.flatten();
assert!(
is_main_context_owner || has_acquired_main_context.is_some(),
"Async operations only allowed if the thread is owning the MainContext"
);
let user_data: Box_<glib::thread_guard::ThreadGuard<P>> =
Box_::new(glib::thread_guard::ThreadGuard::new(callback));
unsafe extern "C" fn load_from_stream_async_trampoline<
P: FnOnce(Result<(), glib::Error>) + 'static,
>(
_source_object: *mut glib::gobject_ffi::GObject,
res: *mut gio::ffi::GAsyncResult,
user_data: glib::ffi::gpointer,
) {
let mut error = std::ptr::null_mut();
ffi::json_parser_load_from_stream_finish(_source_object as *mut _, res, &mut error);
let result = if error.is_null() {
Ok(())
} else {
Err(from_glib_full(error))
};
let callback: Box_<glib::thread_guard::ThreadGuard<P>> =
Box_::from_raw(user_data as *mut _);
let callback: P = callback.into_inner();
callback(result);
}
let callback = load_from_stream_async_trampoline::<P>;
unsafe {
ffi::json_parser_load_from_stream_async(
self.as_ref().to_glib_none().0,
stream.as_ref().to_glib_none().0,
cancellable.map(|p| p.as_ref()).to_glib_none().0,
Some(callback),
Box_::into_raw(user_data) as *mut _,
);
}
}
fn load_from_stream_future(
&self,
stream: &(impl IsA<gio::InputStream> + Clone + 'static),
) -> Pin<Box_<dyn std::future::Future<Output = Result<(), glib::Error>> + 'static>> {
let stream = stream.clone();
Box_::pin(gio::GioFuture::new(self, move |obj, cancellable, send| {
obj.load_from_stream_async(&stream, Some(cancellable), move |res| {
send.resolve(res);
});
}))
}
#[cfg(feature = "v1_10")]
#[cfg_attr(docsrs, doc(cfg(feature = "v1_10")))]
#[doc(alias = "json_parser_set_strict")]
#[doc(alias = "strict")]
fn set_strict(&self, strict: bool) {
unsafe {
ffi::json_parser_set_strict(self.as_ref().to_glib_none().0, strict.into_glib());
}
}
#[cfg(feature = "v1_4")]
#[cfg_attr(docsrs, doc(cfg(feature = "v1_4")))]
#[doc(alias = "json_parser_steal_root")]
fn steal_root(&self) -> Option<Node> {
unsafe { from_glib_full(ffi::json_parser_steal_root(self.as_ref().to_glib_none().0)) }
}
#[cfg(feature = "v1_2")]
#[cfg_attr(docsrs, doc(cfg(feature = "v1_2")))]
fn is_immutable(&self) -> bool {
ObjectExt::property(self.as_ref(), "immutable")
}
#[cfg_attr(feature = "v1_10", deprecated = "Since 1.10")]
#[doc(alias = "array-element")]
fn connect_array_element<F: Fn(&Self, &Array, i32) + 'static>(&self, f: F) -> SignalHandlerId {
unsafe extern "C" fn array_element_trampoline<
P: IsA<Parser>,
F: Fn(&P, &Array, i32) + 'static,
>(
this: *mut ffi::JsonParser,
array: *mut ffi::JsonArray,
index_: std::ffi::c_int,
f: glib::ffi::gpointer,
) {
let f: &F = &*(f as *const F);
f(
Parser::from_glib_borrow(this).unsafe_cast_ref(),
&from_glib_borrow(array),
index_,
)
}
unsafe {
let f: Box_<F> = Box_::new(f);
connect_raw(
self.as_ptr() as *mut _,
c"array-element".as_ptr() as *const _,
Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
array_element_trampoline::<Self, F> as *const (),
)),
Box_::into_raw(f),
)
}
}
#[cfg_attr(feature = "v1_10", deprecated = "Since 1.10")]
#[doc(alias = "array-end")]
fn connect_array_end<F: Fn(&Self, &Array) + 'static>(&self, f: F) -> SignalHandlerId {
unsafe extern "C" fn array_end_trampoline<P: IsA<Parser>, F: Fn(&P, &Array) + 'static>(
this: *mut ffi::JsonParser,
array: *mut ffi::JsonArray,
f: glib::ffi::gpointer,
) {
let f: &F = &*(f as *const F);
f(
Parser::from_glib_borrow(this).unsafe_cast_ref(),
&from_glib_borrow(array),
)
}
unsafe {
let f: Box_<F> = Box_::new(f);
connect_raw(
self.as_ptr() as *mut _,
c"array-end".as_ptr() as *const _,
Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
array_end_trampoline::<Self, F> as *const (),
)),
Box_::into_raw(f),
)
}
}
#[cfg_attr(feature = "v1_10", deprecated = "Since 1.10")]
#[doc(alias = "array-start")]
fn connect_array_start<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
unsafe extern "C" fn array_start_trampoline<P: IsA<Parser>, F: Fn(&P) + 'static>(
this: *mut ffi::JsonParser,
f: glib::ffi::gpointer,
) {
let f: &F = &*(f as *const F);
f(Parser::from_glib_borrow(this).unsafe_cast_ref())
}
unsafe {
let f: Box_<F> = Box_::new(f);
connect_raw(
self.as_ptr() as *mut _,
c"array-start".as_ptr() as *const _,
Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
array_start_trampoline::<Self, F> as *const (),
)),
Box_::into_raw(f),
)
}
}
#[cfg_attr(feature = "v1_10", deprecated = "Since 1.10")]
#[doc(alias = "object-end")]
fn connect_object_end<F: Fn(&Self, &Object) + 'static>(&self, f: F) -> SignalHandlerId {
unsafe extern "C" fn object_end_trampoline<P: IsA<Parser>, F: Fn(&P, &Object) + 'static>(
this: *mut ffi::JsonParser,
object: *mut ffi::JsonObject,
f: glib::ffi::gpointer,
) {
let f: &F = &*(f as *const F);
f(
Parser::from_glib_borrow(this).unsafe_cast_ref(),
&from_glib_borrow(object),
)
}
unsafe {
let f: Box_<F> = Box_::new(f);
connect_raw(
self.as_ptr() as *mut _,
c"object-end".as_ptr() as *const _,
Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
object_end_trampoline::<Self, F> as *const (),
)),
Box_::into_raw(f),
)
}
}
#[cfg_attr(feature = "v1_10", deprecated = "Since 1.10")]
#[doc(alias = "object-member")]
fn connect_object_member<F: Fn(&Self, &Object, &str) + 'static>(
&self,
f: F,
) -> SignalHandlerId {
unsafe extern "C" fn object_member_trampoline<
P: IsA<Parser>,
F: Fn(&P, &Object, &str) + 'static,
>(
this: *mut ffi::JsonParser,
object: *mut ffi::JsonObject,
member_name: *mut std::ffi::c_char,
f: glib::ffi::gpointer,
) {
let f: &F = &*(f as *const F);
f(
Parser::from_glib_borrow(this).unsafe_cast_ref(),
&from_glib_borrow(object),
&glib::GString::from_glib_borrow(member_name),
)
}
unsafe {
let f: Box_<F> = Box_::new(f);
connect_raw(
self.as_ptr() as *mut _,
c"object-member".as_ptr() as *const _,
Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
object_member_trampoline::<Self, F> as *const (),
)),
Box_::into_raw(f),
)
}
}
#[cfg_attr(feature = "v1_10", deprecated = "Since 1.10")]
#[doc(alias = "object-start")]
fn connect_object_start<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
unsafe extern "C" fn object_start_trampoline<P: IsA<Parser>, F: Fn(&P) + 'static>(
this: *mut ffi::JsonParser,
f: glib::ffi::gpointer,
) {
let f: &F = &*(f as *const F);
f(Parser::from_glib_borrow(this).unsafe_cast_ref())
}
unsafe {
let f: Box_<F> = Box_::new(f);
connect_raw(
self.as_ptr() as *mut _,
c"object-start".as_ptr() as *const _,
Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
object_start_trampoline::<Self, F> as *const (),
)),
Box_::into_raw(f),
)
}
}
#[cfg_attr(feature = "v1_10", deprecated = "Since 1.10")]
#[doc(alias = "parse-end")]
fn connect_parse_end<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
unsafe extern "C" fn parse_end_trampoline<P: IsA<Parser>, F: Fn(&P) + 'static>(
this: *mut ffi::JsonParser,
f: glib::ffi::gpointer,
) {
let f: &F = &*(f as *const F);
f(Parser::from_glib_borrow(this).unsafe_cast_ref())
}
unsafe {
let f: Box_<F> = Box_::new(f);
connect_raw(
self.as_ptr() as *mut _,
c"parse-end".as_ptr() as *const _,
Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
parse_end_trampoline::<Self, F> as *const (),
)),
Box_::into_raw(f),
)
}
}
#[cfg_attr(feature = "v1_10", deprecated = "Since 1.10")]
#[doc(alias = "parse-start")]
fn connect_parse_start<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
unsafe extern "C" fn parse_start_trampoline<P: IsA<Parser>, F: Fn(&P) + 'static>(
this: *mut ffi::JsonParser,
f: glib::ffi::gpointer,
) {
let f: &F = &*(f as *const F);
f(Parser::from_glib_borrow(this).unsafe_cast_ref())
}
unsafe {
let f: Box_<F> = Box_::new(f);
connect_raw(
self.as_ptr() as *mut _,
c"parse-start".as_ptr() as *const _,
Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
parse_start_trampoline::<Self, F> as *const (),
)),
Box_::into_raw(f),
)
}
}
#[cfg(feature = "v1_10")]
#[cfg_attr(docsrs, doc(cfg(feature = "v1_10")))]
#[doc(alias = "strict")]
fn connect_strict_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
unsafe extern "C" fn notify_strict_trampoline<P: IsA<Parser>, F: Fn(&P) + 'static>(
this: *mut ffi::JsonParser,
_param_spec: glib::ffi::gpointer,
f: glib::ffi::gpointer,
) {
let f: &F = &*(f as *const F);
f(Parser::from_glib_borrow(this).unsafe_cast_ref())
}
unsafe {
let f: Box_<F> = Box_::new(f);
connect_raw(
self.as_ptr() as *mut _,
c"notify::strict".as_ptr() as *const _,
Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
notify_strict_trampoline::<Self, F> as *const (),
)),
Box_::into_raw(f),
)
}
}
}
impl<O: IsA<Parser>> ParserExt for O {}