ryo-mutations 0.1.0

[experimental] Code transformation primitives for Rust source code
Documentation
//! Const and type alias mutations

use ryo_symbol::SymbolId;

use crate::Mutation;

/// Add a constant to the file
#[derive(Debug, Clone)]
pub struct AddConstMutation {
    pub symbol_id: SymbolId,
    pub name: String,
    pub ty: String,
    pub value: String,
    pub is_pub: bool,
}

impl AddConstMutation {
    pub fn new(
        symbol_id: SymbolId,
        name: impl Into<String>,
        ty: impl Into<String>,
        value: impl Into<String>,
    ) -> Self {
        Self {
            symbol_id,
            name: name.into(),
            ty: ty.into(),
            value: value.into(),
            is_pub: false,
        }
    }

    pub fn public(mut self) -> Self {
        self.is_pub = true;
        self
    }
}

impl Mutation for AddConstMutation {
    fn describe(&self) -> String {
        let vis = if self.is_pub { "pub " } else { "" };
        format!(
            "Add {}const {}: {} = {}",
            vis, self.name, self.ty, self.value
        )
    }

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

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

/// Remove a constant from the file
#[derive(Debug, Clone)]
pub struct RemoveConstMutation {
    pub symbol_id: SymbolId,
}

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

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

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

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

/// Add a type alias to the file
#[derive(Debug, Clone)]
pub struct AddTypeAliasMutation {
    pub symbol_id: SymbolId,
    pub name: String,
    pub ty: String,
    pub is_pub: bool,
}

impl AddTypeAliasMutation {
    pub fn new(symbol_id: SymbolId, name: impl Into<String>, ty: impl Into<String>) -> Self {
        Self {
            symbol_id,
            name: name.into(),
            ty: ty.into(),
            is_pub: false,
        }
    }

    pub fn public(mut self) -> Self {
        self.is_pub = true;
        self
    }
}

impl Mutation for AddTypeAliasMutation {
    fn describe(&self) -> String {
        let vis = if self.is_pub { "pub " } else { "" };
        format!("Add {}type {} = {}", vis, self.name, self.ty)
    }

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

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

/// Remove a type alias from the file
#[derive(Debug, Clone)]
pub struct RemoveTypeAliasMutation {
    pub symbol_id: SymbolId,
}

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

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

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

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