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, PatchRow, PatchStore, RelayOutput, Sink, VersionVector,
11};
12pub use store::{validate_doc_id, SqliteStore, PACKED_MIGRATION, SCHEMA};
13
14/// One writer and its sessions: each session is pinned to a document, and rows
15/// stored through one session are forwarded to the document's other sessions.
16pub type Hub<S> = pulsify_protocol::Hub<SqliteStore, S>;
17
18use std::fmt;
19
20/// A failure carrying a Pulsify protocol error code, such as `CONFLICT`.
21#[derive(Debug, Clone, PartialEq, Eq)]
22pub struct Error {
23    pub code: &'static str,
24    pub message: String,
25}
26impl Error {
27    pub fn new(code: &'static str, message: impl Into<String>) -> Self {
28        Self {
29            code,
30            message: message.into(),
31        }
32    }
33}
34impl fmt::Display for Error {
35    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
36        write!(f, "{}: {}", self.code, self.message)
37    }
38}
39impl std::error::Error for Error {}
40impl From<pulsify_protocol::Error> for Error {
41    fn from(error: pulsify_protocol::Error) -> Self {
42        Self::new(error.code, error.message)
43    }
44}
45impl From<Error> for pulsify_protocol::Error {
46    fn from(error: Error) -> Self {
47        Self::new(error.code, error.message)
48    }
49}
50impl From<rusqlite::Error> for Error {
51    fn from(error: rusqlite::Error) -> Self {
52        Self::new("CONNECTION_FAILED", format!("SQLite: {error}"))
53    }
54}
55impl From<serde_json::Error> for Error {
56    fn from(error: serde_json::Error) -> Self {
57        Self::new(
58            "CONNECTION_FAILED",
59            format!("Corrupt stored metadata: {error}"),
60        )
61    }
62}
63pub type Result<T> = std::result::Result<T, Error>;