peermerge 0.0.3

Manage JSON-like documents with multiple writers, without a central authority, using a P2P protocol
Documentation
use crate::{FeedDiscoveryKey, PeerId};

use super::state::{ChildDocumentInfo, DocumentFeedInfo};

/// An BroadcastMessage transmits info of all of the feeds the peer knows
/// to the other peer.
#[derive(Debug)]
pub(crate) struct BroadcastMessage {
    pub(crate) write_feed: Option<DocumentFeedInfo>,
    pub(crate) active_feeds: Vec<DocumentFeedInfo>,
    pub(crate) inactive_feeds: Option<Vec<DocumentFeedInfo>>,
    pub(crate) child_documents: Vec<ChildDocumentInfo>,
}
impl BroadcastMessage {
    pub(crate) fn new(
        write_feed: Option<DocumentFeedInfo>,
        active_feeds: Vec<DocumentFeedInfo>,
        inactive_feeds: Option<Vec<DocumentFeedInfo>>,
        child_documents: Vec<ChildDocumentInfo>,
    ) -> Self {
        Self {
            write_feed,
            active_feeds,
            inactive_feeds,
            child_documents,
        }
    }
}

/// An FeedsChangedMessage is an internal message that contains all of the
/// ids and public keys of created and replaced peer hypercores.
#[derive(Debug)]
pub(crate) struct FeedsChangedMessage {
    pub(crate) doc_discovery_key: FeedDiscoveryKey,
    pub(crate) replaced_feeds: Vec<DocumentFeedInfo>,
    pub(crate) feeds_to_create: Vec<DocumentFeedInfo>,
}
impl FeedsChangedMessage {
    pub(crate) fn new(
        doc_discovery_key: FeedDiscoveryKey,
        replaced_feeds: Vec<DocumentFeedInfo>,
        feeds_to_create: Vec<DocumentFeedInfo>,
    ) -> Self {
        Self {
            doc_discovery_key,
            replaced_feeds,
            feeds_to_create,
        }
    }
}

/// A FeedVerificationMessage is an internal message that contains
/// the verification status of a feed.
#[derive(Debug)]
pub(crate) struct FeedVerificationMessage {
    pub(crate) doc_discovery_key: FeedDiscoveryKey,
    pub(crate) feed_discovery_key: FeedDiscoveryKey,
    pub(crate) verified: bool,
    pub(crate) peer_id: Option<PeerId>,
}

/// An FeedSyncedMessage is an internal message that contains new length
/// of a hypercore for inter-protocol signaling, and also the pending
/// child documents that could not yet be created.
#[derive(Debug)]
pub(crate) struct FeedSyncedMessage {
    pub(crate) contiguous_length: u64,
    pub(crate) not_created_child_documents: Vec<ChildDocumentInfo>,
}
impl FeedSyncedMessage {
    pub(crate) fn new(
        contiguous_length: u64,
        not_created_child_documents: Vec<ChildDocumentInfo>,
    ) -> Self {
        Self {
            contiguous_length,
            not_created_child_documents,
        }
    }
}