peermerge 0.0.3

Manage JSON-like documents with multiple writers, without a central authority, using a P2P protocol
Documentation
use futures::channel::mpsc::UnboundedSender;
use std::collections::HashMap;
#[cfg(not(target_arch = "wasm32"))]
use std::path::PathBuf;

use crate::{
    common::constants::{DEFAULT_MAX_ENTRY_DATA_SIZE_BYTES, DEFAULT_MAX_WRITE_FEED_LENGTH},
    DocumentId, NameDescription, StateEvent,
};

/// In-memory Peermerge options
#[derive(Builder, Debug)]
pub struct PeermergeMemoryOptions {
    /// Default peer name/description
    pub default_peer_header: NameDescription,
    /// State event sender, can also be given with
    /// [set_state_event_sender()][crate::Peermerge::set_state_event_sender].
    #[builder(setter(into, strip_option), default)]
    pub state_event_sender: Option<UnboundedSender<StateEvent>>,
    /// Reattach secrets used to prevent a new peer being when recreating an in-memory peer.
    /// Value should be stored and updated from [crate::Peermerge::reattach_secret] whenever
    /// a new [crate::StateEventContent::PeerChanged] is received.
    #[builder(setter(into, strip_option), default)]
    pub reattach_secrets: Option<HashMap<DocumentId, String>>,
    /// Maximum size of a data entry. Defaults to [DEFAULT_MAX_ENTRY_DATA_SIZE_BYTES].
    #[builder(default = "DEFAULT_MAX_ENTRY_DATA_SIZE_BYTES")]
    pub max_entry_data_size_bytes: usize,
    /// Maximum length of the write feed data entry. Defaults to [DEFAULT_MAX_WRITE_FEED_LENGTH].
    #[builder(default = "DEFAULT_MAX_WRITE_FEED_LENGTH")]
    pub max_write_feed_length: u64,
}

/// Disk Peermerge options
#[cfg(not(target_arch = "wasm32"))]
#[derive(Builder, Debug)]
pub struct PeermergeDiskOptions {
    /// Root directory for all peermerge data. Sub-directories will
    /// be created under this for each document.
    pub data_root_dir: PathBuf,
    /// Default peer name/description
    pub default_peer_header: NameDescription,
    /// State event sender, can also be given with
    /// [set_state_event_sender()][crate::Peermerge::set_state_event_sender].
    #[builder(setter(into, strip_option), default)]
    pub state_event_sender: Option<UnboundedSender<StateEvent>>,
    /// Maximum size of a data entry. Defaults to [DEFAULT_MAX_ENTRY_DATA_SIZE_BYTES].
    #[builder(default = "DEFAULT_MAX_ENTRY_DATA_SIZE_BYTES")]
    pub max_entry_data_size_bytes: usize,
    /// Maximum length of the write feed data entry. Defaults to [DEFAULT_MAX_WRITE_FEED_LENGTH].
    #[builder(default = "DEFAULT_MAX_WRITE_FEED_LENGTH")]
    pub max_write_feed_length: u64,
}

/// Options for opening an existing peermerge
#[cfg(not(target_arch = "wasm32"))]
#[derive(Builder, Debug)]
pub struct OpenDiskOptions {
    /// Root directory where to look for an existing peermerge
    pub data_root_dir: PathBuf,
    /// Document secrets needed to open parent documents. Use
    /// [document_infos_disk()](crate::Peermerge::document_infos_disk)
    /// to find out documents that need a document secret here.
    #[builder(setter(into, strip_option), default)]
    pub document_secrets: Option<HashMap<DocumentId, String>>,
    /// State event sender, can also be given with
    /// [set_state_event_sender()][crate::Peermerge::set_state_event_sender].
    #[builder(setter(into, strip_option), default)]
    pub state_event_sender: Option<UnboundedSender<StateEvent>>,
}

/// Options for creating new in-memory document.
#[derive(Builder, Debug)]
pub struct CreateNewDocumentMemoryOptions {
    /// Mandatory type of document
    pub document_type: String,
    /// Optional document name/description
    #[builder(setter(into, strip_option), default)]
    pub document_header: Option<NameDescription>,
    /// Parent ID of the document. If set, creates a child
    /// document.
    #[builder(setter(into, strip_option), default)]
    pub parent_id: Option<DocumentId>,
    /// Name and description of the parent as set for this
    /// document. If None, sets the same values as the parent
    /// has currently.
    #[builder(setter(into, strip_option), default)]
    pub parent_header: Option<NameDescription>,
    /// If document is stored encrypted on all peers' feeds, defaults
    /// to true. NB: Only if this is true, is it impossible for a proxy peer to
    /// read the content of the document. Set this to false only if
    /// you really know what you are doing.
    #[builder(default = "true")]
    pub encrypted: bool,
}

/// Options for creating new disk document.
#[cfg(not(target_arch = "wasm32"))]
#[derive(Builder, Debug)]
pub struct CreateNewDocumentDiskOptions {
    /// Mandatory type of document
    pub document_type: String,
    /// Optional document name/description
    #[builder(setter(into, strip_option), default)]
    pub document_header: Option<NameDescription>,
    /// Parent ID of the document. If set, creates a child
    /// document.
    #[builder(setter(into, strip_option), default)]
    pub parent_id: Option<DocumentId>,
    /// Name and description of the parent as set for this
    /// document. If None, sets the same values as the parent
    /// has currently.
    #[builder(setter(into, strip_option), default)]
    pub parent_header: Option<NameDescription>,
    /// If document is stored encrypted on all peers' feeds, defaults
    /// to true. NB: Only if this is true, is it impossible for a proxy peer to
    /// read the content of the document. Set this to false only if
    /// you really know what you are doing.
    #[builder(default = "true")]
    pub encrypted: bool,
}

/// Options for attaching an existing document to an in-memory Peermerge
#[derive(Builder, Debug)]
pub struct AttachDocumentMemoryOptions {
    /// URL of the document. See [sharing_info()](crate::Peermerge::sharing_info).
    pub document_url: String,
    /// Document secret, if needed.
    #[builder(setter(into, strip_option), default)]
    pub document_secret: Option<String>,
    /// Parent ID of the document. Needs to be set if attaching
    /// a child document.
    #[builder(setter(into, strip_option), default)]
    pub parent_id: Option<DocumentId>,
    /// Name and description of the parent as set for this
    /// document. If None, sets the same values as the parent
    /// has currently.
    #[builder(setter(into, strip_option), default)]
    pub parent_header: Option<NameDescription>,
}

/// Options for attaching an existing document to a disk Peermerge
#[cfg(not(target_arch = "wasm32"))]
#[derive(Builder, Debug)]
pub struct AttachDocumentDiskOptions {
    /// URL of the document. See [sharing_info()](crate::Peermerge::sharing_info).
    pub document_url: String,
    /// Document secret, if needed.
    #[builder(setter(into, strip_option), default)]
    pub document_secret: Option<String>,
    /// Parent ID of the document. Needs to be set if attaching
    /// a child document.
    #[builder(setter(into, strip_option), default)]
    pub parent_id: Option<DocumentId>,
    /// Name and description of the parent as set for this
    /// document. If None, sets the same values as the parent
    /// has currently.
    #[builder(setter(into, strip_option), default)]
    pub parent_header: Option<NameDescription>,
}