Return result in resolve_store_path

pull/1860/head
Tony 8 months ago
parent c3748b39fe
commit d88fcb5c8a
No known key found for this signature in database
GPG Key ID: 34BDD3EA27824956

@ -6,7 +6,7 @@
- Renamed `StoreCollection` to `StoreState`
- `StoreBuilder::build` now returns a `Result`
- `StoreExt::store` now returns `Arc<Store>` instead of `Store`
- `StoreExt::store` now returns `Result<Arc<Store>>`
Other Changes:

@ -17,7 +17,7 @@ fn main() {
.plugin(tauri_plugin_store::Builder::new().build())
.setup(|app| {
// Init store and load it from disk
let store = app.store("settings.json");
let store = app.store("settings.json")?;
app.listen("store://change", |event| {
dbg!(event);
});

@ -134,7 +134,7 @@ async fn create_or_existing_store<R: Runtime>(
serialize_fn_name,
deserialize_fn_name,
)?;
let (_, rid) = builder.build_or_existing_inner();
let (_, rid) = builder.build_or_existing_inner()?;
Ok(rid)
}
@ -145,7 +145,7 @@ async fn get_store<R: Runtime>(
path: PathBuf,
) -> Result<Option<ResourceId>> {
let stores = store_state.stores.lock().unwrap();
Ok(stores.get(&resolve_store_path(&app, path)).copied())
Ok(stores.get(&resolve_store_path(&app, path)?).copied())
}
#[tauri::command]
@ -244,7 +244,7 @@ async fn save<R: Runtime>(app: AppHandle<R>, rid: ResourceId) -> Result<()> {
pub trait StoreExt<R: Runtime> {
/// Create a store or get an existing store with default settings at path
fn store(&self, path: impl AsRef<Path>) -> Arc<Store<R>>;
fn store(&self, path: impl AsRef<Path>) -> Result<Arc<Store<R>>>;
/// Create a store with default settings
fn create_store(&self, path: impl AsRef<Path>) -> Result<Arc<Store<R>>>;
/// Get a store builder
@ -254,7 +254,7 @@ pub trait StoreExt<R: Runtime> {
}
impl<R: Runtime, T: Manager<R>> StoreExt<R> for T {
fn store(&self, path: impl AsRef<Path>) -> Arc<Store<R>> {
fn store(&self, path: impl AsRef<Path>) -> Result<Arc<Store<R>>> {
StoreBuilder::new(self.app_handle(), path).build_or_existing()
}
@ -270,7 +270,7 @@ impl<R: Runtime, T: Manager<R>> StoreExt<R> for T {
let collection = self.state::<StoreState>();
let stores = collection.stores.lock().unwrap();
stores
.get(&resolve_store_path(self.app_handle(), path.as_ref()))
.get(&resolve_store_path(self.app_handle(), path.as_ref()).ok()?)
.and_then(|rid| self.resources_table().get(*rid).ok())
}
}

@ -27,13 +27,8 @@ pub type DeserializeFn =
pub(crate) fn resolve_store_path<R: Runtime>(
app: &AppHandle<R>,
path: impl AsRef<Path>,
) -> PathBuf {
dunce::simplified(
&app.path()
.resolve(path, BaseDirectory::AppData)
.expect("failed to resolve app dir"),
)
.to_path_buf()
) -> crate::Result<PathBuf> {
Ok(dunce::simplified(&app.path().resolve(path, BaseDirectory::AppData)?).to_path_buf())
}
/// Builds a [`Store`]
@ -61,13 +56,12 @@ impl<R: Runtime> StoreBuilder<R> {
/// ```
pub fn new<M: Manager<R>, P: AsRef<Path>>(manager: &M, path: P) -> Self {
let app = manager.app_handle().clone();
let path = resolve_store_path(&app, path);
let state = app.state::<StoreState>();
let serialize_fn = state.default_serialize;
let deserialize_fn = state.default_deserialize;
Self {
app,
path,
path: path.as_ref().to_path_buf(),
defaults: None,
serialize_fn,
deserialize_fn,
@ -217,35 +211,15 @@ impl<R: Runtime> StoreBuilder<R> {
Ok((store, rid))
}
pub(crate) fn build_or_existing_inner(mut self) -> (Arc<Store<R>>, ResourceId) {
pub(crate) fn build_or_existing_inner(self) -> crate::Result<(Arc<Store<R>>, ResourceId)> {
{
let state = self.app.state::<StoreState>();
let mut stores = state.stores.lock().unwrap();
let stores = state.stores.lock().unwrap();
if let Some(rid) = stores.get(&self.path) {
(self.app.resources_table().get(*rid).unwrap(), *rid)
} else {
let mut store_inner = StoreInner::new(
self.app.clone(),
self.path.clone(),
self.defaults.take(),
self.serialize_fn,
self.deserialize_fn,
);
if self.load_on_build {
let _ = store_inner.load();
return Ok((self.app.resources_table().get(*rid).unwrap(), *rid));
}
let store = Store {
auto_save: self.auto_save,
auto_save_debounce_sender: Arc::new(Mutex::new(None)),
store: Arc::new(Mutex::new(store_inner)),
};
let store = Arc::new(store);
let rid = self.app.resources_table().add_arc(store.clone());
stores.insert(self.path, rid);
(store, rid)
}
self.build_inner()
}
/// Builds the [`Store`], also see [`build_or_existing`](Self::build_or_existing).
@ -283,9 +257,9 @@ impl<R: Runtime> StoreBuilder<R> {
/// Ok(())
/// });
/// ```
pub fn build_or_existing(self) -> Arc<Store<R>> {
let (store, _) = self.build_or_existing_inner();
store
pub fn build_or_existing(self) -> crate::Result<Arc<Store<R>>> {
let (store, _) = self.build_or_existing_inner()?;
Ok(store)
}
}

Loading…
Cancel
Save