anyrun_interface/
lib.rs

1use abi_stable::{
2    declare_root_module_statics,
3    library::RootModule,
4    package_version_strings,
5    sabi_types::VersionStrings,
6    std_types::{ROption, RString, RVec},
7    StableAbi,
8};
9
10#[repr(C)]
11#[derive(StableAbi)]
12#[sabi(kind(Prefix(prefix_ref = PluginRef)))]
13#[sabi(missing_field(panic))]
14pub struct Plugin {
15    pub init: extern "C" fn(RString),
16    pub info: extern "C" fn() -> PluginInfo,
17    pub get_matches: extern "C" fn(RString) -> u64,
18    pub poll_matches: extern "C" fn(u64) -> PollResult,
19    pub handle_selection: extern "C" fn(Match) -> HandleResult,
20}
21
22/// Info of the plugin. Used for the main UI
23#[repr(C)]
24#[derive(StableAbi, Debug)]
25pub struct PluginInfo {
26    pub name: RString,
27    /// The icon name from the icon theme in use
28    pub icon: RString,
29}
30
31/// Represents a match from a plugin
32///
33/// The `title` and `description` support pango markup when `use_pango` is set to true.
34/// Refer to [Pango Markup](https://docs.gtk.org/Pango/pango_markup.html) for how to use pango markup.
35#[repr(C)]
36#[derive(StableAbi, Clone)]
37pub struct Match {
38    pub title: RString,
39    pub description: ROption<RString>,
40    /// Whether the title and description should be interpreted as pango markup.
41    pub use_pango: bool,
42    /// The icon name from the icon theme in use
43    pub icon: ROption<RString>,
44    /// For runners to differentiate between the matches. Not required.
45    pub id: ROption<u64>,
46}
47
48/// For determining how anyrun should proceed after the plugin has handled a match selection
49#[repr(C)]
50#[derive(StableAbi)]
51pub enum HandleResult {
52    /// Shut down the program
53    Close,
54    /// Refresh the items. Useful if the runner wants to alter results in place.
55    /// The inner value can set an exclusive mode for the plugin.
56    Refresh(bool),
57    /// Copy the content, due to how copying works it must be done like this.
58    Copy(RVec<u8>),
59    /// Output the content to stdout, printing to stdout has issues in plugins.
60    Stdout(RVec<u8>),
61}
62
63#[repr(C)]
64#[derive(StableAbi)]
65pub enum PollResult {
66    Ready(RVec<Match>),
67    Pending,
68    Cancelled,
69}
70
71impl RootModule for PluginRef {
72    declare_root_module_statics! {PluginRef}
73
74    const BASE_NAME: &'static str = "anyrun_plugin";
75    const NAME: &'static str = "anyrun_plugin";
76    const VERSION_STRINGS: VersionStrings = package_version_strings!();
77}