From fdacfae108092ae23d6f61b0bf529aa364c188ed Mon Sep 17 00:00:00 2001 From: kaichaosun Date: Fri, 27 Feb 2026 15:31:38 +0800 Subject: [PATCH] refactor: uniform storage error into chat error --- conversations/src/api.rs | 4 +++- conversations/src/context.rs | 11 ++--------- conversations/src/errors.rs | 4 ++++ conversations/src/ffi/utils.rs | 2 +- conversations/src/storage/mod.rs | 1 - 5 files changed, 10 insertions(+), 12 deletions(-) diff --git a/conversations/src/api.rs b/conversations/src/api.rs index 6493980..2fa1625 100644 --- a/conversations/src/api.rs +++ b/conversations/src/api.rs @@ -66,7 +66,9 @@ pub fn create_context(name: repr_c::String) -> repr_c::Box { /// - db_path: Path to the SQLite database file /// /// # Returns -/// CResult with context handle on success, or error string on failure +/// CResult with context handle on success, or error string on failure. +/// On success, the context handle must be freed with `destroy_context()` after usage. +/// On error, the error string must be freed with `destroy_string()` after usage. #[ffi_export] pub fn create_context_with_storage( name: repr_c::String, diff --git a/conversations/src/context.rs b/conversations/src/context.rs index f112053..bec37a5 100644 --- a/conversations/src/context.rs +++ b/conversations/src/context.rs @@ -8,20 +8,13 @@ use crate::{ identity::Identity, inbox::Inbox, proto::{EncryptedPayload, EnvelopeV1, Message}, - storage::{ChatStorage, StorageError}, + storage::ChatStorage, types::{AddressedEnvelope, ContentData}, }; pub use crate::conversation::ConversationIdOwned; pub use crate::inbox::Introduction; -/// Error type for Context operations. -#[derive(Debug, thiserror::Error)] -pub enum ContextError { - #[error("storage error: {0}")] - Storage(#[from] StorageError), -} - // This is the main entry point to the conversations api. // Ctx manages lifetimes of objects to process and generate payloads. pub struct Context { @@ -37,7 +30,7 @@ impl Context { /// /// If an identity exists in storage, it will be restored. /// Otherwise, a new identity will be created with the given name and saved. - pub fn open(name: impl Into, config: StorageConfig) -> Result { + pub fn open(name: impl Into, config: StorageConfig) -> Result { let mut storage = ChatStorage::new(config)?; let name = name.into(); diff --git a/conversations/src/errors.rs b/conversations/src/errors.rs index d551960..f47004c 100644 --- a/conversations/src/errors.rs +++ b/conversations/src/errors.rs @@ -1,5 +1,7 @@ pub use thiserror::Error; +use storage::StorageError; + #[derive(Error, Debug)] pub enum ChatError { #[error("protocol error: {0:?}")] @@ -20,6 +22,8 @@ pub enum ChatError { BadParsing(&'static str), #[error("convo with id: {0} was not found")] NoConvo(String), + #[error("storage error: {0}")] + Storage(#[from] StorageError), } #[derive(Error, Debug)] diff --git a/conversations/src/ffi/utils.rs b/conversations/src/ffi/utils.rs index f50033f..0df0b3b 100644 --- a/conversations/src/ffi/utils.rs +++ b/conversations/src/ffi/utils.rs @@ -8,6 +8,6 @@ pub struct CResult { } #[ffi_export] -pub fn ffi_c_string_free(s: repr_c::String) { +pub fn destroy_string(s: repr_c::String) { drop(s); } diff --git a/conversations/src/storage/mod.rs b/conversations/src/storage/mod.rs index 7a32751..9364aeb 100644 --- a/conversations/src/storage/mod.rs +++ b/conversations/src/storage/mod.rs @@ -5,4 +5,3 @@ mod migrations; pub(crate) mod types; pub(crate) use db::ChatStorage; -pub(crate) use storage::StorageError;