From d29a8fbb6740b03b0b584c1dc52bf94297a256ec Mon Sep 17 00:00:00 2001 From: Lee Smet Date: Fri, 16 May 2025 11:24:27 +0200 Subject: [PATCH] Rename Store to Vault and move to lib root Signed-off-by: Lee Smet --- vault/src/kvs.rs | 47 ---------------------------------------------- vault/src/lib.rs | 49 +++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 48 insertions(+), 48 deletions(-) delete mode 100644 vault/src/kvs.rs diff --git a/vault/src/kvs.rs b/vault/src/kvs.rs deleted file mode 100644 index 2052afa..0000000 --- a/vault/src/kvs.rs +++ /dev/null @@ -1,47 +0,0 @@ -#[cfg(not(target_arch = "wasm32"))] -use std::path::{Path, PathBuf}; - -use crate::{error::Error, key::symmetric::SymmetricKey, keyspace::KeySpace}; - -/// Store is a 2 tiered key-value store. That is, it is a collection of [`spaces`](KeySpace), where -/// each [`space`](KeySpace) is itself an encrypted key-value store -pub struct Store { - #[cfg(not(target_arch = "wasm32"))] - path: PathBuf, -} - -#[cfg(not(target_arch = "wasm32"))] -impl Store { - /// Create a new store at the given path, creating the path if it does not exist yet. - pub async fn new(path: &Path) -> Result { - if path.exists() { - if !path.is_dir() { - return Err(Error::IOError(std::io::Error::new( - std::io::ErrorKind::InvalidInput, - "expected directory", - ))); - } - } else { - std::fs::create_dir_all(path)?; - } - Ok(Self { - path: path.to_path_buf(), - }) - } -} - -impl Store { - /// Open a keyspace with the given name - pub async fn open_keyspace(&self, name: &str, password: &str) -> Result { - let encryption_key = SymmetricKey::derive_from_password(password); - #[cfg(not(target_arch = "wasm32"))] - { - let path = self.path.join(name); - KeySpace::open(&path, encryption_key).await - } - #[cfg(target_arch = "wasm32")] - { - KeySpace::open(name, encryption_key).await - } - } -} diff --git a/vault/src/lib.rs b/vault/src/lib.rs index b0e8ef9..1f9e834 100644 --- a/vault/src/lib.rs +++ b/vault/src/lib.rs @@ -1,4 +1,51 @@ pub mod error; pub mod key; pub mod keyspace; -pub mod kvs; + +#[cfg(not(target_arch = "wasm32"))] +use std::path::{Path, PathBuf}; + +use crate::{error::Error, key::symmetric::SymmetricKey, keyspace::KeySpace}; + +/// Vault is a 2 tiered key-value store. That is, it is a collection of [`spaces`](KeySpace), where +/// each [`space`](KeySpace) is itself an encrypted key-value store +pub struct Vault { + #[cfg(not(target_arch = "wasm32"))] + path: PathBuf, +} + +#[cfg(not(target_arch = "wasm32"))] +impl Vault { + /// Create a new store at the given path, creating the path if it does not exist yet. + pub async fn new(path: &Path) -> Result { + if path.exists() { + if !path.is_dir() { + return Err(Error::IOError(std::io::Error::new( + std::io::ErrorKind::InvalidInput, + "expected directory", + ))); + } + } else { + std::fs::create_dir_all(path)?; + } + Ok(Self { + path: path.to_path_buf(), + }) + } +} + +impl Vault { + /// Open a keyspace with the given name + pub async fn open_keyspace(&self, name: &str, password: &str) -> Result { + let encryption_key = SymmetricKey::derive_from_password(password); + #[cfg(not(target_arch = "wasm32"))] + { + let path = self.path.join(name); + KeySpace::open(&path, encryption_key).await + } + #[cfg(target_arch = "wasm32")] + { + KeySpace::open(name, encryption_key).await + } + } +}