mirror of
https://github.com/logos-messaging/libchat.git
synced 2026-05-17 23:49:27 +00:00
refactor: use result wrapper for ffi
This commit is contained in:
parent
7019b04ccb
commit
3673d730f3
@ -18,6 +18,7 @@ use storage::StorageConfig;
|
|||||||
use crate::{
|
use crate::{
|
||||||
context::{Context, Introduction},
|
context::{Context, Introduction},
|
||||||
errors::ChatError,
|
errors::ChatError,
|
||||||
|
ffi::utils::CResult,
|
||||||
types::ContentData,
|
types::ContentData,
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -65,15 +66,23 @@ pub fn create_context(name: repr_c::String) -> repr_c::Box<ContextHandle> {
|
|||||||
/// - db_path: Path to the SQLite database file
|
/// - db_path: Path to the SQLite database file
|
||||||
///
|
///
|
||||||
/// # Returns
|
/// # 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]
|
#[ffi_export]
|
||||||
pub fn create_context_with_storage(
|
pub fn create_context_with_storage(
|
||||||
name: repr_c::String,
|
name: repr_c::String,
|
||||||
db_path: repr_c::String,
|
db_path: repr_c::String,
|
||||||
) -> repr_c::Box<ContextHandle> {
|
) -> CResult<repr_c::Box<ContextHandle>, repr_c::String> {
|
||||||
let config = StorageConfig::File(db_path.to_string());
|
let config = StorageConfig::File(db_path.to_string());
|
||||||
let ctx = Context::open(&*name, config).expect("failed to open context with storage");
|
match Context::open(&*name, config) {
|
||||||
Box::new(ContextHandle(ctx)).into()
|
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.
|
/// Returns the friendly name of the contexts installation.
|
||||||
|
|||||||
@ -18,9 +18,6 @@ pub use crate::inbox::Introduction;
|
|||||||
/// Error type for Context operations.
|
/// Error type for Context operations.
|
||||||
#[derive(Debug, thiserror::Error)]
|
#[derive(Debug, thiserror::Error)]
|
||||||
pub enum ContextError {
|
pub enum ContextError {
|
||||||
#[error("chat error: {0}")]
|
|
||||||
Chat(#[from] ChatError),
|
|
||||||
|
|
||||||
#[error("storage error: {0}")]
|
#[error("storage error: {0}")]
|
||||||
Storage(#[from] StorageError),
|
Storage(#[from] StorageError),
|
||||||
}
|
}
|
||||||
|
|||||||
1
conversations/src/ffi/mod.rs
Normal file
1
conversations/src/ffi/mod.rs
Normal file
@ -0,0 +1 @@
|
|||||||
|
pub mod utils;
|
||||||
13
conversations/src/ffi/utils.rs
Normal file
13
conversations/src/ffi/utils.rs
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
use safer_ffi::prelude::*;
|
||||||
|
|
||||||
|
#[derive_ReprC]
|
||||||
|
#[repr(C)]
|
||||||
|
pub struct CResult<T: ReprC, Err: ReprC> {
|
||||||
|
pub ok: Option<T>,
|
||||||
|
pub err: Option<Err>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[ffi_export]
|
||||||
|
pub fn ffi_c_string_free(s: repr_c::String) {
|
||||||
|
drop(s);
|
||||||
|
}
|
||||||
@ -3,6 +3,7 @@ mod context;
|
|||||||
mod conversation;
|
mod conversation;
|
||||||
mod crypto;
|
mod crypto;
|
||||||
mod errors;
|
mod errors;
|
||||||
|
mod ffi;
|
||||||
mod identity;
|
mod identity;
|
||||||
mod inbox;
|
mod inbox;
|
||||||
mod proto;
|
mod proto;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user