findex_plugin/lib.rs
1#[cfg(feature = "findex_internals")]
2pub mod findex_internal;
3
4use abi_stable::std_types::*;
5
6/// This struct is used to represent results by plugins and internal code of Findex.
7#[derive(Clone)]
8#[repr(C)]
9pub struct FResult {
10 /// Name of the result
11 pub name: RString,
12 /// Optional description of the result
13 pub desc: ROption<RString>,
14 /// The command to execute when the user presses enter
15 pub cmd: ApplicationCommand,
16 /// The icon of the result
17 pub icon: RString,
18 /// Score of the result. This will be used to sort multiple results
19 pub score: isize,
20}
21
22#[derive(Clone, PartialEq)]
23#[repr(C)]
24pub enum ApplicationCommand {
25 /// Exact command to execute
26 Command(RString),
27 /// AppId of GIO AppInfo
28 Id(RString),
29 /// Useful for results from plugins like calculator.
30 None,
31}
32
33/// This macro is used to define a Findex plugin.
34///
35/// Example usage:
36/// ```rust
37/// use findex_plugin::{define_plugin, FResult};
38/// use abi_stable::std_types::*;
39///
40/// fn init(config: &RHashMap<RString, RString>) -> RResult<(), RString> {
41/// // Set up your plugin using the config if necessary
42/// // Return RErr if something went wrong
43///
44/// // Returning this indicates that the plugin initalization is successful
45/// ROk(())
46/// }
47///
48/// fn handle_query(query: RStr) -> RVec<FResult> {
49/// let mut result = vec![];
50///
51/// /* Do stuff here */
52///
53/// RVec::from(result)
54/// }
55///
56/// define_plugin!("prefix!", init, handle_query);
57/// // or, add a shortcut key
58/// define_plugin!("prefix!", "<Ctrl><Shift>p", init, handle_query);
59/// ```
60///
61/// Refer to the `README.md` of this crate for more detailed explanation
62#[macro_export]
63macro_rules! define_plugin {
64 ($prefix:literal, $init_function:ident, $query_handler:ident) => {
65 #[no_mangle]
66 #[used]
67 pub static FINDEX_PLUGIN_PREFIX: &'static str = $prefix;
68
69 #[no_mangle]
70 extern "C" fn findex_plugin_init(
71 config: &RHashMap<RString, RString>,
72 ) -> RResult<(), RString> {
73 $init_function(config)
74 }
75
76 #[no_mangle]
77 extern "C" fn findex_plugin_query_handler(query: RStr) -> RVec<FResult> {
78 $query_handler(query)
79 }
80 };
81
82 ($prefix:literal, $keyboard_shortcut:literal, $init_function:ident, $query_handler:ident) => {
83 #[no_mangle]
84 #[used]
85 pub static FINDEX_PLUGIN_PREFIX: &'static str = $prefix;
86
87 #[no_mangle]
88 #[used]
89 pub static FINDEX_PLUGIN_KEYBOARD_SHORTCUT: &'static str = $keyboard_shortcut;
90
91 #[no_mangle]
92 extern "C" fn findex_plugin_init(
93 config: &RHashMap<RString, RString>,
94 ) -> RResult<(), RString> {
95 $init_function(config)
96 }
97
98 #[no_mangle]
99 extern "C" fn findex_plugin_query_handler(query: RStr) -> RVec<FResult> {
100 $query_handler(query)
101 }
102 };
103}