pipecircuit 0.0.22

Correct. Robust. Fast. Portable. Pick four.
Documentation
use super::StableSlot;

#[derive(
    core::clone::Clone, core::marker::Copy, core::fmt::Debug, core::cmp::Eq, core::cmp::PartialEq,
)]
pub enum StableSlotsInconsistency {
    RowUnrepresentable {
        row: usize,
    },
    NewSlotUnrepresentable {
        slot: usize,
    },
    SlotUnrepresentable {
        slot: StableSlot,
    },
    StoredRowUnrepresentable {
        slot: StableSlot,
        stored_row: u32,
    },
    SlotUnavailable {
        slot: StableSlot,
    },
    SlotOccupied {
        slot: StableSlot,
        row: u32,
    },
    RowUnavailable {
        row: usize,
    },
    SlotVacant {
        slot: StableSlot,
    },
    SlotRowMismatch {
        slot: StableSlot,
        expected: u32,
        actual: u32,
    },
    DuplicateSlot {
        slot: StableSlot,
    },
}

pub(super) fn stored_row<Index: TryFrom<u32>>(
    slot: StableSlot,
    stored_row: u32,
) -> Result<Index, StableSlotsInconsistency> {
    Index::try_from(stored_row)
        .map_err(|_| StableSlotsInconsistency::StoredRowUnrepresentable { slot, stored_row })
}

pub(super) fn slot_index<Index: TryFrom<u32>>(
    slot: StableSlot,
) -> Result<Index, StableSlotsInconsistency> {
    Index::try_from(slot.value())
        .map_err(|_| StableSlotsInconsistency::SlotUnrepresentable { slot })
}

pub(super) fn row(row: usize) -> Result<u32, StableSlotsInconsistency> {
    u32::try_from(row).map_err(|_| StableSlotsInconsistency::RowUnrepresentable { row })
}

pub(super) fn new_slot(slot: usize) -> Result<StableSlot, StableSlotsInconsistency> {
    u32::try_from(slot)
        .map(StableSlot::new)
        .map_err(|_| StableSlotsInconsistency::NewSlotUnrepresentable { slot })
}