minerva 0.2.0

Causal ordering for distributed systems
//! The [`RefoundMap`] wire frame suite (S278): the golden layout fixture,
//! the framing and canonical-form refusals, the entry-order-is-semantic
//! pin, and the round-trip, translation-agreement, budget, and totality
//! laws.
//!
//! Maps are earned, never forged: every fixture and generated map comes
//! out of a real [`Rhapsody::refound`] fold over a built document, so the
//! suite exercises exactly the values the lineage recipe will digest.

extern crate alloc;

use crate::kairos::{Clock, TickCounter};
use crate::metis::{Anchor, Dot, DotSet, Dotted, Locus, Metatheses, RefoundMap, Rhapsody};

mod examples;
mod properties;

type Seq = Dotted<Rhapsody>;

/// A dot literal, for the pins that spell an identity by hand.
fn d(station: u32, counter: u64) -> Dot {
    Dot::from_parts(station, counter).expect("a test literal names a real dot")
}

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

/// A one-element rhapsody delta: `dot` woven at `locus`, a covered pair
/// (the refound suite's twin helper).
fn woven(dot: Dot, locus: Locus) -> Seq {
    let mut rhapsody = Rhapsody::new();
    assert!(rhapsody.weave(dot, locus));
    Dotted::from_store(rhapsody)
}

fn insert(replica: &mut Seq, clk: &Clock<TickCounter>, station: u32, anchor: Anchor) -> Dot {
    let dot = replica.next_dot(station);
    let locus = Locus {
        anchor,
        rank: clk.now(0u16),
    };
    *replica = replica.merge(&woven(dot, locus));
    dot
}

fn delete(replica: &mut Seq, dot: Dot) {
    let mut ctx = DotSet::new();
    assert!(ctx.insert(dot));
    *replica = replica.merge(&Dotted::from_context(ctx));
}

/// The map behind the golden fixture, earned from a real fold. Station 1
/// weaves a three-element chain and deletes its middle; station 2 weaves
/// one element before the chain's head. The effective live order is
/// `(2,1), (1,1), (1,3)`, so the re-mint compacts station 1 onto
/// `(1,1), (1,2)` (old 3 becoming new 2) and station 2 onto `(2,1)`;
/// ceilings are the woven high water `{1: 3, 2: 1}`.
fn fixture_map() -> RefoundMap {
    let mut replica: Seq = Dotted::new();
    let one = clock(1);
    let two = clock(2);
    let a = insert(&mut replica, &one, 1, Anchor::Origin);
    let b = insert(&mut replica, &one, 1, Anchor::After(a.into()));
    let _c = insert(&mut replica, &one, 1, Anchor::After(b.into()));
    let _d = insert(&mut replica, &two, 2, Anchor::Before(a.into()));
    delete(&mut replica, b);

    let refounded = replica
        .store()
        .refound(&Metatheses::new())
        .expect("a sealed stratum folds");
    let (_, map) = refounded.into_parts();
    map
}