Refactor textimport for strict Clippy checks

This commit is contained in:
Dylan M. Taylor
2026-10-28 11:39:25 +00:00
parent c2869280a2
commit f1a5744e8c
+31 -19
View File
@@ -62,6 +62,35 @@ const SKIP: &[&str] = &[
"object",
];
struct ControlWord {
word: String,
parameter: Option<i64>,
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::<String>().parse().ok();
if chars.get(next_index) == Some(&' ') {
next_index += 1;
}
ControlWord { word, parameter, next_index }
}
pub fn import(bytes: &[u8]) -> Result<Imported, ImportError> {
let src = String::from_utf8_lossy(bytes);
if !src.trim_start().starts_with("{\\rtf") {
@@ -159,25 +188,8 @@ pub fn import(bytes: &[u8]) -> Result<Imported, ImportError> {
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<i64> = chars[j..k].iter().collect::<String>().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;