Skip to content

Commit ffd32e9

Browse files
make clippy happy
1 parent f02468b commit ffd32e9

File tree

2 files changed

+30
-40
lines changed

2 files changed

+30
-40
lines changed

i18n-book-to-po/src/structure/align.rs

Lines changed: 25 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ use crate::structure::{
1212
/// This is splitting by "." and replacing these elements by a Vector of Sentence Elements
1313
fn generate_sentence_structure(text: &str) -> Vec<CmarkEvent> {
1414
text.split(".")
15-
.into_iter()
1615
.filter_map(|sentence| {
1716
if sentence.is_empty() {
1817
None
@@ -26,7 +25,7 @@ fn generate_sentence_structure(text: &str) -> Vec<CmarkEvent> {
2625
/// Parse the structure of the provided cmark events and return a Markdown structure
2726
/// It leverages additional sentence elements as structural elements if feature_sentence is set
2827
fn parse_structure(
29-
markdown: &Vec<pulldown_cmark::Event<'_>>,
28+
markdown: &[pulldown_cmark::Event<'_>],
3029
feature_sentence: bool,
3130
) -> Vec<CmarkEvent> {
3231
let structure: Vec<_> = markdown
@@ -38,15 +37,15 @@ fn parse_structure(
3837
if let pulldown_cmark::Event::Text(text) = event {
3938
// prepend the sentence elements with the Text variant
4039
let mut result = vec![(event).into()];
41-
result.extend(generate_sentence_structure(&text));
40+
result.extend(generate_sentence_structure(text));
4241
return result;
4342
}
4443
};
4544
vec![(event).into()]
4645
})
4746
.collect();
4847

49-
return structure;
48+
structure
5049
}
5150

5251
/// normalize the event stream
@@ -67,7 +66,7 @@ fn normalize_events(events: Vec<pulldown_cmark::Event>) -> Vec<pulldown_cmark::E
6766
normalized_events.last_mut()
6867
{
6968
// merge text events (with space as this is a soft break)
70-
*prev_text = format!("{} {}", prev_text, text).into();
69+
*prev_text = format!("{prev_text} {text}").into();
7170
} else {
7271
// add the text event unmodified
7372
normalized_events.push(pulldown_cmark::Event::Text(text));
@@ -100,7 +99,7 @@ fn read_structure(content: &str) -> anyhow::Result<Vec<pulldown_cmark::Event<'_>
10099
opts.insert(pulldown_cmark::Options::ENABLE_TASKLISTS);
101100
opts.insert(pulldown_cmark::Options::ENABLE_HEADING_ATTRIBUTES);
102101

103-
Ok(pulldown_cmark::Parser::new_ext(&content, opts).collect())
102+
Ok(pulldown_cmark::Parser::new_ext(content, opts).collect())
104103
}
105104

106105
/// apply the diff to align the markdown events.
@@ -159,29 +158,23 @@ fn minimize_aligned_events<'a>(
159158
source
160159
.into_iter()
161160
.zip(translated)
162-
.filter_map(|(s, t)| {
163-
if s.is_some() && t.is_some() {
164-
Some((s.unwrap(), t.unwrap()))
165-
} else {
166-
None
167-
}
168-
})
161+
.filter_map(|(s, t)| s.zip(t))
169162
.unzip()
170163
}
171164

172165
/// this is a debug variant of minimize_aligned_events() that returns all events on both sides
173166
/// that don't have a pendant in the other document. This is mostly useful for debugging if the
174167
/// markdown cannot be properly reconstructed.
175168
fn debug_get_unaligned_events<'a>(
176-
source: Vec<Option<Event<'a>>>,
177-
translated: Vec<Option<Event<'a>>>,
178-
) -> (Vec<Option<Event<'a>>>, Vec<Option<Event<'a>>>) {
169+
source: &'a [Option<Event<'a>>],
170+
translated: &'a [Option<Event<'a>>],
171+
) -> (Vec<Option<&'a Event<'a>>>, Vec<Option<&'a Event<'a>>>) {
179172
source
180-
.into_iter()
173+
.iter()
181174
.zip(translated)
182175
.filter_map(|(s, t)| {
183176
if s.is_none() || t.is_none() {
184-
Some((s, t))
177+
Some((s.as_ref(), t.as_ref()))
185178
} else {
186179
None
187180
}
@@ -238,13 +231,12 @@ pub fn align_markdown_docs(
238231
None,
239232
);
240233
if let Err(e) = reconstructed_source {
241-
println!("Error reconstructing source markdown: {:?}", e);
242-
dbg!(&aligned_source);
243-
dbg!(&aligned_translated);
244-
dbg!(debug_get_unaligned_events(
245-
aligned_source,
246-
aligned_translated
247-
));
234+
println!(
235+
"Error reconstructing sourcemarkdown: {e:?}\n{:?}\n{:?}\n{:?}",
236+
&aligned_source,
237+
&aligned_translated,
238+
debug_get_unaligned_events(&aligned_source, &aligned_translated)
239+
);
248240
return Err(e.into());
249241
}
250242
let reconstructed_source = reconstructed_source.unwrap();
@@ -257,13 +249,12 @@ pub fn align_markdown_docs(
257249
None,
258250
);
259251
if let Err(e) = reconstructed_translated {
260-
println!("Error reconstructing translated markdown: {:?}", e);
261-
dbg!(&aligned_source);
262-
dbg!(&aligned_translated);
263-
dbg!(debug_get_unaligned_events(
264-
aligned_source,
265-
aligned_translated
266-
));
252+
println!(
253+
"Error reconstructing translated markdown: {e:?}\n{:?}\n{:?}\n{:?}",
254+
&aligned_source,
255+
&aligned_translated,
256+
debug_get_unaligned_events(&aligned_source, &aligned_translated)
257+
);
267258
return Err(e.into());
268259
}
269260
let reconstructed_translated = reconstructed_translated.unwrap();
@@ -489,14 +480,14 @@ new sentence";
489480
.into_iter()
490481
.flatten()
491482
.cloned()
492-
.collect();
483+
.collect::<Vec<_>>();
493484

494485
// the untranslated paragraph is missing but a new sentence was added by the translator
495486
let translated_events = [&translated_a, &translated_b, &new_paragraph_in_translation]
496487
.into_iter()
497488
.flatten()
498489
.cloned()
499-
.collect();
490+
.collect::<Vec<_>>();
500491
let original_structure = parse_structure(&original_events, false);
501492
let translated_structure = parse_structure(&translated_events, false);
502493
let diff = diff_structure(

i18n-book-to-po/src/structure/diff.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::structure::types::{AlignAction, CmarkEvent, DiffAlgorithm};
44

55
/// this diffs the structure in how the original needs to be modified in order to create the translation.
66
/// lcs_diff:diff() already does the job, but we transform this to a better understandable datastructure
7-
fn diff_structure_lcs(source: &Vec<CmarkEvent>, translated: &Vec<CmarkEvent>) -> Vec<AlignAction> {
7+
fn diff_structure_lcs(source: &[CmarkEvent], translated: &[CmarkEvent]) -> Vec<AlignAction> {
88
lcs_diff::diff(translated, source)
99
.into_iter()
1010
.map(|change| {
@@ -23,8 +23,8 @@ fn diff_structure_lcs(source: &Vec<CmarkEvent>, translated: &Vec<CmarkEvent>) ->
2323
/// this diffs the structure in how the original needs to be modified in order to create the translation.
2424
/// We use the global alignment algorithm NeedlemanWunsch and transform the result into a understandable datastructure
2525
fn diff_structure_seal(
26-
source: &Vec<CmarkEvent>,
27-
translation: &Vec<CmarkEvent>,
26+
source: &[CmarkEvent],
27+
translation: &[CmarkEvent],
2828
) -> Vec<AlignAction> {
2929
// equal is good, align operation is not good
3030
let strategy = seal::pair::NeedlemanWunsch::new(1, -1, -1, 0);
@@ -36,7 +36,6 @@ fn diff_structure_seal(
3636
let global_alignment = set.global_alignment();
3737
global_alignment
3838
.steps()
39-
.into_iter()
4039
.map(|step| {
4140
// x is valid in source and y is valid in target
4241
match step {
@@ -67,8 +66,8 @@ fn diff_structure_seal(
6766

6867
/// diff the structure of to content-less CmarkEvent streams with the specified algorithm
6968
pub fn diff_structure(
70-
source: &Vec<CmarkEvent>,
71-
translated: &Vec<CmarkEvent>,
69+
source: &[CmarkEvent],
70+
translated: &[CmarkEvent],
7271
algorithm: &DiffAlgorithm,
7372
) -> Vec<AlignAction> {
7473
match algorithm {

0 commit comments

Comments
 (0)