From f1a5744e8cd88163e9784fcd366c7e925b0395ce Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Wed, 28 Oct 2026 11:39:25 +0000 Subject: [PATCH] Refactor textimport for strict Clippy checks --- crates/textimport/src/rtf.rs | 50 ++++++++++++++++++++++-------------- 1 file changed, 31 insertions(+), 19 deletions(-) diff --git a/crates/textimport/src/rtf.rs b/crates/textimport/src/rtf.rs index 6254ca668..f795804bb 100644 --- a/crates/textimport/src/rtf.rs +++ b/crates/textimport/src/rtf.rs @@ -62,6 +62,35 @@ const SKIP: &[&str] = &[ "object", ]; +struct ControlWord { + word: String, + parameter: Option, + next_index: usize, +} + +fn read_control_word(chars: &[char], index: usize) -> ControlWord { + let mut word_end = index + 1; + while word_end < chars.len() && chars[word_end].is_ascii_alphabetic() { + word_end += 1; + } + let word = chars[index + 1..word_end].iter().collect(); + let mut next_index = word_end; + // A '-' only starts the parameter when a digit follows; otherwise it is text. + let negative = chars.get(next_index) == Some(&'-') && chars.get(next_index + 1).is_some_and(char::is_ascii_digit); + let has_parameter = negative || chars.get(next_index).is_some_and(char::is_ascii_digit); + if has_parameter { + next_index += 1; + while next_index < chars.len() && chars[next_index].is_ascii_digit() { + next_index += 1; + } + } + let parameter = chars[word_end..next_index].iter().collect::().parse().ok(); + if chars.get(next_index) == Some(&' ') { + next_index += 1; + } + ControlWord { word, parameter, next_index } +} + pub fn import(bytes: &[u8]) -> Result { let src = String::from_utf8_lossy(bytes); if !src.trim_start().starts_with("{\\rtf") { @@ -159,25 +188,8 @@ pub fn import(bytes: &[u8]) -> Result { continue; } // A control word with an optional numeric parameter and one delimiting space. - let mut j = i + 1; - while j < chars.len() && chars[j].is_ascii_alphabetic() { - j += 1; - } - let word: String = chars[i + 1..j].iter().collect(); - let mut k = j; - // A '-' only starts the parameter when a digit follows; otherwise it is text. - let negative = chars.get(k) == Some(&'-') && chars.get(k + 1).is_some_and(char::is_ascii_digit); - if negative || chars.get(k).is_some_and(char::is_ascii_digit) { - k += 1; - while k < chars.len() && chars[k].is_ascii_digit() { - k += 1; - } - } - let param: Option = chars[j..k].iter().collect::().parse().ok(); - if k < chars.len() && chars[k] == ' ' { - k += 1; - } - i = k; + let ControlWord { word, parameter: param, next_index } = read_control_word(&chars, i); + i = next_index; // A control word is one character of the `\uN` fallback (a following `\u` is not). if pending > 0 && word != "u" { pending -= 1;