p2panda_sync/
lib.rs

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