Skip to main content

ryo_executor/engine/impls/
manual_map.rs

1//! V2 ASTRegApply implementation for ManualMapMutation
2//!
3//! Converts manual Option/Result mapping patterns to .map():
4//! - `match opt { Some(x) => Some(f(x)), None => None }` → `opt.map(|x| f(x))`
5
6use 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, ModificationType};
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            // Target function specified: direct lookup
19            if let Some(PureItem::Fn(f)) = ctx.ast_registry.get_mut(target_id) {
20                let changes = self.transform_block(&mut f.body);
21                if changes > 0 {
22                    ctx.emit_modified(target_id, ModificationType::BodyModified);
23                    total_changes += changes;
24                }
25            }
26        } else {
27            // No target: process all functions
28            let fn_ids: Vec<_> = ctx
29                .symbol_registry
30                .iter()
31                .filter(|(id, _)| {
32                    matches!(ctx.symbol_registry.kind(*id), Some(SymbolKind::Function))
33                })
34                .map(|(id, _)| id)
35                .collect();
36
37            for id in fn_ids {
38                if let Some(PureItem::Fn(f)) = ctx.ast_registry.get_mut(id) {
39                    let changes = self.transform_block(&mut f.body);
40                    if changes > 0 {
41                        ctx.emit_modified(id, ModificationType::BodyModified);
42                        total_changes += changes;
43                    }
44                }
45            }
46
47            // Process impl blocks (methods)
48            let impl_ids: Vec<_> = ctx
49                .symbol_registry
50                .iter()
51                .filter(|(id, _)| matches!(ctx.symbol_registry.kind(*id), Some(SymbolKind::Impl)))
52                .map(|(id, _)| id)
53                .collect();
54
55            for id in impl_ids {
56                let mut impl_changes = 0;
57                if let Some(PureItem::Impl(imp)) = ctx.ast_registry.get_mut(id) {
58                    for impl_item in &mut imp.items {
59                        if let PureImplItem::Fn(f) = impl_item {
60                            impl_changes += self.transform_block(&mut f.body);
61                        }
62                    }
63                }
64                if impl_changes > 0 {
65                    ctx.emit_modified(id, ModificationType::BodyModified);
66                    total_changes += impl_changes;
67                }
68            }
69        }
70
71        MutationResult {
72            mutation_type: self.mutation_type().to_string(),
73            changes: total_changes,
74            description: if total_changes > 0 {
75                format!(
76                    "Converted {} manual map pattern(s) to .map()",
77                    total_changes
78                )
79            } else {
80                "No manual map patterns converted".to_string()
81            },
82        }
83    }
84}
85
86#[cfg(test)]
87mod tests {
88    use super::*;
89    use crate::engine::ASTMutationEngine;
90    use ryo_analysis::testing::ContextBuilder;
91
92    #[test]
93    fn test_v2_manual_map_no_changes() {
94        // This test verifies basic integration - manual_map requires specific AST patterns
95        let mut ctx = ContextBuilder::new()
96            .with_file(
97                "src/lib.rs",
98                r#"
99fn process(opt: Option<i32>) -> Option<i32> {
100    opt.map(|x| x + 1)
101}
102"#,
103            )
104            .build();
105
106        let mutation = ManualMapMutation::new();
107        let result = ASTMutationEngine::execute_ast_reg(&mutation, &mut ctx);
108
109        assert_eq!(result.result.changes, 0);
110    }
111}