autoclip_core/lib.rs
1pub static CORE_VERSION: &str = env!("CARGO_PKG_VERSION");
2pub static RUSTC_VERSION: &str = env!("RUSTC_VERSION");
3
4/// Plugins must implement this trait.
5/// This trait is used to trigger events from the app.
6pub trait AutoclipPlugin {
7 /// This event will be triggered when the content of the clipboard is updated.
8 /// If the plugin does not perform any modification for the content,
9 /// this function must return None, instead of `Some(contents.to_string())` .
10 fn on_clip(&self, contents: &str) -> Option<String>;
11}
12
13/// An internal struct to load plugins from the app.
14/// The version of rustc and autoclip-core may be checked on loading.
15pub struct PluginDeclaration {
16 /// The version of rustc, used to compile the core.
17 /// This should be set on build-time.
18 pub rustc_version: &'static str,
19
20 /// The version of the core.
21 /// This also should be set on build-time.
22 pub core_version: &'static str,
23
24 /// The function to register a plugin to the app.
25 /// In this function, `PluginRegistrar::register` must be called.
26 pub register: unsafe extern "C" fn(&mut dyn PluginRegistrar),
27}
28
29/// The app must implement this trait.
30/// This trait is used to give plugins a method to register themselves to the app.
31pub trait PluginRegistrar {
32 fn register(&mut self, name: &str, entrypoint: Box<dyn AutoclipPlugin>);
33}
34
35/// An useful macro to export plugins.
36/// Using only this macro, each plugin can integrate with the app easily.
37#[macro_export]
38macro_rules! export_plugin {
39 ($name:expr, $implementation:expr) => {
40 #[doc(hidden)]
41 #[no_mangle]
42 pub static plugin_declaration: $crate::PluginDeclaration = $crate::PluginDeclaration {
43 rustc_version: $crate::RUSTC_VERSION,
44 core_version: $crate::CORE_VERSION,
45 register,
46 };
47
48 #[allow(improper_ctypes_definitions)]
49 extern "C" fn register(registrar: &mut dyn PluginRegistrar) {
50 registrar.register($name, Box::new($implementation));
51 }
52 };
53}