Files
photocraft/crates/format/src/migrate.rs
.CooCoo 289dd0a874 Format: take the .pcraft envelope from craft-pcraft-format (#10724)
* Format: take the .pcraft envelope from craft-pcraft-format

The manifest envelope, TileRef, Hash, FORMAT_VERSION, EXTENSION, the
entry names, tile_path and blob_path, is_valid_hash, the manifest
nesting and item checks and the migration runner now come from
craft-libs' craft-pcraft-format. photocraft-format re-exports them, so
callers do not change. Manifest is craft_pcraft_format::Manifest<DocM>.

Saved bundles keep the same bytes. A save still checks only the nesting
and the item count of its manifest, not its size, and the manifest
nesting limit is still computed from photocraft_doc::MAX_GROUP_DEPTH.

Tests: a save, load and save again gives the same bytes in every color
mode and depth, with and without previews; MAX_GROUP_DEPTH equals
craft_pcraft_format::MAX_GROUP_DEPTH.

* Format: pin craft-pcraft-format from storytold/craft-libs

* Format: read manifests with craft-pcraft-format's read_manifest_with

Pins craft-pcraft-format to dca870a0, the head of craft-libs #407.
Loading reads the manifest with read_manifest_with, which runs the
limit check, the version check, the migration steps and the envelope
read in one path; the layers are still decoded one at a time after the
rest of the document. A save builds the manifest with Manifest::new and
tile references with TileRef::new, and the bundle's directory names and
the .zst suffix come from the crate's constants. Saved bytes and error
messages are unchanged.

* Format: pin craft-pcraft-format to craft-libs #407's head 04dd1bd

04dd1bd changes only the crate's docs and a test, so photocraft-format
builds the same code.

* Format: pin craft-pcraft-format to craft-libs main at b8f7d93

craft-libs #407 merged as b8f7d93. Its read_manifest also refuses a
thumbnail other than thumb.png and a composite other than
composite/preview.png, the only names photocraft-format writes.
2026-11-02 11:43:46 -05:00

71 lines
2.4 KiB
Rust

//! Manifest migrations. Each step upgrades the raw JSON from version `n` to
//! `n + 1`, so old bundles keep loading as the schema evolves.
use crate::FormatError;
pub(crate) type Step = craft_pcraft_format::Step<FormatError>;
/// `STEPS[i]` upgrades version `i + 1` to `i + 2`. Empty while version 1 is
/// the only version. `craft_pcraft_format::read_manifest_with` runs them on load.
pub(crate) const STEPS: &[Step] = &[];
#[cfg(test)]
mod tests {
use super::*;
use crate::Result;
use crate::manifest::FORMAT_VERSION;
use serde_json::{Value, json};
/// Upgrade `v` in place to `target` using `steps`, with craft-pcraft-format's runner.
fn migrate_with(v: &mut Value, steps: &[Step], target: u32) -> Result<u32> {
craft_pcraft_format::migrate(v, steps, target)
}
fn migrate(v: &mut Value) -> Result<u32> {
migrate_with(v, STEPS, FORMAT_VERSION)
}
fn rename_title(v: &mut Value) -> Result<()> {
let t = v["document"]["title"].take();
v["document"]["name"] = t;
Ok(())
}
fn add_field(v: &mut Value) -> Result<()> {
v["document"]["added"] = json!(true);
Ok(())
}
#[test]
fn chained_steps_run_in_order() {
let mut v = json!({"format_version": 1, "document": {"title": "old"}});
let found = migrate_with(&mut v, &[rename_title, add_field], 3).unwrap();
assert_eq!(found, 1);
assert_eq!(v["format_version"], 3);
assert_eq!(v["document"]["name"], "old");
assert_eq!(v["document"]["added"], true);
}
#[test]
fn partial_chain_from_middle() {
let mut v = json!({"format_version": 2, "document": {}});
migrate_with(&mut v, &[rename_title, add_field], 3).unwrap();
assert_eq!(v["document"]["added"], true);
assert!(v["document"].get("name").is_none());
}
#[test]
fn too_new_and_invalid_versions() {
let mut v = json!({"format_version": 99});
assert!(matches!(migrate(&mut v), Err(FormatError::TooNew { found: 99, .. })));
assert!(migrate(&mut json!({"format_version": 0})).is_err());
assert!(migrate(&mut json!({})).is_err());
assert!(migrate_with(&mut json!({"format_version": 1}), &[], 2).is_err());
}
#[test]
fn current_version_is_noop() {
let mut v = json!({"format_version": FORMAT_VERSION});
assert_eq!(migrate(&mut v).unwrap(), FORMAT_VERSION);
}
}