ryo-mutations 0.1.0

[experimental] Code transformation primitives for Rust source code
Documentation
//! WrapExprMutation: Wrap expressions with macro calls
//!
//! Converts:
//! ```ignore
//! let x = value;
//! foo(value);
//! ```
//! Into:
//! ```ignore
//! let x = dbg!(value);
//! foo(dbg!(value));
//! ```

use ryo_source::pure::PureExpr;
use ryo_symbol::SymbolId;

use crate::Mutation;

/// Wrap an expression with a macro call (e.g., dbg!, Some, Ok)
#[derive(Debug, Clone)]
pub struct WrapExprMutation {
    /// The expression to wrap (matched by structure)
    pub target_expr: PureExpr,
    /// The wrapper macro name (e.g., "dbg", "Some", "Ok")
    pub wrapper_macro: String,
    /// Target function SymbolId
    pub target_fn: SymbolId,
    /// Wrap all occurrences (true) or just first (false)
    pub wrap_all: bool,
}

impl WrapExprMutation {
    pub fn new(
        target_expr: PureExpr,
        wrapper_macro: impl Into<String>,
        target_fn: SymbolId,
    ) -> Self {
        Self {
            target_expr,
            wrapper_macro: wrapper_macro.into(),
            target_fn,
            wrap_all: true,
        }
    }

    /// Wrap only the first occurrence
    pub fn first_only(mut self) -> Self {
        self.wrap_all = false;
        self
    }
}

impl Mutation for WrapExprMutation {
    fn describe(&self) -> String {
        format!("Wrap expression with {}!()", self.wrapper_macro)
    }

    fn mutation_type(&self) -> &'static str {
        "WrapExpr"
    }

    fn box_clone(&self) -> Box<dyn Mutation> {
        Box::new(self.clone())
    }
}