From c8429922b71a8a549fd0f9e12de95c019627c631 Mon Sep 17 00:00:00 2001 From: scarf Date: Tue, 28 Nov 2023 02:25:12 +0900 Subject: [PATCH] refactor: use iterator (#1) --- src/format_text.rs | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/format_text.rs b/src/format_text.rs index 5d16cce..25c5a49 100644 --- a/src/format_text.rs +++ b/src/format_text.rs @@ -39,13 +39,13 @@ fn format_root( let root_obj = root_value.as_object()?; let maybe_default_language = get_metadata_language(root_obj); let cells = root_value.as_object()?.get_array("cells")?; - let mut text_changes = Vec::new(); - for element in &cells.elements { - let maybe_text_change = get_cell_text_change(input_text, element, maybe_default_language, &mut format_with_host); - if let Some(text_change) = maybe_text_change { - text_changes.push(text_change); - } - } + + let text_changes: Vec = cells + .elements + .iter() + .filter_map(|element| get_cell_text_change(input_text, element, maybe_default_language, &mut format_with_host)) + .collect(); + if text_changes.is_empty() { None } else { @@ -105,6 +105,7 @@ fn analyze_code_block<'a>(cell: &jsonc_parser::ast::Object<'a>, file_text: &'a s } strings.push(&string_lit.value); } + let mut text = String::with_capacity(strings.iter().map(|s| s.len()).sum::()); for string in strings { text.push_str(string);