diff --git a/reconcile-python/src/lib.rs b/reconcile-python/src/lib.rs index a50366a..f068485 100644 --- a/reconcile-python/src/lib.rs +++ b/reconcile-python/src/lib.rs @@ -1,8 +1,6 @@ use pyo3::prelude::*; use pyo3::types::{PyDict, PyList}; -use reconcile_text::{ - BuiltinTokenizer, CursorPosition, EditedText, NumberOrText, TextWithCursors, -}; +use reconcile_text::{BuiltinTokenizer, CursorPosition, EditedText, NumberOrText, TextWithCursors}; fn parse_tokenizer(tokenizer: &str) -> PyResult { match tokenizer { @@ -31,6 +29,7 @@ fn extract_text_with_cursors(input: &Bound<'_, PyAny>) -> PyResult { let list = obj.cast::()?; + let text_length = text.chars().count(); let mut cursors = Vec::with_capacity(list.len()); for item in list { let cursor_dict = item.cast::()?; @@ -42,6 +41,11 @@ fn extract_text_with_cursors(input: &Bound<'_, PyAny>) -> PyResult text_length { + return Err(pyo3::exceptions::PyValueError::new_err(format!( + "Cursor position {position} exceeds text length {text_length}" + ))); + } cursors.push(CursorPosition::new(id, position)); } cursors