1#![forbid(unsafe_code)]
7#![cfg_attr(docsrs, feature(doc_auto_cfg))]
8#![doc = include_str!("../README.md")]
9#![doc(html_favicon_url = "https://tb.dev.br/tauri-store/favicon.ico")]
10
11mod command;
12mod manager;
13mod vue;
14
15use std::collections::HashSet;
16use std::path::PathBuf;
17use std::time::Duration;
18use tauri::plugin::TauriPlugin;
19use tauri::{AppHandle, RunEvent, Runtime};
20use tauri_store::CollectionBuilder;
21
22#[cfg(feature = "unstable-migration")]
23use tauri_store::Migrator;
24
25pub use manager::ManagerExt;
26pub use tauri_store::prelude::*;
27pub use vue::{Vue, VueMarker};
28
29#[cfg(feature = "unstable-migration")]
30pub use tauri_store::{Migration, MigrationContext};
31
32type Marker = VueMarker;
34
35#[derive(CollectionBuilder)]
37pub struct Builder<R: Runtime> {
38 path: Option<PathBuf>,
39 default_save_strategy: SaveStrategy,
40 autosave: Option<Duration>,
41 on_load: Option<Box<OnLoadFn<R, Marker>>>,
42 pretty: bool,
43 save_denylist: HashSet<StoreId>,
44 sync_denylist: HashSet<StoreId>,
45
46 #[cfg(feature = "unstable-migration")]
47 migrator: Migrator,
48}
49
50impl<R: Runtime> Builder<R> {
51 pub fn build(self) -> TauriPlugin<R> {
53 tauri::plugin::Builder::new("vue")
54 .setup(|app, _| setup(app, self))
55 .on_event(on_event)
56 .invoke_handler(tauri::generate_handler![
57 command::clear_autosave,
58 command::get_default_save_strategy,
59 command::get_store_collection_path,
60 command::get_save_strategy,
61 command::get_store_ids,
62 command::get_store_path,
63 command::get_store_state,
64 command::load,
65 command::patch,
66 command::save,
67 command::save_all,
68 command::save_all_now,
69 command::save_now,
70 command::save_some,
71 command::save_some_now,
72 command::set_autosave,
73 command::set_save_strategy,
74 command::set_store_collection_path,
75 command::set_store_options,
76 command::unload
77 ])
78 .build()
79 }
80}
81
82fn setup<R>(app: &AppHandle<R>, builder: Builder<R>) -> BoxResult<()>
83where
84 R: Runtime,
85{
86 builder.build_collection(app)?;
87 Ok(())
88}
89
90fn on_event<R>(app: &AppHandle<R>, event: &RunEvent)
91where
92 R: Runtime,
93{
94 if let RunEvent::Exit = event {
95 let _ = app.vue().0.on_exit();
96 }
97}
98
99pub fn init<R: Runtime>() -> TauriPlugin<R> {
101 Builder::default().build()
102}