Skip to main content

pulsify_sqlite/
lib.rs

1//! Durable Pulsify documents in SQLite, without materializing them.
2//!
3//! The database file uses the schema and append semantics of the TypeScript
4//! `SqlitePatchStore`, so either implementation can open a file written by the other.
5mod hub;
6mod relay;
7mod store;
8
9pub use pulsify_protocol::{
10    chunk_rows, relay_frame, AppendResult, DocumentStore, PatchRow, PatchStore, RelayOutput, Sink,
11    VersionVector,
12};
13pub use store::{validate_doc_id, SqliteStore, PACKED_MIGRATION, SCHEMA};
14
15/// One writer and its sessions: each session is pinned to a document, and rows
16/// stored through one session are forwarded to the document's other sessions.
17pub type Hub<S> = pulsify_protocol::Hub<SqliteStore, S>;
18
19use std::fmt;
20
21/// A failure carrying a Pulsify protocol error code, such as `CONFLICT`.
22#[derive(Debug, Clone, PartialEq, Eq)]
23pub struct Error {
24    pub code: &'static str,
25    pub message: String,
26}
27impl Error {
28    pub fn new(code: &'static str, message: impl Into<String>) -> Self {
29        Self {
30            code,
31            message: message.into(),
32        }
33    }
34}
35impl fmt::Display for Error {
36    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
37        write!(f, "{}: {}", self.code, self.message)
38    }
39}
40impl std::error::Error for Error {}
41impl From<pulsify_protocol::Error> for Error {
42    fn from(error: pulsify_protocol::Error) -> Self {
43        Self::new(error.code, error.message)
44    }
45}
46impl From<Error> for pulsify_protocol::Error {
47    fn from(error: Error) -> Self {
48        Self::new(error.code, error.message)
49    }
50}
51impl From<rusqlite::Error> for Error {
52    fn from(error: rusqlite::Error) -> Self {
53        Self::new("CONNECTION_FAILED", format!("SQLite: {error}"))
54    }
55}
56impl From<serde_json::Error> for Error {
57    fn from(error: serde_json::Error) -> Self {
58        Self::new(
59            "CONNECTION_FAILED",
60            format!("Corrupt stored metadata: {error}"),
61        )
62    }
63}
64pub type Result<T> = std::result::Result<T, Error>;