From 0d0fa585a0e49d333dc1d8f8fc92b0a0fbb6af20 Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Mon, 19 Oct 2026 02:23:07 -0400 Subject: [PATCH] story.replaceRange: convert offsets with usize::try_from (wasm32-safe) Co-Authored-By: Claude Opus 5.5 --- crates/engine/src/cmd/text.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/crates/engine/src/cmd/text.rs b/crates/engine/src/cmd/text.rs index 32cbfd0f..12bd540b 100644 --- a/crates/engine/src/cmd/text.rs +++ b/crates/engine/src/cmd/text.rs @@ -125,8 +125,12 @@ pub fn specs() -> Vec { has_doc, |s, p| { let sid = StoryId(p.get("story").and_then(Value::as_u64).ok_or_else(|| bad("story.replaceRange", "missing story"))?); - let a = p.get("start").and_then(Value::as_u64).unwrap_or(0) as usize; - let b = p.get("end").and_then(Value::as_u64).map(|v| v as usize).unwrap_or(a); + let offset = |v: u64| usize::try_from(v).map_err(|_| bad("story.replaceRange", format!("offset {v} is out of range"))); + let a = offset(p.get("start").and_then(Value::as_u64).unwrap_or(0))?; + let b = match p.get("end").and_then(Value::as_u64) { + Some(v) => offset(v)?, + None => a, + }; let text = super::text_param(p, "text"); s.edit(|d, sel| { let st = d.story_mut(sid).ok_or(designcraft_doc::DocError::NoStory(sid))?;