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
25mod dedup;
26pub mod manager;
27pub mod protocols;
28#[doc(hidden)]
29#[cfg(any(test, feature = "test_utils"))]
30pub mod test_utils;
31pub mod traits;
32
33/// Configuration object for instantiating sync sessions.
34#[derive(Clone, Debug)]
35pub struct SessionConfig<T> {
36    pub topic: T,
37    pub remote: VerifyingKey,
38    pub live_mode: bool,
39}
40
41/// Message sent to running sync sessions.
42#[derive(Clone, Debug)]
43pub enum ToSync<M> {
44    Payload(M),
45    Close,
46}
47
48/// Events which are emitted from a manager.
49#[derive(Clone, PartialEq, Debug)]
50pub struct FromSync<E> {
51    pub session_id: u64,
52    pub remote: VerifyingKey,
53    pub event: E,
54}
55
56impl<E> FromSync<E> {
57    pub fn session_id(&self) -> u64 {
58        self.session_id
59    }
60
61    pub fn event(&self) -> &E {
62        &self.event
63    }
64
65    pub fn remote(&self) -> &VerifyingKey {
66        &self.remote
67    }
68}