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 40
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 ImportStorage/ExportStorage tr…
…aits, and other trait-based operations.
- Loading branch information
Showing
12 changed files
with
596 additions
and
130 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 OpenStorage: 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> OpenStorage 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 RemoveStorage: 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> RemoveStorage 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 RenameStorage: 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> RenameStorage 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 was deleted.
Oops, something went wrong.
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.