tauri_plugin_keylight/
lib.rs1use keylight::{Keylight, KeylightConfig};
2use std::sync::Arc;
3use tauri::{
4 plugin::{Builder, TauriPlugin},
5 Manager, Runtime, State,
6};
7
8struct KeylightState(Arc<Keylight>);
9
10#[tauri::command]
11fn activate(state: State<'_, KeylightState>, key: String) -> Result<bool, String> {
12 state
13 .0
14 .activate(&key)
15 .map(|r| r.activated)
16 .map_err(|e| e.to_string())
17}
18
19#[tauri::command]
20fn validate(state: State<'_, KeylightState>) -> Result<bool, String> {
21 state
22 .0
23 .validate()
24 .map(|r| r.valid)
25 .map_err(|e| e.to_string())
26}
27
28#[tauri::command]
29fn has_entitlement(state: State<'_, KeylightState>, feature: String) -> bool {
30 state.0.has_entitlement(&feature)
31}
32
33pub fn init<R: Runtime>(config: KeylightConfig) -> TauriPlugin<R> {
35 Builder::new("keylight")
36 .invoke_handler(tauri::generate_handler![
37 activate,
38 validate,
39 has_entitlement
40 ])
41 .setup(move |app, _api| {
42 let kl = Keylight::new(config.clone())?;
43 app.manage(KeylightState(Arc::new(kl)));
44 Ok(())
45 })
46 .build()
47}