Skip to main content

tauri_plugin_libsql/
desktop.rs

1use serde::de::DeserializeOwned;
2use std::path::PathBuf;
3use tauri::{plugin::PluginApi, AppHandle, Runtime};
4
5use crate::models::*;
6
7/// Plugin configuration
8#[derive(Debug, Clone, Default)]
9pub struct Config {
10    /// Base path for relative database paths. Defaults to current working directory.
11    pub base_path: Option<PathBuf>,
12    /// Default encryption configuration for all databases.
13    /// Can be overridden per-database when loading.
14    pub encryption: Option<EncryptionConfig>,
15}
16
17pub fn init<R: Runtime, C: DeserializeOwned>(
18    _app: &AppHandle<R>,
19    _api: PluginApi<R, C>,
20    config: Config,
21) -> crate::Result<Libsql> {
22    Ok(Libsql(config))
23}
24
25/// Access to the libsql APIs.
26pub struct Libsql(Config);
27
28impl Libsql {
29    pub fn ping(&self, payload: PingRequest) -> crate::Result<PingResponse> {
30        Ok(PingResponse {
31            value: payload.value,
32        })
33    }
34
35    /// Get the configured base path for databases
36    pub fn base_path(&self) -> PathBuf {
37        self.0
38            .base_path
39            .clone()
40            .unwrap_or_else(|| std::env::current_dir().unwrap_or_else(|_| PathBuf::from(".")))
41    }
42
43    /// Get the default encryption config
44    pub fn encryption(&self) -> Option<&EncryptionConfig> {
45        self.0.encryption.as_ref()
46    }
47}