Skip to main content

ryo_mutations/basic/stmt/
wrap_expr.rs

1//! WrapExprMutation: Wrap expressions with macro calls
2//!
3//! Converts:
4//! ```ignore
5//! let x = value;
6//! foo(value);
7//! ```
8//! Into:
9//! ```ignore
10//! let x = dbg!(value);
11//! foo(dbg!(value));
12//! ```
13
14use ryo_source::pure::PureExpr;
15use ryo_symbol::SymbolId;
16
17use crate::Mutation;
18
19/// Wrap an expression with a macro call (e.g., dbg!, Some, Ok)
20#[derive(Debug, Clone)]
21pub struct WrapExprMutation {
22    /// The expression to wrap (matched by structure)
23    pub target_expr: PureExpr,
24    /// The wrapper macro name (e.g., "dbg", "Some", "Ok")
25    pub wrapper_macro: String,
26    /// Target function SymbolId
27    pub target_fn: SymbolId,
28    /// Wrap all occurrences (true) or just first (false)
29    pub wrap_all: bool,
30}
31
32impl WrapExprMutation {
33    pub fn new(
34        target_expr: PureExpr,
35        wrapper_macro: impl Into<String>,
36        target_fn: SymbolId,
37    ) -> Self {
38        Self {
39            target_expr,
40            wrapper_macro: wrapper_macro.into(),
41            target_fn,
42            wrap_all: true,
43        }
44    }
45
46    /// Wrap only the first occurrence
47    pub fn first_only(mut self) -> Self {
48        self.wrap_all = false;
49        self
50    }
51}
52
53impl Mutation for WrapExprMutation {
54    fn describe(&self) -> String {
55        format!("Wrap expression with {}!()", self.wrapper_macro)
56    }
57
58    fn mutation_type(&self) -> &'static str {
59        "WrapExpr"
60    }
61
62    fn box_clone(&self) -> Box<dyn Mutation> {
63        Box::new(self.clone())
64    }
65}