ryo_executor/engine/impls/
manual_map.rs1use ryo_analysis::SymbolKind;
7use ryo_mutations::idiom::ManualMapMutation;
8use ryo_mutations::{Mutation, MutationResult};
9use ryo_source::pure::{PureImplItem, PureItem};
10
11use crate::engine::{ASTMutationContext, ASTRegApply};
12
13impl ASTRegApply for ManualMapMutation {
14 fn apply_to_registry(&self, ctx: &mut ASTMutationContext) -> MutationResult {
15 let mut total_changes = 0;
16
17 if let Some(target_id) = self.target_fn {
18 if let Some(PureItem::Fn(f)) = ctx.ast_registry.get_mut(target_id) {
20 total_changes += self.transform_block(&mut f.body);
21 }
22 } else {
23 let fn_ids: Vec<_> = ctx
25 .symbol_registry
26 .iter()
27 .filter(|(id, _)| {
28 matches!(ctx.symbol_registry.kind(*id), Some(SymbolKind::Function))
29 })
30 .map(|(id, _)| id)
31 .collect();
32
33 for id in fn_ids {
34 if let Some(PureItem::Fn(f)) = ctx.ast_registry.get_mut(id) {
35 total_changes += self.transform_block(&mut f.body);
36 }
37 }
38
39 let impl_ids: Vec<_> = ctx
41 .symbol_registry
42 .iter()
43 .filter(|(id, _)| matches!(ctx.symbol_registry.kind(*id), Some(SymbolKind::Impl)))
44 .map(|(id, _)| id)
45 .collect();
46
47 for id in impl_ids {
48 if let Some(PureItem::Impl(imp)) = ctx.ast_registry.get_mut(id) {
49 for impl_item in &mut imp.items {
50 if let PureImplItem::Fn(f) = impl_item {
51 total_changes += self.transform_block(&mut f.body);
52 }
53 }
54 }
55 }
56 }
57
58 MutationResult {
59 mutation_type: self.mutation_type().to_string(),
60 changes: total_changes,
61 description: if total_changes > 0 {
62 format!(
63 "Converted {} manual map pattern(s) to .map()",
64 total_changes
65 )
66 } else {
67 "No manual map patterns converted".to_string()
68 },
69 }
70 }
71}
72
73#[cfg(test)]
74mod tests {
75 use super::*;
76 use crate::engine::ASTMutationEngine;
77 use ryo_analysis::testing::ContextBuilder;
78
79 #[test]
80 fn test_v2_manual_map_no_changes() {
81 let mut ctx = ContextBuilder::new()
83 .with_file(
84 "src/lib.rs",
85 r#"
86fn process(opt: Option<i32>) -> Option<i32> {
87 opt.map(|x| x + 1)
88}
89"#,
90 )
91 .build();
92
93 let mutation = ManualMapMutation::new();
94 let result = ASTMutationEngine::execute_ast_reg(&mutation, &mut ctx);
95
96 assert_eq!(result.result.changes, 0);
97 }
98}