collab-common 0.0.7

Code shared by collab's client and server
Documentation
use serde::{Deserialize, Serialize};

use crate::encode::ShouldCompress;
use crate::PeerId;

/// TODO: docs
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct Insertion {
    crdt: cola::Insertion,
    text: Box<str>,
}

impl Insertion {
    /// TODO: docs
    #[inline(always)]
    pub fn crdt(&self) -> &cola::Insertion {
        &self.crdt
    }

    /// TODO: docs
    #[inline(always)]
    pub fn inserted_by(&self) -> PeerId {
        PeerId::new(self.crdt().text().inserted_by())
    }

    /// TODO: docs
    #[inline(always)]
    pub fn new(crdt: cola::Insertion, text: impl Into<Box<str>>) -> Self {
        Self { crdt, text: text.into() }
    }

    /// TODO: docs
    #[inline(always)]
    pub fn text(&self) -> &str {
        &self.text
    }
}

impl ShouldCompress for Insertion {
    #[inline]
    fn should_compress(&self) -> bool {
        self.text.len() > 100
    }
}