Skip to main content

libsimplex_rs/
lib.rs

1//! Safe, typed Rust interface to the official SimpleX Chat library.
2//!
3//! The native ABI and textual SimpleX command language are implementation
4//! details of this crate. Consumers interact only with Rust data types,
5//! commands and events.
6
7mod client;
8mod ffi;
9mod model;
10
11use std::{path::Path, path::PathBuf, sync::Arc, sync::mpsc};
12
13pub use client::{ChatDeleteMode, ChatFeature, SimplexCommand as Command};
14pub use ffi::SimplexError as Error;
15pub use model::{
16    Attachment, AttachmentKind, ChatDeletionSettings, ChatFeatures, ChatRef, ChatSummary, Message,
17    MessageReaction, Profile, ServerEntry, ServerProtocol, SimplexEvent as Event, User,
18};
19
20/// Loaded official SimpleX Chat runtime.
21pub struct Client {
22    api: Arc<ffi::SimplexApi>,
23}
24
25/// Filesystem locations selected by the embedding application.
26#[derive(Clone, Debug, Eq, PartialEq)]
27pub struct Config {
28    pub data_directory: PathBuf,
29    pub download_directory: PathBuf,
30}
31
32impl Config {
33    pub fn new(data_directory: impl Into<PathBuf>, download_directory: impl Into<PathBuf>) -> Self {
34        Self {
35            data_directory: data_directory.into(),
36            download_directory: download_directory.into(),
37        }
38    }
39}
40
41impl Client {
42    /// Load an official, ABI-compatible SimpleX Chat native library.
43    pub fn load(path: impl AsRef<Path>) -> Result<Self, Error> {
44        // SAFETY: symbol lookup and ownership are validated and encapsulated by
45        // `SimplexApi`; callers only receive the safe typed wrapper.
46        let api = unsafe { ffi::SimplexApi::load(path) }?;
47        Ok(Self { api })
48    }
49
50    /// Start the serialized SimpleX controller and its typed command/event bridge.
51    pub fn start(self, config: Config) -> Session {
52        let (event_sender, events) = mpsc::channel();
53        let commands = client::spawn(self.api, config, event_sender);
54        Session { commands, events }
55    }
56}
57
58/// Typed command and event endpoints for one SimpleX client session.
59pub struct Session {
60    commands: mpsc::Sender<Command>,
61    events: mpsc::Receiver<Event>,
62}
63
64impl Session {
65    pub fn into_parts(self) -> (mpsc::Sender<Command>, mpsc::Receiver<Event>) {
66        (self.commands, self.events)
67    }
68}