chore: remove feature gates

This commit is contained in:
kaichaosun 2026-01-28 16:56:35 +08:00
parent 28a965dff4
commit 810dcc28e9
No known key found for this signature in database
GPG Key ID: 223E0F992F4F03BF
7 changed files with 34 additions and 51 deletions

View File

@ -20,10 +20,9 @@ thiserror = "2"
blake2 = "0.10.6" blake2 = "0.10.6"
safer-ffi = "0.1.13" safer-ffi = "0.1.13"
zeroize = "1.8.2" zeroize = "1.8.2"
storage = { workspace = true, optional = true, features = ["sqlite"] } storage = { workspace = true, optional = true }
[features] [features]
default = [] default = []
persist = ["storage"] persist = ["storage"]
sqlcipher = ["persist", "storage/sqlcipher"]
headers = ["safer-ffi/headers"] headers = ["safer-ffi/headers"]

View File

@ -1,21 +1,23 @@
//! Demonstrates out-of-order message handling with skipped keys persistence. //! Demonstrates out-of-order message handling with skipped keys persistence.
//! //!
//! Run with: cargo run --example out_of_order_demo --features storage //! Run with: cargo run --example out_of_order_demo --features persist
#[cfg(feature = "storage")] #[cfg(feature = "persist")]
use double_ratchets::{ use double_ratchets::{
InstallationKeyPair, RatchetState, RatchetStorage, StorageConfig, hkdf::DefaultDomain, InstallationKeyPair, RatchetState, RatchetStorage, StorageConfig, hkdf::DefaultDomain,
state::Header, state::Header,
}; };
fn main() { fn main() {
println!("=== Out-of-Order Message Handling Demo (skipped - enable 'storage' feature) ===\n"); println!("=== Out-of-Order Message Handling Demo ===\n");
#[cfg(feature = "storage")] #[cfg(feature = "persist")]
run_demo(); run_demo();
#[cfg(not(feature = "persist"))]
println!("(skipped - enable 'persist' feature)");
} }
#[cfg(feature = "storage")] #[cfg(feature = "persist")]
fn run_demo() { fn run_demo() {
let mut storage = let mut storage =
RatchetStorage::with_config(StorageConfig::InMemory).expect("Failed to create storage"); RatchetStorage::with_config(StorageConfig::InMemory).expect("Failed to create storage");

View File

@ -1,9 +1,8 @@
//! Demonstrates SQLite storage for Double Ratchet state persistence. //! Demonstrates SQLite storage for Double Ratchet state persistence.
//! //!
//! Run with: cargo run --example storage_demo --features storage //! Run with: cargo run --example storage_demo --features persist
//! For SQLCipher: cargo run --example storage_demo --features sqlcipher
#[cfg(feature = "storage")] #[cfg(feature = "persist")]
use double_ratchets::{ use double_ratchets::{
InstallationKeyPair, RatchetSession, RatchetStorage, StorageConfig, hkdf::PrivateV1Domain, InstallationKeyPair, RatchetSession, RatchetStorage, StorageConfig, hkdf::PrivateV1Domain,
}; };
@ -12,29 +11,28 @@ fn main() {
println!("=== Double Ratchet Storage Demo ===\n"); println!("=== Double Ratchet Storage Demo ===\n");
// Demo 1: In-memory storage (for testing) // Demo 1: In-memory storage (for testing)
println!("--- Demo 1: In-Memory Storage (skipped - enable 'storage' feature) ---"); println!("--- Demo 1: In-Memory Storage ---");
#[cfg(feature = "storage")] #[cfg(feature = "persist")]
demo_in_memory(); demo_in_memory();
#[cfg(not(feature = "persist"))]
println!(" (skipped - enable 'persist' feature)");
// Demo 2: File-based storage (for local development) // Demo 2: File-based storage (for local development)
println!("\n--- Demo 2: File-Based Storage (skipped - enable 'storage' feature) ---"); println!("\n--- Demo 2: File-Based Storage ---");
#[cfg(feature = "storage")] #[cfg(feature = "persist")]
demo_file_storage(); demo_file_storage();
#[cfg(not(feature = "persist"))]
println!(" (skipped - enable 'persist' feature)");
// Demo 3: SQLCipher encrypted storage (for production) // Demo 3: SQLCipher encrypted storage (for production)
#[cfg(feature = "sqlcipher")] println!("\n--- Demo 3: SQLCipher Encrypted Storage ---");
{ #[cfg(feature = "persist")]
println!("\n--- Demo 3: SQLCipher Encrypted Storage ---"); demo_sqlcipher();
demo_sqlcipher(); #[cfg(not(feature = "persist"))]
} println!(" (skipped - enable 'persist' feature)");
#[cfg(not(feature = "sqlcipher"))]
{
println!("\n--- Demo 3: SQLCipher (skipped - enable 'sqlcipher' feature) ---");
}
} }
#[cfg(feature = "storage")] #[cfg(feature = "persist")]
fn demo_in_memory() { fn demo_in_memory() {
let mut alice_storage = let mut alice_storage =
RatchetStorage::with_config(StorageConfig::InMemory).expect("Failed to create storage"); RatchetStorage::with_config(StorageConfig::InMemory).expect("Failed to create storage");
@ -43,7 +41,7 @@ fn demo_in_memory() {
run_conversation(&mut alice_storage, &mut bob_storage); run_conversation(&mut alice_storage, &mut bob_storage);
} }
#[cfg(feature = "storage")] #[cfg(feature = "persist")]
fn demo_file_storage() { fn demo_file_storage() {
ensure_tmp_directory(); ensure_tmp_directory();
@ -78,7 +76,7 @@ fn demo_file_storage() {
let _ = std::fs::remove_file(db_path_bob); let _ = std::fs::remove_file(db_path_bob);
} }
#[cfg(feature = "sqlcipher")] #[cfg(feature = "persist")]
fn demo_sqlcipher() { fn demo_sqlcipher() {
ensure_tmp_directory(); ensure_tmp_directory();
let alice_db_path = "./tmp/double_ratchet_encrypted_alice.db"; let alice_db_path = "./tmp/double_ratchet_encrypted_alice.db";
@ -136,7 +134,7 @@ fn ensure_tmp_directory() {
/// Simulates a conversation between Alice and Bob. /// Simulates a conversation between Alice and Bob.
/// Each party saves/loads state from storage for each operation. /// Each party saves/loads state from storage for each operation.
#[cfg(feature = "storage")] #[cfg(feature = "persist")]
fn run_conversation(alice_storage: &mut RatchetStorage, bob_storage: &mut RatchetStorage) { fn run_conversation(alice_storage: &mut RatchetStorage, bob_storage: &mut RatchetStorage) {
// === Setup: Simulate X3DH key exchange === // === Setup: Simulate X3DH key exchange ===
let shared_secret = [0x42u8; 32]; // In reality, this comes from X3DH let shared_secret = [0x42u8; 32]; // In reality, this comes from X3DH
@ -208,7 +206,7 @@ fn run_conversation(alice_storage: &mut RatchetStorage, bob_storage: &mut Ratche
); );
} }
#[cfg(feature = "storage")] #[cfg(feature = "persist")]
fn continue_after_restart(alice_storage: &mut RatchetStorage, bob_storage: &mut RatchetStorage) { fn continue_after_restart(alice_storage: &mut RatchetStorage, bob_storage: &mut RatchetStorage) {
// Load persisted states // Load persisted states
let conv_id = "conv1"; let conv_id = "conv1";

View File

@ -6,9 +6,4 @@ description = "Shared storage layer for libchat"
[dependencies] [dependencies]
thiserror = "2" thiserror = "2"
rusqlite = { version = "0.35", optional = true, features = ["bundled"] } rusqlite = { version = "0.35", features = ["bundled-sqlcipher-vendored-openssl"] }
[features]
default = []
sqlite = ["rusqlite"]
sqlcipher = ["sqlite", "rusqlite/bundled-sqlcipher-vendored-openssl"]

View File

@ -28,7 +28,6 @@ pub enum StorageError {
Transaction(String), Transaction(String),
} }
#[cfg(feature = "sqlite")]
impl From<rusqlite::Error> for StorageError { impl From<rusqlite::Error> for StorageError {
fn from(e: rusqlite::Error) -> Self { fn from(e: rusqlite::Error) -> Self {
StorageError::Database(e.to_string()) StorageError::Database(e.to_string())

View File

@ -3,24 +3,16 @@
//! This crate provides a common storage abstraction that can be used by //! This crate provides a common storage abstraction that can be used by
//! multiple crates in the libchat workspace (double-ratchets, conversations, etc.). //! multiple crates in the libchat workspace (double-ratchets, conversations, etc.).
//! //!
//! # Features //! Uses SQLCipher for encrypted SQLite storage.
//!
//! - `sqlite`: Enable SQLite-based storage
//! - `sqlcipher`: Enable encrypted SQLite storage via SQLCipher
mod error; mod error;
#[cfg(feature = "sqlite")]
mod sqlite; mod sqlite;
pub use error::StorageError; pub use error::StorageError;
#[cfg(feature = "sqlite")]
pub use sqlite::{SqliteDb, StorageConfig}; pub use sqlite::{SqliteDb, StorageConfig};
// Re-export rusqlite types that domain crates will need // Re-export rusqlite types that domain crates will need
#[cfg(feature = "sqlite")] pub use rusqlite::{Error as RusqliteError, Transaction, params};
pub use rusqlite::{params, Transaction, Error as RusqliteError};
/// Trait for types that can be stored and retrieved. /// Trait for types that can be stored and retrieved.
/// ///

View File

@ -12,8 +12,7 @@ pub enum StorageConfig {
InMemory, InMemory,
/// File-based SQLite database. /// File-based SQLite database.
File(String), File(String),
/// SQLCipher encrypted database (requires `sqlcipher` feature). /// SQLCipher encrypted database.
#[cfg(feature = "sqlcipher")]
Encrypted { Encrypted {
path: String, path: String,
key: String, key: String,
@ -34,7 +33,6 @@ impl SqliteDb {
let conn = match config { let conn = match config {
StorageConfig::InMemory => Connection::open_in_memory()?, StorageConfig::InMemory => Connection::open_in_memory()?,
StorageConfig::File(ref path) => Connection::open(path)?, StorageConfig::File(ref path) => Connection::open(path)?,
#[cfg(feature = "sqlcipher")]
StorageConfig::Encrypted { ref path, ref key } => { StorageConfig::Encrypted { ref path, ref key } => {
let conn = Connection::open(path)?; let conn = Connection::open(path)?;
conn.pragma_update(None, "key", key)?; conn.pragma_update(None, "key", key)?;