chore: reuse storage config

This commit is contained in:
kaichaosun 2026-02-05 14:18:33 +08:00
parent b2a1ca647b
commit 8e1403bd14
No known key found for this signature in database
GPG Key ID: 223E0F992F4F03BF
2 changed files with 8 additions and 26 deletions

View File

@ -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<Self, ChatManagerError> {
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()? {

View File

@ -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<Self, StorageError> {
let db = SqliteDb::sqlcipher(path.to_string(), key.to_string())?;
/// Creates a new ChatStorage with the given configuration.
pub fn new(config: StorageConfig) -> Result<Self, StorageError> {
let db = SqliteDb::new(config)?;
Self::run_migration(db)
}
/// Creates an in-memory storage (useful for testing).
pub fn in_memory() -> Result<Self, StorageError> {
let db = SqliteDb::in_memory()?;
Self::run_migration(db)
}
/// Opens an unencrypted database file (for development/testing).
pub fn open(path: &str) -> Result<Self, StorageError> {
let db = SqliteDb::open(path)?;
Self::run_migration(db)
Self::new(StorageConfig::InMemory)
}
/// Creates a new chat storage with the given database.