This repository has been archived by the owner on Sep 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Introduce storage migrations via Importable/Exportable Storage …
…traits, and other trait-based operations.
- Loading branch information
Showing
12 changed files
with
554 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
use crate::storage::Storage; | ||
use anyhow::Result; | ||
use async_trait::async_trait; | ||
use noosphere_common::ConditionalSend; | ||
use std::{fs, path::Path}; | ||
|
||
/// [Storage] that can be opened via [Path] reference. | ||
/// [FsBackedStorage] types get a blanket implementation. | ||
#[cfg_attr(not(target_arch = "wasm32"), async_trait)] | ||
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))] | ||
pub trait StorageOpen: Storage + Sized { | ||
async fn open<P: AsRef<Path> + ConditionalSend>(path: P) -> Result<Self>; | ||
} | ||
|
||
#[cfg_attr(not(target_arch = "wasm32"), async_trait)] | ||
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))] | ||
impl<T> StorageOpen for T | ||
where | ||
T: FsBackedStorage, | ||
{ | ||
async fn open<P: AsRef<Path> + ConditionalSend>(path: P) -> Result<Self> { | ||
FsBackedStorage::open(path).await | ||
} | ||
} | ||
|
||
/// [Storage] that can be deleted via [Path] reference. | ||
/// [FsBackedStorage] types get a blanket implementation. | ||
#[cfg_attr(not(target_arch = "wasm32"), async_trait)] | ||
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))] | ||
pub trait StorageRemove: Storage + Sized { | ||
async fn remove<P: AsRef<Path> + ConditionalSend>(path: P) -> Result<()>; | ||
} | ||
|
||
#[cfg_attr(not(target_arch = "wasm32"), async_trait)] | ||
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))] | ||
impl<T> StorageRemove for T | ||
where | ||
T: FsBackedStorage, | ||
{ | ||
async fn remove<P: AsRef<Path> + ConditionalSend>(path: P) -> Result<()> { | ||
<T as FsBackedStorage>::remove(path).await | ||
} | ||
} | ||
|
||
/// [Storage] that can be moved/renamed via [Path] reference. | ||
/// [FsBackedStorage] types get a blanket implementation. | ||
#[cfg_attr(not(target_arch = "wasm32"), async_trait)] | ||
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))] | ||
pub trait StorageRename: Storage + Sized { | ||
async fn rename<P: AsRef<Path> + ConditionalSend, Q: AsRef<Path> + ConditionalSend>( | ||
from: P, | ||
to: Q, | ||
) -> Result<()>; | ||
} | ||
|
||
#[cfg_attr(not(target_arch = "wasm32"), async_trait)] | ||
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))] | ||
impl<T> StorageRename for T | ||
where | ||
T: FsBackedStorage, | ||
{ | ||
async fn rename<P: AsRef<Path> + ConditionalSend, Q: AsRef<Path> + ConditionalSend>( | ||
from: P, | ||
to: Q, | ||
) -> Result<()> { | ||
<T as FsBackedStorage>::rename(from, to).await | ||
} | ||
} | ||
|
||
/// [Storage] that is based on a file system. Implementing [FsBackedStorage] | ||
/// provides blanket implementations for other trait-based [Storage] operations. | ||
#[cfg_attr(not(target_arch = "wasm32"), async_trait)] | ||
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))] | ||
pub trait FsBackedStorage: Storage + Sized { | ||
/// Opens the storage at `path`. | ||
async fn open<P: AsRef<Path> + ConditionalSend>(path: P) -> Result<Self>; | ||
|
||
/// Deletes the storage located at `path` directory. Returns `Ok(())` if | ||
/// the directory is successfully removed, or if it already does not exist. | ||
async fn remove<P: AsRef<Path> + ConditionalSend>(path: P) -> Result<()> { | ||
match fs::metadata(path.as_ref()) { | ||
Ok(_) => fs::remove_dir_all(path.as_ref()).map_err(|e| e.into()), | ||
Err(_) => Ok(()), | ||
} | ||
} | ||
|
||
/// Moves the storage located at `from` to the `to` location. | ||
async fn rename<P: AsRef<Path> + ConditionalSend, Q: AsRef<Path> + ConditionalSend>( | ||
from: P, | ||
to: Q, | ||
) -> Result<()> { | ||
fs::rename(from, to).map_err(|e| e.into()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.