diff --git a/conversations/src/api.rs b/conversations/src/api.rs index 8ba81b9..6493980 100644 --- a/conversations/src/api.rs +++ b/conversations/src/api.rs @@ -18,6 +18,7 @@ use storage::StorageConfig; use crate::{ context::{Context, Introduction}, errors::ChatError, + ffi::utils::CResult, types::ContentData, }; @@ -65,15 +66,23 @@ pub fn create_context(name: repr_c::String) -> repr_c::Box { /// - db_path: Path to the SQLite database file /// /// # Returns -/// Opaque handle to the context. Must be freed with destroy_context() +/// CResult with context handle on success, or error string on failure #[ffi_export] pub fn create_context_with_storage( name: repr_c::String, db_path: repr_c::String, -) -> repr_c::Box { +) -> CResult, repr_c::String> { let config = StorageConfig::File(db_path.to_string()); - let ctx = Context::open(&*name, config).expect("failed to open context with storage"); - Box::new(ContextHandle(ctx)).into() + match Context::open(&*name, config) { + Ok(ctx) => CResult { + ok: Some(Box::new(ContextHandle(ctx)).into()), + err: None, + }, + Err(e) => CResult { + ok: None, + err: Some(e.to_string().into()), + }, + } } /// Returns the friendly name of the contexts installation. diff --git a/conversations/src/context.rs b/conversations/src/context.rs index fe410d0..f112053 100644 --- a/conversations/src/context.rs +++ b/conversations/src/context.rs @@ -18,9 +18,6 @@ pub use crate::inbox::Introduction; /// Error type for Context operations. #[derive(Debug, thiserror::Error)] pub enum ContextError { - #[error("chat error: {0}")] - Chat(#[from] ChatError), - #[error("storage error: {0}")] Storage(#[from] StorageError), } diff --git a/conversations/src/ffi/mod.rs b/conversations/src/ffi/mod.rs new file mode 100644 index 0000000..b5614dd --- /dev/null +++ b/conversations/src/ffi/mod.rs @@ -0,0 +1 @@ +pub mod utils; diff --git a/conversations/src/ffi/utils.rs b/conversations/src/ffi/utils.rs new file mode 100644 index 0000000..f50033f --- /dev/null +++ b/conversations/src/ffi/utils.rs @@ -0,0 +1,13 @@ +use safer_ffi::prelude::*; + +#[derive_ReprC] +#[repr(C)] +pub struct CResult { + pub ok: Option, + pub err: Option, +} + +#[ffi_export] +pub fn ffi_c_string_free(s: repr_c::String) { + drop(s); +} diff --git a/conversations/src/lib.rs b/conversations/src/lib.rs index e13c3fa..5490629 100644 --- a/conversations/src/lib.rs +++ b/conversations/src/lib.rs @@ -3,6 +3,7 @@ mod context; mod conversation; mod crypto; mod errors; +mod ffi; mod identity; mod inbox; mod proto;