From 8e1403bd14aaa378662bba8ef50954b99f439088 Mon Sep 17 00:00:00 2001 From: kaichaosun Date: Thu, 5 Feb 2026 14:18:33 +0800 Subject: [PATCH] chore: reuse storage config --- conversations/src/chat.rs | 17 +++-------------- conversations/src/storage/db.rs | 17 +++++------------ 2 files changed, 8 insertions(+), 26 deletions(-) diff --git a/conversations/src/chat.rs b/conversations/src/chat.rs index 8cd7f1b..4292b50 100644 --- a/conversations/src/chat.rs +++ b/conversations/src/chat.rs @@ -14,15 +14,8 @@ use crate::{ types::{AddressedEnvelope, ContentData}, }; -/// Configuration for ChatManager storage. -pub enum StorageConfig { - /// In-memory storage (data lost on restart, useful for testing). - InMemory, - /// Unencrypted file storage (for development). - File(String), - /// Encrypted file storage (for production). - Encrypted { path: String, key: String }, -} +// Re-export StorageConfig from storage crate for convenience +pub use storage::StorageConfig; /// Error type for ChatManager operations. #[derive(Debug, thiserror::Error)] @@ -74,11 +67,7 @@ impl ChatManager { /// If an identity exists in storage, it will be restored. /// Otherwise, a new identity will be created and saved. pub fn open(config: StorageConfig) -> Result { - let mut storage = match config { - StorageConfig::InMemory => ChatStorage::in_memory()?, - StorageConfig::File(path) => ChatStorage::open(&path)?, - StorageConfig::Encrypted { path, key } => ChatStorage::new(&path, &key)?, - }; + let mut storage = ChatStorage::new(config)?; // Load or create identity let identity = if let Some(identity) = storage.load_identity()? { diff --git a/conversations/src/storage/db.rs b/conversations/src/storage/db.rs index 5820fe5..5a2c135 100644 --- a/conversations/src/storage/db.rs +++ b/conversations/src/storage/db.rs @@ -1,6 +1,6 @@ //! Chat-specific storage implementation. -use storage::{RusqliteError, SqliteDb, StorageError, params}; +use storage::{RusqliteError, SqliteDb, StorageConfig, StorageError, params}; use super::types::{ChatRecord, IdentityRecord}; use crate::identity::Identity; @@ -41,22 +41,15 @@ pub struct ChatStorage { } impl ChatStorage { - /// Opens an existing encrypted database file. - pub fn new(path: &str, key: &str) -> Result { - let db = SqliteDb::sqlcipher(path.to_string(), key.to_string())?; + /// Creates a new ChatStorage with the given configuration. + pub fn new(config: StorageConfig) -> Result { + let db = SqliteDb::new(config)?; Self::run_migration(db) } /// Creates an in-memory storage (useful for testing). pub fn in_memory() -> Result { - let db = SqliteDb::in_memory()?; - Self::run_migration(db) - } - - /// Opens an unencrypted database file (for development/testing). - pub fn open(path: &str) -> Result { - let db = SqliteDb::open(path)?; - Self::run_migration(db) + Self::new(StorageConfig::InMemory) } /// Creates a new chat storage with the given database.