From f07d66c7cfa9b298b0e36b6035e2d9fe7ffc5eda Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 25 Oct 2026 01:44:21 +0200 Subject: [PATCH] Trims: their own arm in Component::read The trim decode sat inside the read-past fallback and matched the kind there; it now has a TRIM arm, and the fallback only reads past. Long comment lines wrapped. Co-Authored-By: Claude Opus 5.5 --- .../examples/item_wire_probe.rs | 3 +- crates/horizon-protocol/src/item.rs | 33 +++++++++++-------- 2 files changed, 21 insertions(+), 15 deletions(-) diff --git a/crates/horizon-protocol/examples/item_wire_probe.rs b/crates/horizon-protocol/examples/item_wire_probe.rs index 097a9f40d..eb613b520 100644 --- a/crates/horizon-protocol/examples/item_wire_probe.rs +++ b/crates/horizon-protocol/examples/item_wire_probe.rs @@ -3,7 +3,8 @@ //! `item::Slot` and prints, per stack, whether every byte was consumed, the //! patch's component ids (added, then removed), which of them Horizon //! decoded into its own lossy types (the rest it reads past keeping their -//! bytes, or decodes into ids it writes back unchanged, like trims) and whether encoding the decoded stack gives the same bytes back. +//! bytes, or decodes into ids it writes back unchanged, like trims) and +//! whether encoding the decoded stack gives the same bytes back. //! //! cargo run -p horizon-protocol --example item_wire_probe -- vanilla.jsonl rust.jsonl use horizon_protocol::{ diff --git a/crates/horizon-protocol/src/item.rs b/crates/horizon-protocol/src/item.rs index 2872c0228..6b6b845f8 100644 --- a/crates/horizon-protocol/src/item.rs +++ b/crates/horizon-protocol/src/item.rs @@ -236,21 +236,25 @@ impl Component { hidden: (0..n).map(|_| r.varint()).collect::>()?, } } + // ArmorTrim.STREAM_CODEC: two holders, each a reference's id plus + // one. A direct material or pattern has no registry name: read past. + TRIM => { + let start = r.position(); + skip::component(TRIM, r)?; + let bytes = r.since(start); + let mut ids = Reader::new(bytes); + match (ids.varint(), ids.varint()) { + (Ok(material @ 1..), Ok(pattern @ 1..)) if ids.remaining() == 0 => Self::Trim { + material: material - 1, + pattern: pattern - 1, + }, + _ => Self::Raw(bytes.to_vec()), + } + } other => { let start = r.position(); skip::component(other, r)?; - let bytes = r.since(start); - // Two references: each holder is just its id plus one. - let mut ids = Reader::new(bytes); - match (other, ids.varint(), ids.varint()) { - (TRIM, Ok(material @ 1..), Ok(pattern @ 1..)) if ids.remaining() == 0 => { - Self::Trim { - material: material - 1, - pattern: pattern - 1, - } - } - _ => Self::Raw(bytes.to_vec()), - } + Self::Raw(r.since(start).to_vec()) } }) } @@ -668,8 +672,9 @@ mod tests { #[test] fn components_read_past_keep_their_bytes() { // Vanilla 26.3's encoding (tools/test_item_wire_parity.py) of a chestplate's - // armour trim (decoded: both holders are references), a banner's patterns (one direct), a pot's sherds and a stew's - // effects: one server's inventory, recipe book and advancements had them. + // armour trim (decoded: both holders are references), a banner's patterns + // (one direct), a pot's sherds and a stew's effects: one server's + // inventory, recipe book and advancements had them. for (hex, kind) in [ ("01bd0801003a0502", 58), ("01890b01004a02200e000b6d696e6563726166743a7801790b", 74),