tlpt 0.8.0

A set of protocols for building local-first distributed systems
Documentation
use futures::{future, Stream, StreamExt, TryStreamExt, stream};
use jmbl::{
    ops::OpWithTarget,
    stream_format::{OpDeserializer, StreamFormatReadError},
};
use litl::ReadNewlnSepStreamError;
use thiserror::Error;
use tracing::debug;

use super::super::ContentDiff;

fn vals_to_ops<S: Stream<Item = litl::Val>>(
    values: S,
) -> impl Stream<Item = OpWithTarget> {
    values.scan(OpDeserializer::new(), |deserializer, value| {
        future::ready(
            match deserializer.deserialize(value)
            {
                Ok(op) => Some(op),
                Err(err) => {
                    tracing::error!(?err, "Error reading ops from log");
                    None
                }
            },
        )
    })
}

pub fn diffs_to_ops(diffs: impl Stream<Item = ContentDiff>) -> impl Stream<Item = OpWithTarget> {
    // TODO: add decompression
    vals_to_ops(
        diffs
            // .inspect(|diff| {
            //     let id = diff.raw_diff.expect_log().id;
            //     let after = diff.raw_diff.expect_log().after;
            //     let len = diff.raw_diff.expect_log().new_entries.len();
            //     debug!(?id, after, len, "Decrypted diff {}", litl::to_string(&diff.decrypted_entries).unwrap())
            // })
            .flat_map(|diff| stream::iter(diff.decrypted_entries))
    )
}

#[derive(Error, Debug)]
pub enum LogStreamError {
    #[error(transparent)]
    StreamFormatReadError(#[from] StreamFormatReadError),
    #[error(transparent)]
    ReadError(#[from] ReadNewlnSepStreamError),
}