{{#if license_header}} {{ license_header }} {{/if}} use serde::{ser::Serializer, Serialize}; use serde_json::Value as JsonValue; use tauri::{command, plugin::Plugin, AppHandle, Invoke, Manager, Runtime, State, Window}; use std::{collections::HashMap, sync::Mutex}; type Result = std::result::Result; #[derive(Debug, thiserror::Error)] pub enum Error { #[error(transparent)] Io(#[from] std::io::Error), } impl Serialize for Error { fn serialize(&self, serializer: S) -> std::result::Result where S: Serializer, { serializer.serialize_str(self.to_string().as_ref()) } } #[derive(Default)] struct MyState(Mutex>); #[command] async fn execute( _app: AppHandle, _window: Window, state: State<'_, MyState>, ) -> Result { state.0.lock().unwrap().insert("key".into(), "value".into()); Ok("success".to_string()) } /// Tauri plugin. pub struct YourPlugin { invoke_handler: Box) + Send + Sync>, } impl Default for YourPlugin { fn default() -> Self { Self { invoke_handler: Box::new(tauri::generate_handler![execute]), } } } impl Plugin for YourPlugin { fn name(&self) -> &'static str { "{{ plugin_name }}" } fn initialize(&mut self, app: &AppHandle, _config: JsonValue) -> tauri::plugin::Result<()> { app.manage(MyState::default()); Ok(()) } fn extend_api(&mut self, message: Invoke) { (self.invoke_handler)(message) } }