use glib::{prelude::*, translate::*};
use std::{boxed::Box as Box_, fmt, pin::Pin, ptr};
glib::wrapper! {
#[doc(alias = "DzlFuzzyIndex")]
pub struct FuzzyIndex(Object<ffi::DzlFuzzyIndex, ffi::DzlFuzzyIndexClass>);
match fn {
type_ => || ffi::dzl_fuzzy_index_get_type(),
}
}
impl FuzzyIndex {
#[doc(alias = "dzl_fuzzy_index_new")]
pub fn new() -> FuzzyIndex {
assert_initialized_main_thread!();
unsafe { from_glib_full(ffi::dzl_fuzzy_index_new()) }
}
#[doc(alias = "dzl_fuzzy_index_get_metadata")]
#[doc(alias = "get_metadata")]
pub fn metadata(&self, key: &str) -> Option<glib::Variant> {
unsafe {
from_glib_full(ffi::dzl_fuzzy_index_get_metadata(
self.to_glib_none().0,
key.to_glib_none().0,
))
}
}
#[doc(alias = "dzl_fuzzy_index_get_metadata_string")]
#[doc(alias = "get_metadata_string")]
pub fn metadata_string(&self, key: &str) -> Option<glib::GString> {
unsafe {
from_glib_none(ffi::dzl_fuzzy_index_get_metadata_string(
self.to_glib_none().0,
key.to_glib_none().0,
))
}
}
#[doc(alias = "dzl_fuzzy_index_get_metadata_uint32")]
#[doc(alias = "get_metadata_uint32")]
pub fn metadata_uint32(&self, key: &str) -> u32 {
unsafe {
ffi::dzl_fuzzy_index_get_metadata_uint32(self.to_glib_none().0, key.to_glib_none().0)
}
}
#[doc(alias = "dzl_fuzzy_index_get_metadata_uint64")]
#[doc(alias = "get_metadata_uint64")]
pub fn metadata_uint64(&self, key: &str) -> u64 {
unsafe {
ffi::dzl_fuzzy_index_get_metadata_uint64(self.to_glib_none().0, key.to_glib_none().0)
}
}
#[doc(alias = "dzl_fuzzy_index_load_file")]
pub fn load_file(
&self,
file: &impl IsA<gio::File>,
cancellable: Option<&impl IsA<gio::Cancellable>>,
) -> Result<(), glib::Error> {
unsafe {
let mut error = ptr::null_mut();
let is_ok = ffi::dzl_fuzzy_index_load_file(
self.to_glib_none().0,
file.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 = "dzl_fuzzy_index_load_file_async")]
pub fn load_file_async<P: FnOnce(Result<(), glib::Error>) + 'static>(
&self,
file: &impl IsA<gio::File>,
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_file_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 = ptr::null_mut();
let _ =
ffi::dzl_fuzzy_index_load_file_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_file_async_trampoline::<P>;
unsafe {
ffi::dzl_fuzzy_index_load_file_async(
self.to_glib_none().0,
file.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 _,
);
}
}
pub fn load_file_future(
&self,
file: &(impl IsA<gio::File> + Clone + 'static),
) -> Pin<Box_<dyn std::future::Future<Output = Result<(), glib::Error>> + 'static>> {
let file = file.clone();
Box_::pin(gio::GioFuture::new(self, move |obj, cancellable, send| {
obj.load_file_async(&file, Some(cancellable), move |res| {
send.resolve(res);
});
}))
}
#[doc(alias = "dzl_fuzzy_index_query_async")]
pub fn query_async<P: FnOnce(Result<gio::ListModel, glib::Error>) + 'static>(
&self,
query: &str,
max_matches: u32,
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 query_async_trampoline<
P: FnOnce(Result<gio::ListModel, glib::Error>) + 'static,
>(
_source_object: *mut glib::gobject_ffi::GObject,
res: *mut gio::ffi::GAsyncResult,
user_data: glib::ffi::gpointer,
) {
let mut error = ptr::null_mut();
let ret = ffi::dzl_fuzzy_index_query_finish(_source_object as *mut _, res, &mut error);
let result = if error.is_null() {
Ok(from_glib_full(ret))
} 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 = query_async_trampoline::<P>;
unsafe {
ffi::dzl_fuzzy_index_query_async(
self.to_glib_none().0,
query.to_glib_none().0,
max_matches,
cancellable.map(|p| p.as_ref()).to_glib_none().0,
Some(callback),
Box_::into_raw(user_data) as *mut _,
);
}
}
pub fn query_future(
&self,
query: &str,
max_matches: u32,
) -> Pin<Box_<dyn std::future::Future<Output = Result<gio::ListModel, glib::Error>> + 'static>>
{
let query = String::from(query);
Box_::pin(gio::GioFuture::new(self, move |obj, cancellable, send| {
obj.query_async(&query, max_matches, Some(cancellable), move |res| {
send.resolve(res);
});
}))
}
}
impl Default for FuzzyIndex {
fn default() -> Self {
Self::new()
}
}
impl fmt::Display for FuzzyIndex {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str("FuzzyIndex")
}
}