Skip to main content

p2panda_sync/
lib.rs

1// SPDX-License-Identifier: MIT OR Apache-2.0
2
3#![cfg_attr(docsrs, feature(doc_cfg))]
4
5//! Data-type agnostic interfaces for implementing sync protocols and managers which can be used
6//! stand-alone or as part of the local-first stack provided by
7//! [`p2panda-net`].
8//!
9//! Users can implement two-party sync protocols over a `Sink` / `Stream` pair with the `Protocol`
10//! trait and a system for instantiating and orchestrating concurrent sync sessions with the
11//! `Manager` trait.
12//!
13//! Concrete implementations for performing sync over p2panda append-only logs associated with a
14//! generic topic can be found in the `manager` and `protocols` modules.
15//!
16//! For most high-level users [`p2panda-net`]
17//! will be the entry point into local-first development with p2panda. Interfaces in this crate
18//! are intended for cases where users want to integrate their own base convergent data-type and
19//! sync protocols as a module in the
20//! [`p2panda-net`] stack.
21//!
22//! [`p2panda-net`]: https://docs.rs/p2panda-net/latest/p2panda_net/
23use p2panda_core::VerifyingKey;
24
25pub mod api;
26pub mod dedup;
27pub mod manager;
28pub mod protocols;
29#[doc(hidden)]
30#[cfg(any(test, feature = "test_utils"))]
31pub mod test_utils;
32pub mod traits;
33
34/// Configuration object for instantiating sync sessions.
35#[derive(Clone, Debug)]
36pub struct SessionConfig<T> {
37    pub topic: T,
38    pub remote: VerifyingKey,
39    pub live_mode: bool,
40}
41
42/// Message sent to running sync sessions.
43#[derive(Clone, Debug)]
44pub enum ToSync<M> {
45    Payload(M),
46    Close,
47}
48
49/// Events which are emitted from a manager.
50#[derive(Clone, PartialEq, Debug)]
51pub struct FromSync<E> {
52    pub session_id: u64,
53    pub remote: VerifyingKey,
54    pub event: E,
55}
56
57impl<E> FromSync<E> {
58    pub fn session_id(&self) -> u64 {
59        self.session_id
60    }
61
62    pub fn event(&self) -> &E {
63        &self.event
64    }
65
66    pub fn remote(&self) -> &VerifyingKey {
67        &self.remote
68    }
69}