use ryo_source::pure::PureStmt;
use ryo_symbol::SymbolId;
use crate::Mutation;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum InsertPosition {
Start,
#[default]
End,
BeforePattern,
AfterPattern,
}
#[derive(Debug, Clone)]
pub struct InsertStatementMutation {
pub stmt: PureStmt,
pub target_fn: SymbolId,
pub position: InsertPosition,
pub reference_stmt: Option<PureStmt>,
}
impl InsertStatementMutation {
pub fn new(stmt: PureStmt, target_fn: SymbolId) -> Self {
Self {
stmt,
target_fn,
position: InsertPosition::End,
reference_stmt: None,
}
}
pub fn at_start(mut self) -> Self {
self.position = InsertPosition::Start;
self
}
pub fn at_end(mut self) -> Self {
self.position = InsertPosition::End;
self
}
pub fn before(mut self, reference_stmt: PureStmt) -> Self {
self.position = InsertPosition::BeforePattern;
self.reference_stmt = Some(reference_stmt);
self
}
pub fn after(mut self, reference_stmt: PureStmt) -> Self {
self.position = InsertPosition::AfterPattern;
self.reference_stmt = Some(reference_stmt);
self
}
}
impl Mutation for InsertStatementMutation {
fn describe(&self) -> String {
format!(
"Insert statement in function '{}' at {:?}",
self.target_fn, self.position
)
}
fn mutation_type(&self) -> &'static str {
"InsertStatement"
}
fn box_clone(&self) -> Box<dyn Mutation> {
Box::new(self.clone())
}
}