fabrilis/crates/storage/src/migrate/mod.rs
loki5512344 190ef9cc4b Add design persistence, world rendering, metrics overlay and logs
M3: STL/OBJ import with mesh validation, mass/COM/inertia, material database, RON scene with content-addressed blobs and v0->v1 migration. M4: deterministic heightmap terrain, trimesh colliders, wgpu renderer with instancing and depth, winit app with 1 kHz physics, fly camera, on-screen metrics overlay and per-frame CSV/dev logs. Docs: UI, logging, network and headless CLI plans.
2026-11-16 23:40:39 +01:00

101 lines
2.6 KiB
Rust

//! Forward-only schema migrations over RON values.
//!
//! A migration upgrades the payload of one format version to the next. Steps are
//! kept in a [`BTreeMap`] so the chain is explicit and missing links are reported
//! instead of silently skipped (docs/STORAGE.md).
use core::fmt;
use std::collections::BTreeMap;
/// A step from version `version` to `version + 1` over the payload.
pub type Migration = fn(ron::Value) -> Result<ron::Value, MigrateError>;
/// Why a migration chain could not be applied.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum MigrateError {
/// The caller asked to go backwards; saved data is never downgraded.
Downgrade {
/// Version of the loaded value.
from: u32,
/// Requested version, lower than `from`.
to: u32,
},
/// No step is registered for the version that needs upgrading.
MissingStep {
/// Version without a step.
version: u32,
/// Version the chain was asked to reach.
to: u32,
},
/// A step rejected the value it was given.
Step {
/// Version the failing step reads.
version: u32,
/// What was wrong with the value.
reason: String,
},
}
impl MigrateError {
/// Builds [`Self::Step`] for a step that rejects its input.
#[must_use]
pub fn step(version: u32, reason: impl Into<String>) -> Self {
Self::Step {
version,
reason: reason.into(),
}
}
}
impl fmt::Display for MigrateError {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Downgrade { from, to } => {
write!(
formatter,
"cannot migrate version {from} back to version {to}"
)
}
Self::MissingStep { version, to } => write!(
formatter,
"no migration from version {version} while migrating to version {to}"
),
Self::Step { version, reason } => {
write!(
formatter,
"migration from version {version} failed: {reason}"
)
}
}
}
}
impl std::error::Error for MigrateError {}
/// Applies the steps from `from` up to `to`, one version at a time.
///
/// `steps[version]` must upgrade version `version` to `version + 1`. `from == to`
/// is a no-op. A downgrade, a missing step or a rejecting step is an error; the
/// value is left untouched on failure.
pub fn migrate_value(
mut value: ron::Value,
from: u32,
to: u32,
steps: &BTreeMap<u32, Migration>,
) -> Result<ron::Value, MigrateError> {
if to < from {
return Err(MigrateError::Downgrade { from, to });
}
let mut version = from;
while version < to {
let step = steps
.get(&version)
.ok_or(MigrateError::MissingStep { version, to })?;
value = step(value)?;
version += 1;
}
Ok(value)
}
#[cfg(test)]
mod tests;