Skip to content

Commit

Permalink
Fix Clippy and fmt issues
Browse files Browse the repository at this point in the history
  • Loading branch information
ironcev committed Jan 17, 2025
1 parent 2782874 commit d71d4c9
Show file tree
Hide file tree
Showing 11 changed files with 307 additions and 165 deletions.
102 changes: 75 additions & 27 deletions forc-plugins/forc-migrate/src/cli/commands/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,13 @@ pub(crate) fn exec(command: Command) -> Result<()> {
MigrationStepKind::Instruction(instruction) => {
let occurrences_spans = instruction(&program_info)?;

print_instruction_result(&engines, max_len, feature, migration_step, &occurrences_spans);
print_instruction_result(
&engines,
max_len,
feature,
migration_step,
&occurrences_spans,
);

if !occurrences_spans.is_empty() {
println_yellow_bold("If you've already reviewed the above points, you can ignore this info.");
Expand All @@ -142,25 +148,58 @@ pub(crate) fn exec(command: Command) -> Result<()> {
MigrationStepKind::CodeModification(modification, manual_migration_actions) => {
let occurrences_spans = modification(&mut program_info.as_mut(), DryRun::No)?;

output_modified_modules(&build_instructions.manifest_dir()?, &program_info, &occurrences_spans)?;

let stop_migration_process = print_modification_result(max_len, feature, migration_step, manual_migration_actions, &occurrences_spans, &mut current_feature_migration_has_code_changes);
output_modified_modules(
&build_instructions.manifest_dir()?,
&program_info,
&occurrences_spans,
)?;

let stop_migration_process = print_modification_result(
max_len,
feature,
migration_step,
manual_migration_actions,
&occurrences_spans,
&mut current_feature_migration_has_code_changes,
);
if stop_migration_process == StopMigrationProcess::Yes {
return Ok(());
}
}
MigrationStepKind::Interaction(instruction, interaction, manual_migration_actions) => {
MigrationStepKind::Interaction(
instruction,
interaction,
manual_migration_actions,
) => {
let instruction_occurrences_spans = instruction(&program_info)?;

print_instruction_result(&engines, max_len, feature, migration_step, &instruction_occurrences_spans);
print_instruction_result(
&engines,
max_len,
feature,
migration_step,
&instruction_occurrences_spans,
);

// We have occurrences, let's continue with the interaction.
if !instruction_occurrences_spans.is_empty() {
let interaction_occurrences_spans = interaction(&mut program_info.as_mut())?;

output_modified_modules(&build_instructions.manifest_dir()?, &program_info, &interaction_occurrences_spans)?;

let stop_migration_process = print_modification_result(max_len, feature, migration_step, manual_migration_actions, &interaction_occurrences_spans, &mut current_feature_migration_has_code_changes);
let interaction_occurrences_spans =
interaction(&mut program_info.as_mut())?;

output_modified_modules(
&build_instructions.manifest_dir()?,
&program_info,
&interaction_occurrences_spans,
)?;

let stop_migration_process = print_modification_result(
max_len,
feature,
migration_step,
manual_migration_actions,
&interaction_occurrences_spans,
&mut current_feature_migration_has_code_changes,
);
if stop_migration_process == StopMigrationProcess::Yes {
return Ok(());
}
Expand Down Expand Up @@ -196,7 +235,14 @@ enum StopMigrationProcess {
No,
}

fn print_modification_result(max_len: usize, feature: &Feature, migration_step: &MigrationStep, manual_migration_actions: &[&str], occurrences_spans: &[Span], current_feature_migration_has_code_changes: &mut bool) -> StopMigrationProcess {
fn print_modification_result(
max_len: usize,
feature: &Feature,
migration_step: &MigrationStep,
manual_migration_actions: &[&str],
occurrences_spans: &[Span],
current_feature_migration_has_code_changes: &mut bool,
) -> StopMigrationProcess {
if occurrences_spans.is_empty() {
print_checked_action(max_len, feature, migration_step);
StopMigrationProcess::No
Expand Down Expand Up @@ -233,18 +279,21 @@ fn print_modification_result(max_len: usize, feature: &Feature, migration_step:
}
}

fn print_instruction_result(engines: &Engines, max_len: usize, feature: &Feature, migration_step: &MigrationStep, occurrences_spans: &[Span]) {
fn print_instruction_result(
engines: &Engines,
max_len: usize,
feature: &Feature,
migration_step: &MigrationStep,
occurrences_spans: &[Span],
) {
if occurrences_spans.is_empty() {
print_checked_action(max_len, feature, migration_step);
} else {
print_review_action(max_len, feature, migration_step);

if let Some(diagnostic) = create_migration_diagnostic(
engines.se(),
feature,
migration_step,
&occurrences_spans,
) {
if let Some(diagnostic) =
create_migration_diagnostic(engines.se(), feature, migration_step, occurrences_spans)
{
format_diagnostic(&diagnostic);
}
}
Expand All @@ -254,21 +303,20 @@ fn print_instruction_result(engines: &Engines, max_len: usize, feature: &Feature
///
/// A module is considered modified, if any of the [Span]s in `occurrences_spans`
/// has that module as its source.
fn output_modified_modules(manifest_dir: &Path, program_info: &ProgramInfo, occurrences_spans: &[Span]) -> Result<()> {
fn output_modified_modules(
manifest_dir: &Path,
program_info: &ProgramInfo,
occurrences_spans: &[Span],
) -> Result<()> {
if occurrences_spans.is_empty() {
return Ok(());
}

let modified_modules =
ModifiedModules::new(program_info.engines.se(), occurrences_spans);
let modified_modules = ModifiedModules::new(program_info.engines.se(), occurrences_spans);

check_that_modified_modules_are_not_dirty(&modified_modules)?;

output_changed_lexed_program(
manifest_dir,
&modified_modules,
&program_info.lexed_program,
)?;
output_changed_lexed_program(manifest_dir, &modified_modules, &program_info.lexed_program)?;

Ok(())
}
Expand Down
13 changes: 9 additions & 4 deletions forc-plugins/forc-migrate/src/cli/shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,15 +251,20 @@ pub(crate) fn create_migration_diagnostic(
MigrationStepKind::CodeModification(_, []) => vec![],
MigrationStepKind::CodeModification(_, manual_migration_actions) => {
get_manual_migration_actions_help(manual_migration_actions)
},
}
MigrationStepKind::Interaction(_, _, []) => vec![
"This migration step will interactively modify the code, based on your input.".to_string(),
"This migration step will interactively modify the code, based on your input."
.to_string(),
Diagnostic::help_empty_line(),
],
MigrationStepKind::Interaction(_, _, manual_migration_actions) => vec![
"This migration step will interactively modify the code, based on your input.".to_string(),
"This migration step will interactively modify the code, based on your input."
.to_string(),
Diagnostic::help_empty_line(),
].into_iter().chain(get_manual_migration_actions_help(manual_migration_actions)).collect(),
]
.into_iter()
.chain(get_manual_migration_actions_help(manual_migration_actions))
.collect(),
})
.chain(vec![detailed_migration_guide_msg(feature)])
.collect(),
Expand Down
10 changes: 6 additions & 4 deletions forc-plugins/forc-migrate/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ mod matching;
mod modifying;

use std::fmt::Display;
use std::io::Write;
use std::{io, usize, vec};
use std::io::{self, Write};

/// Returns a single error string formed of the `error` and `instructions`.
/// The returned string is formatted to be used as an error message in the [anyhow::bail] macro.
Expand All @@ -31,10 +30,13 @@ fn internal_error<E: Display>(error: E) -> String {
/// Prints a menu containing numbered `options` and asks to choose one of them.
/// Returns zero-indexed index of the chosen option.
fn print_single_choice_menu<S: AsRef<str> + Display>(options: &[S]) -> usize {
assert!(options.len() > 1, "There must be at least two options to choose from.");
assert!(
options.len() > 1,
"There must be at least two options to choose from."
);

for (i, option) in options.iter().enumerate() {
println!("{}. {option}", i+1);
println!("{}. {option}", i + 1);
}

let mut choice = usize::MAX;
Expand Down
82 changes: 52 additions & 30 deletions forc-plugins/forc-migrate/src/matching/lexed_tree.rs
Original file line number Diff line number Diff line change
@@ -1,59 +1,61 @@
//! This module contains helper functions for matching elements within a lexed program.
use super::{any_mut, LexedElementsMatcher, LexedElementsMatcherDeep};
use sway_ast::{ItemKind, ItemStorage, StorageEntry, StorageField};
use sway_core::language::lexed::{LexedModule, LexedProgram};
use super::{any_mut, LexedElementsMatcher, LexedElementsMatcherDeep};

impl LexedElementsMatcher<ItemStorage> for LexedProgram {
fn match_elems<'a, F>(&'a mut self, predicate: F) -> impl Iterator<Item=&'a mut ItemStorage>
fn match_elems<'a, F>(&'a mut self, predicate: F) -> impl Iterator<Item = &'a mut ItemStorage>
where
F: Fn(&&'a mut ItemStorage) -> bool + Clone + 'a,
ItemStorage: 'a
ItemStorage: 'a,
{
// Storage can be declared only in the root of a contract.
self.root.match_elems(predicate)
}
}

impl LexedElementsMatcher<ItemStorage> for LexedModule {
fn match_elems<'a, F>(&'a mut self, predicate: F) -> impl Iterator<Item=&'a mut ItemStorage>
fn match_elems<'a, F>(&'a mut self, predicate: F) -> impl Iterator<Item = &'a mut ItemStorage>
where
F: Fn(&&'a mut ItemStorage) -> bool + Clone + 'a,
ItemStorage: 'a
ItemStorage: 'a,
{
self
.tree
self.tree
.items
.iter_mut()
.map(|annotated_item| &mut annotated_item.value)
.filter_map(move |decl| match decl {
ItemKind::Storage(ref mut item_storage) => if predicate(&item_storage) {
Some(item_storage)
} else {
None
ItemKind::Storage(ref mut item_storage) => {
if predicate(&item_storage) {
Some(item_storage)
} else {
None
}
}
_ => None,
})
}
}

impl LexedElementsMatcher<StorageField> for ItemStorage {
fn match_elems<'a, F>(&'a mut self, predicate: F) -> impl Iterator<Item=&'a mut StorageField>
fn match_elems<'a, F>(&'a mut self, predicate: F) -> impl Iterator<Item = &'a mut StorageField>
where
F: Fn(&&'a mut StorageField) -> bool + Clone + 'a,
StorageField: 'a
StorageField: 'a,
{
self
.entries
self.entries
.inner
.iter_mut()
.map(|annotated_item| &mut annotated_item.value)
.filter_map(move |storage_entry| match storage_entry.field {
Some(ref mut sf) => if predicate(&sf) {
Some(sf)
} else {
None
},
Some(ref mut sf) => {
if predicate(&sf) {
Some(sf)
} else {
None
}
}
None => None,
})
}
Expand All @@ -63,10 +65,13 @@ impl LexedElementsMatcherDeep<StorageField> for ItemStorage {
fn match_elems_deep<'a, F>(&'a mut self, predicate: F) -> Vec<&'a mut StorageField>
where
F: Fn(&&'a mut StorageField) -> bool + Clone + 'a,
StorageField: 'a
StorageField: 'a,
{
fn recursively_collect_storage_fields_in_storage_entry<'a, P>(result: &mut Vec<&'a mut StorageField>, predicate: P, storage_entry: &'a mut StorageEntry)
where
fn recursively_collect_storage_fields_in_storage_entry<'a, P>(
result: &mut Vec<&'a mut StorageField>,
predicate: P,
storage_entry: &'a mut StorageEntry,
) where
P: Fn(&&'a mut StorageField) -> bool + Clone + 'a,
{
if let Some(ref mut sf) = storage_entry.field {
Expand All @@ -80,17 +85,28 @@ impl LexedElementsMatcherDeep<StorageField> for ItemStorage {
.inner
.iter_mut()
.map(|annotated_item| &mut annotated_item.value)
.for_each(|storage_entry| recursively_collect_storage_fields_in_storage_entry(result, predicate.clone(), storage_entry.as_mut()));
.for_each(|storage_entry| {
recursively_collect_storage_fields_in_storage_entry(
result,
predicate.clone(),
storage_entry.as_mut(),
)
});
}
}

let mut result = vec![];
self
.entries
self.entries
.inner
.iter_mut()
.map(|annotated_item| &mut annotated_item.value)
.for_each(|storage_entry| recursively_collect_storage_fields_in_storage_entry(&mut result, predicate.clone(), storage_entry));
.for_each(|storage_entry| {
recursively_collect_storage_fields_in_storage_entry(
&mut result,
predicate.clone(),
storage_entry,
)
});

result
}
Expand All @@ -107,15 +123,21 @@ pub mod matchers {
}

#[allow(dead_code)]
pub(crate) fn storage_fields<'a, P, F>(parent: &'a mut P, predicate: F) -> impl Iterator<Item=&'a mut StorageField>
pub(crate) fn storage_fields<'a, P, F>(
parent: &'a mut P,
predicate: F,
) -> impl Iterator<Item = &'a mut StorageField>
where
F: Fn(&&'a mut StorageField) -> bool + Clone + 'a,
P: LexedElementsMatcher<StorageField>,
{
parent.match_elems(predicate)
}

pub(crate) fn storage_fields_deep<'a, S, F>(scope: &'a mut S, predicate: F) -> Vec<&'a mut StorageField>
pub(crate) fn storage_fields_deep<'a, S, F>(
scope: &'a mut S,
predicate: F,
) -> Vec<&'a mut StorageField>
where
F: Fn(&&'a mut StorageField) -> bool + Clone + 'a,
S: LexedElementsMatcherDeep<StorageField>,
Expand All @@ -137,4 +159,4 @@ pub mod predicates {
storage_field.key_expr.is_none()
}
}
}
}
Loading

0 comments on commit d71d4c9

Please sign in to comment.