Skip to main content

ryo_mutations/basic/
impl_block.rs

1//! Impl block mutations: AddImplMutation, RemoveImplMutation
2
3use ryo_symbol::SymbolId;
4
5use crate::Mutation;
6
7/// Add an impl block for a type
8#[derive(Debug, Clone)]
9pub struct AddImplMutation {
10    /// Parent module SymbolId
11    pub parent: SymbolId,
12    pub target: String,             // e.g., "Greeter"
13    pub trait_name: Option<String>, // e.g., Some("Display")
14}
15
16impl AddImplMutation {
17    pub fn new(parent: SymbolId, target: impl Into<String>) -> Self {
18        Self {
19            parent,
20            target: target.into(),
21            trait_name: None,
22        }
23    }
24
25    pub fn for_trait(mut self, trait_name: impl Into<String>) -> Self {
26        self.trait_name = Some(trait_name.into());
27        self
28    }
29}
30
31impl Mutation for AddImplMutation {
32    fn describe(&self) -> String {
33        match &self.trait_name {
34            Some(t) => format!("Add impl {} for {} to {}", t, self.target, self.parent),
35            None => format!("Add impl {} to {}", self.target, self.parent),
36        }
37    }
38
39    fn mutation_type(&self) -> &'static str {
40        "AddImpl"
41    }
42
43    fn box_clone(&self) -> Box<dyn Mutation> {
44        Box::new(self.clone())
45    }
46}
47
48/// Remove an impl block from the file
49#[derive(Debug, Clone)]
50pub struct RemoveImplMutation {
51    pub symbol_id: SymbolId,
52}
53
54impl RemoveImplMutation {
55    pub fn new(symbol_id: SymbolId) -> Self {
56        Self { symbol_id }
57    }
58}
59
60impl Mutation for RemoveImplMutation {
61    fn describe(&self) -> String {
62        format!("Remove impl {}", self.symbol_id)
63    }
64
65    fn mutation_type(&self) -> &'static str {
66        "RemoveImpl"
67    }
68
69    fn box_clone(&self) -> Box<dyn Mutation> {
70        Box::new(self.clone())
71    }
72}