ryo-mutations 0.1.0

[experimental] Code transformation primitives for Rust source code
Documentation
//! Derive mutations: AddDeriveMutation, RemoveDeriveMutation

use ryo_symbol::SymbolId;

use crate::Mutation;

/// Add a derive macro to a struct or enum
///
/// Uses SymbolId for O(1) lookup.
#[derive(Debug, Clone)]
pub struct AddDeriveMutation {
    /// SymbolId of the target type (required)
    pub symbol_id: SymbolId,
    pub derives: Vec<String>,
}

impl AddDeriveMutation {
    pub fn new(symbol_id: SymbolId, derives: Vec<String>) -> Self {
        Self { symbol_id, derives }
    }

    pub fn single(symbol_id: SymbolId, derive: impl Into<String>) -> Self {
        Self {
            symbol_id,
            derives: vec![derive.into()],
        }
    }
}

impl Mutation for AddDeriveMutation {
    fn describe(&self) -> String {
        format!(
            "Add #[derive({})] to SymbolId({})",
            self.derives.join(", "),
            self.symbol_id
        )
    }

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

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

/// Remove a derive macro from a struct or enum
///
/// Uses SymbolId for O(1) lookup.
#[derive(Debug, Clone)]
pub struct RemoveDeriveMutation {
    /// SymbolId of the target type (required)
    pub symbol_id: SymbolId,
    pub derives: Vec<String>,
}

impl RemoveDeriveMutation {
    pub fn new(symbol_id: SymbolId, derives: Vec<String>) -> Self {
        Self { symbol_id, derives }
    }

    pub fn single(symbol_id: SymbolId, derive: impl Into<String>) -> Self {
        Self {
            symbol_id,
            derives: vec![derive.into()],
        }
    }
}

impl Mutation for RemoveDeriveMutation {
    fn describe(&self) -> String {
        format!(
            "Remove #[derive({})] from SymbolId({})",
            self.derives.join(", "),
            self.symbol_id
        )
    }

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

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