feat: Refactor kvstore and vault to use features and logging
- Remove hardcoded dependencies in kvstore Cargo.toml; use features instead. This allows for more flexible compilation for different targets (native vs. WASM). - Improve logging in vault crate using the `log` crate. This makes debugging easier and provides more informative output during execution. Native tests use `env_logger`, WASM tests use `console_log`. - Update README to reflect new logging best practices. - Add cfg attributes to native and wasm modules to improve clarity. - Update traits.rs to specify Send + Sync behavior expectations.
This commit is contained in:
		@@ -2,6 +2,7 @@
 | 
			
		||||
//!
 | 
			
		||||
//! # Runtime Requirement
 | 
			
		||||
//!
 | 
			
		||||
#![cfg(not(target_arch = "wasm32"))]
 | 
			
		||||
//! **A Tokio runtime must be running to use this backend.**
 | 
			
		||||
//! This library does not start or manage a runtime; it assumes that all async methods are called from within an existing Tokio runtime context (e.g., via `#[tokio::main]` or `tokio::test`).
 | 
			
		||||
//!
 | 
			
		||||
@@ -10,11 +11,18 @@
 | 
			
		||||
//! # Example
 | 
			
		||||
//!
 | 
			
		||||
 | 
			
		||||
use crate::traits::KVStore;
 | 
			
		||||
use crate::error::{KVError, Result};
 | 
			
		||||
//! Native backend for kvstore using sled
 | 
			
		||||
//! Only compiled for non-wasm32 targets
 | 
			
		||||
 | 
			
		||||
#[cfg(not(target_arch = "wasm32"))]
 | 
			
		||||
use crate::traits::KVStore;
 | 
			
		||||
#[cfg(not(target_arch = "wasm32"))]
 | 
			
		||||
use crate::error::{KVError, Result};
 | 
			
		||||
#[cfg(not(target_arch = "wasm32"))]
 | 
			
		||||
use async_trait::async_trait;
 | 
			
		||||
#[cfg(not(target_arch = "wasm32"))]
 | 
			
		||||
use sled::Db;
 | 
			
		||||
#[cfg(not(target_arch = "wasm32"))]
 | 
			
		||||
use std::sync::Arc;
 | 
			
		||||
 | 
			
		||||
#[derive(Clone)]
 | 
			
		||||
 
 | 
			
		||||
@@ -16,6 +16,11 @@ use crate::error::Result;
 | 
			
		||||
/// - contains_key (was exists)
 | 
			
		||||
/// - keys
 | 
			
		||||
/// - clear
 | 
			
		||||
/// Async key-value store interface for both native and WASM backends.
 | 
			
		||||
///
 | 
			
		||||
/// For native (non-wasm32) backends, implementers should be `Send + Sync` to support async usage.
 | 
			
		||||
/// For WASM (wasm32) backends, `Send + Sync` is not required.
 | 
			
		||||
#[async_trait::async_trait]
 | 
			
		||||
pub trait KVStore {
 | 
			
		||||
    async fn get(&self, key: &str) -> Result<Option<Vec<u8>>>;
 | 
			
		||||
    async fn set(&self, key: &str, value: &[u8]) -> Result<()>;
 | 
			
		||||
 
 | 
			
		||||
@@ -13,12 +13,15 @@
 | 
			
		||||
//!
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
//! WASM backend for kvstore using IndexedDB (idb crate)
 | 
			
		||||
//! Only compiled for wasm32 targets
 | 
			
		||||
 | 
			
		||||
#[cfg(target_arch = "wasm32")]
 | 
			
		||||
use crate::traits::KVStore;
 | 
			
		||||
#[cfg(target_arch = "wasm32")]
 | 
			
		||||
use crate::error::{KVError, Result};
 | 
			
		||||
 | 
			
		||||
#[cfg(target_arch = "wasm32")]
 | 
			
		||||
use async_trait::async_trait;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
#[cfg(target_arch = "wasm32")]
 | 
			
		||||
use idb::{Database, TransactionMode, Factory};
 | 
			
		||||
#[cfg(target_arch = "wasm32")]
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user