ryo-mutations 0.1.0

[experimental] Code transformation primitives for Rust source code
Documentation
//! ReplaceExprMutation: Replace expressions with other expressions
//!
//! Converts:
//! ```ignore
//! let x = old_expr;
//! foo(old_expr);
//! ```
//! Into:
//! ```ignore
//! let x = new_expr;
//! foo(new_expr);
//! ```

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

use crate::Mutation;

/// Replace an expression with another expression
#[derive(Debug, Clone)]
pub struct ReplaceExprMutation {
    /// The expression to find (matched by structure)
    pub old_expr: PureExpr,
    /// The replacement expression
    pub new_expr: PureExpr,
    /// Target function SymbolId
    pub target_fn: SymbolId,
    /// Replace all occurrences (true) or just first (false)
    pub replace_all: bool,
}

impl ReplaceExprMutation {
    pub fn new(old_expr: PureExpr, new_expr: PureExpr, target_fn: SymbolId) -> Self {
        Self {
            old_expr,
            new_expr,
            target_fn,
            replace_all: true,
        }
    }

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

impl Mutation for ReplaceExprMutation {
    fn describe(&self) -> String {
        "Replace expression with another expression".to_string()
    }

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

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