mirror of
https://github.com/logos-messaging/libchat.git
synced 2026-02-10 08:53:08 +00:00
chore: reuse storage config
This commit is contained in:
parent
b2a1ca647b
commit
8e1403bd14
@ -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()? {
|
||||
|
||||
@ -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.
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user