ryo-mutations 0.1.0

[experimental] Code transformation primitives for Rust source code
Documentation
//! Impl block mutations: AddImplMutation, RemoveImplMutation

use ryo_symbol::SymbolId;

use crate::Mutation;

/// Add an impl block for a type
#[derive(Debug, Clone)]
pub struct AddImplMutation {
    /// Parent module SymbolId
    pub parent: SymbolId,
    pub target: String,             // e.g., "Greeter"
    pub trait_name: Option<String>, // e.g., Some("Display")
}

impl AddImplMutation {
    pub fn new(parent: SymbolId, target: impl Into<String>) -> Self {
        Self {
            parent,
            target: target.into(),
            trait_name: None,
        }
    }

    pub fn for_trait(mut self, trait_name: impl Into<String>) -> Self {
        self.trait_name = Some(trait_name.into());
        self
    }
}

impl Mutation for AddImplMutation {
    fn describe(&self) -> String {
        match &self.trait_name {
            Some(t) => format!("Add impl {} for {} to {}", t, self.target, self.parent),
            None => format!("Add impl {} to {}", self.target, self.parent),
        }
    }

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

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

/// Remove an impl block from the file
#[derive(Debug, Clone)]
pub struct RemoveImplMutation {
    pub symbol_id: SymbolId,
}

impl RemoveImplMutation {
    pub fn new(symbol_id: SymbolId) -> Self {
        Self { symbol_id }
    }
}

impl Mutation for RemoveImplMutation {
    fn describe(&self) -> String {
        format!("Remove impl {}", self.symbol_id)
    }

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

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