Return result instead of panicking

This commit is contained in:
Andras Schmelczer 2026-03-10 20:34:07 +00:00
parent b012330a36
commit 776571bc5e

View file

@ -101,21 +101,22 @@ pub fn diff(parent: &str, changed: &TextWithCursors, tokenizer: BuiltinTokenizer
/// Returns a JS error if the diff format is invalid or references ranges /// Returns a JS error if the diff format is invalid or references ranges
/// exceeding the original text length. /// exceeding the original text length.
#[wasm_bindgen(js_name = undiff)] #[wasm_bindgen(js_name = undiff)]
#[must_use] pub fn undiff(
pub fn undiff(parent: &str, diff: Vec<JsValue>, tokenizer: BuiltinTokenizer) -> String { parent: &str,
diff: Vec<JsValue>,
tokenizer: BuiltinTokenizer,
) -> Result<String, JsValue> {
set_panic_hook(); set_panic_hook();
match EditedText::from_diff( let parsed_diff: Vec<_> = diff
parent, .into_iter()
diff.into_iter() .map(std::convert::TryInto::try_into)
.map(std::convert::TryInto::try_into) .collect::<Result<_, _>>()
.collect::<Result<_, _>>() .map_err(|e: crate::types::number_or_text::DeserialisationError| -> JsValue { e.into() })?;
.expect("Invalid diff format"),
&*tokenizer, EditedText::from_diff(parent, parsed_diff, &*tokenizer)
) { .map(|edited_text| edited_text.apply().text())
Ok(edited_text) => edited_text.apply().text(), .map_err(|e| JsValue::from_str(&e.to_string()))
Err(e) => panic!("{}", e),
}
} }
fn set_panic_hook() { fn set_panic_hook() {