minerva 0.2.0

Causal ordering for distributed systems
//! The move-record suite (PRD 0022): the movement flows over the
//! [`Composer`] facade in the parallel-pair recipe (text pair beside move
//! pair), the recension's verdicts, and the convergence laws. The store's
//! axis laws run in the shared conformance harness
//! (`causal_store::foreign::laws::axis::prop_metatheses_conforms_to_the_axis`);
//! this module owns the flows and the replay laws.

extern crate alloc;

mod batch;
mod collation;
mod examples;
mod foundations;
mod properties;

use crate::kairos::{Clock, TickCounter};
use crate::metis::dot::RawDot;
use crate::metis::{Composer, Dot, DotSet, Locus, Metatheses, Metathesis, Rhapsody};

/// Builds an identity literal. Panics on the non-dot counter zero (R-91).
#[track_caller]
fn d(station: u32, counter: u64) -> Dot {
    Dot::from_parts(station, counter).expect("test literal names the non-dot counter zero")
}

/// The text composer a replica holds.
type Text = Composer<Rhapsody>;
/// The move composer a replica holds, in its own causal pair.
type Moves = Composer<Metatheses>;

fn clock(station: u32) -> Clock<TickCounter> {
    Clock::with_default_config(TickCounter::new(), station).unwrap()
}

/// Types one element through the documented visual-insert flow (anchor from
/// the caret, rank minted above the incumbent bucket head), returning its
/// dot.
fn type_after(text: &mut Text, clk: &Clock<TickCounter>, after: Option<RawDot>) -> Dot {
    let anchor = text.state().store().anchor_for_visual_insert(after);
    if let Some(top) = text.state().store().children_of(anchor).next()
        && let Some(locus) = text.state().store().locus(top)
    {
        clk.observe(locus.rank);
    }
    let rank = clk.now(0u16);
    let (dot, _) = text.compose(|dot| {
        let mut delta = Rhapsody::new();
        assert!(delta.weave(dot, Locus { anchor, rank }));
        (delta, DotSet::new())
    });
    dot
}

/// Mints one movement testimony through the facade: `target` re-placed at
/// `to`. Returns the testimony's dot beside the delta.
fn move_to(
    moves: &mut Moves,
    target: RawDot,
    to: Locus,
) -> (Dot, crate::metis::Dotted<Metatheses>) {
    moves.compose(|dot| {
        (
            Metatheses::singleton(dot, Metathesis { target, to }),
            DotSet::new(),
        )
    })
}

/// A locus at `anchor` with a fresh rank from `clk`.
fn locus_at(clk: &Clock<TickCounter>, anchor: crate::metis::Anchor) -> Locus {
    Locus {
        anchor,
        rank: clk.now(0u16),
    }
}