Rename Store to Vault and move to lib root
Signed-off-by: Lee Smet <lee.smet@hotmail.com>
This commit is contained in:
		@@ -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<Self, Error> {
 | 
			
		||||
        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<KeySpace, Error> {
 | 
			
		||||
        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
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -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<Self, Error> {
 | 
			
		||||
        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<KeySpace, Error> {
 | 
			
		||||
        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
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user