1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
// This file was generated by gir (https://github.com/gtk-rs/gir)
// from
// from gir-files (https://github.com/gtk-rs/gir-files.git)
// DO NOT EDIT

use glib::{
    prelude::*,
    signal::{connect_raw, SignalHandlerId},
    translate::*,
};
use std::{boxed::Box as Box_, fmt, mem::transmute, pin::Pin, ptr};

glib::wrapper! {
    ///
    ///
    /// ## Properties
    ///
    ///
    /// #### `case-sensitive`
    ///  Readable | Writeable
    ///
    /// # Implements
    ///
    /// [`trait@glib::ObjectExt`]
    #[doc(alias = "DzlFuzzyIndexBuilder")]
    pub struct FuzzyIndexBuilder(Object<ffi::DzlFuzzyIndexBuilder, ffi::DzlFuzzyIndexBuilderClass>);

    match fn {
        type_ => || ffi::dzl_fuzzy_index_builder_get_type(),
    }
}

impl FuzzyIndexBuilder {
    #[doc(alias = "dzl_fuzzy_index_builder_new")]
    pub fn new() -> FuzzyIndexBuilder {
        assert_initialized_main_thread!();
        unsafe { from_glib_full(ffi::dzl_fuzzy_index_builder_new()) }
    }

    #[doc(alias = "dzl_fuzzy_index_builder_get_case_sensitive")]
    #[doc(alias = "get_case_sensitive")]
    pub fn is_case_sensitive(&self) -> bool {
        unsafe {
            from_glib(ffi::dzl_fuzzy_index_builder_get_case_sensitive(
                self.to_glib_none().0,
            ))
        }
    }

    /// Returns the document that was inserted in a previous call to
    /// [`insert()`][Self::insert()].
    ///
    /// # Returns
    ///
    /// A [`glib::Variant`][struct@crate::glib::Variant]
    #[doc(alias = "dzl_fuzzy_index_builder_get_document")]
    #[doc(alias = "get_document")]
    pub fn document(&self, document_id: u64) -> Option<glib::Variant> {
        unsafe {
            from_glib_none(ffi::dzl_fuzzy_index_builder_get_document(
                self.to_glib_none().0,
                document_id,
            ))
        }
    }

    /// Inserts `document` into the index using `key` as the lookup key.
    ///
    /// If a matching document (checked by hashing `document`) has already
    /// been inserted, only a single instance of the document will be stored.
    ///
    /// If `document` is floating, it will be consumed.
    ///
    /// `priority` may be used to group results by priority. Priority must be
    /// less than 256.
    /// ## `key`
    /// The UTF-8 encoded key for the document
    /// ## `document`
    /// The document to store
    /// ## `priority`
    /// An optional priority for the keyword.
    ///
    /// # Returns
    ///
    /// The document id registered for `document`.
    #[doc(alias = "dzl_fuzzy_index_builder_insert")]
    pub fn insert(&self, key: &str, document: &glib::Variant, priority: u32) -> u64 {
        unsafe {
            ffi::dzl_fuzzy_index_builder_insert(
                self.to_glib_none().0,
                key.to_glib_none().0,
                document.to_glib_none().0,
                priority,
            )
        }
    }

    #[doc(alias = "dzl_fuzzy_index_builder_set_case_sensitive")]
    pub fn set_case_sensitive(&self, case_sensitive: bool) {
        unsafe {
            ffi::dzl_fuzzy_index_builder_set_case_sensitive(
                self.to_glib_none().0,
                case_sensitive.into_glib(),
            );
        }
    }

    #[doc(alias = "dzl_fuzzy_index_builder_set_metadata")]
    pub fn set_metadata(&self, key: &str, value: &glib::Variant) {
        unsafe {
            ffi::dzl_fuzzy_index_builder_set_metadata(
                self.to_glib_none().0,
                key.to_glib_none().0,
                value.to_glib_none().0,
            );
        }
    }

    #[doc(alias = "dzl_fuzzy_index_builder_set_metadata_string")]
    pub fn set_metadata_string(&self, key: &str, value: &str) {
        unsafe {
            ffi::dzl_fuzzy_index_builder_set_metadata_string(
                self.to_glib_none().0,
                key.to_glib_none().0,
                value.to_glib_none().0,
            );
        }
    }

    #[doc(alias = "dzl_fuzzy_index_builder_set_metadata_uint32")]
    pub fn set_metadata_uint32(&self, key: &str, value: u32) {
        unsafe {
            ffi::dzl_fuzzy_index_builder_set_metadata_uint32(
                self.to_glib_none().0,
                key.to_glib_none().0,
                value,
            );
        }
    }

    #[doc(alias = "dzl_fuzzy_index_builder_set_metadata_uint64")]
    pub fn set_metadata_uint64(&self, key: &str, value: u64) {
        unsafe {
            ffi::dzl_fuzzy_index_builder_set_metadata_uint64(
                self.to_glib_none().0,
                key.to_glib_none().0,
                value,
            );
        }
    }

    #[doc(alias = "dzl_fuzzy_index_builder_write")]
    pub fn write(
        &self,
        file: &impl IsA<gio::File>,
        io_priority: i32,
        cancellable: Option<&impl IsA<gio::Cancellable>>,
    ) -> Result<(), glib::Error> {
        unsafe {
            let mut error = ptr::null_mut();
            let is_ok = ffi::dzl_fuzzy_index_builder_write(
                self.to_glib_none().0,
                file.as_ref().to_glib_none().0,
                io_priority,
                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))
            }
        }
    }

    /// Builds and writes the index to `file`. The file format is a
    /// GVariant on disk and can be loaded and searched using
    /// `FuzzyIndex`.
    /// ## `file`
    /// A [`gio::File`][crate::gio::File] to write the index to
    /// ## `io_priority`
    /// The priority for IO operations
    /// ## `cancellable`
    /// An optional [`gio::Cancellable`][crate::gio::Cancellable] or [`None`]
    /// ## `callback`
    /// A callback for completion or [`None`]
    #[doc(alias = "dzl_fuzzy_index_builder_write_async")]
    pub fn write_async<P: FnOnce(Result<(), glib::Error>) + 'static>(
        &self,
        file: &impl IsA<gio::File>,
        io_priority: glib::Priority,
        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 write_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_builder_write_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 = write_async_trampoline::<P>;
        unsafe {
            ffi::dzl_fuzzy_index_builder_write_async(
                self.to_glib_none().0,
                file.as_ref().to_glib_none().0,
                io_priority.into_glib(),
                cancellable.map(|p| p.as_ref()).to_glib_none().0,
                Some(callback),
                Box_::into_raw(user_data) as *mut _,
            );
        }
    }

    pub fn write_future(
        &self,
        file: &(impl IsA<gio::File> + Clone + 'static),
        io_priority: glib::Priority,
    ) -> 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.write_async(&file, io_priority, Some(cancellable), move |res| {
                send.resolve(res);
            });
        }))
    }

    #[doc(alias = "case-sensitive")]
    pub fn connect_case_sensitive_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
        unsafe extern "C" fn notify_case_sensitive_trampoline<
            F: Fn(&FuzzyIndexBuilder) + 'static,
        >(
            this: *mut ffi::DzlFuzzyIndexBuilder,
            _param_spec: glib::ffi::gpointer,
            f: glib::ffi::gpointer,
        ) {
            let f: &F = &*(f as *const F);
            f(&from_glib_borrow(this))
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"notify::case-sensitive\0".as_ptr() as *const _,
                Some(transmute::<_, unsafe extern "C" fn()>(
                    notify_case_sensitive_trampoline::<F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }
}

impl Default for FuzzyIndexBuilder {
    fn default() -> Self {
        Self::new()
    }
}

impl fmt::Display for FuzzyIndexBuilder {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.write_str("FuzzyIndexBuilder")
    }
}