ryo_mutations/basic/
create_mod.rs1use ryo_symbol::SymbolId;
6
7use crate::Mutation;
8
9#[derive(Debug, Clone)]
11pub struct CreateModMutation {
12 pub parent: SymbolId,
14 pub name: String,
16 pub content: String,
18 pub is_pub: bool,
20}
21
22impl CreateModMutation {
23 pub fn new(parent: SymbolId, name: impl Into<String>) -> Self {
24 Self {
25 parent,
26 name: name.into(),
27 content: String::new(),
28 is_pub: false,
29 }
30 }
31
32 pub fn with_content(mut self, content: impl Into<String>) -> Self {
33 self.content = content.into();
34 self
35 }
36
37 pub fn public(mut self) -> Self {
38 self.is_pub = true;
39 self
40 }
41}
42
43impl Mutation for CreateModMutation {
44 fn mutation_type(&self) -> &'static str {
45 "CreateMod"
46 }
47
48 fn describe(&self) -> String {
49 let vis = if self.is_pub { "pub " } else { "" };
50 format!("Create {}mod '{}' in {}", vis, self.name, self.parent)
51 }
52
53 fn box_clone(&self) -> Box<dyn Mutation> {
54 Box::new(self.clone())
55 }
56}
57
58#[cfg(test)]
59mod tests {
60 use super::*;
61 use ryo_symbol::{SymbolKind, SymbolPath, SymbolRegistry};
62
63 #[test]
64 fn test_create_mod_mutation_describe() {
65 let mut registry = SymbolRegistry::new();
67 let parent_path = SymbolPath::parse("test_crate").unwrap();
68 let parent_id = registry.register(parent_path, SymbolKind::Mod).unwrap();
69
70 let mutation = CreateModMutation::new(parent_id, "models")
71 .with_content("pub struct Config {}")
72 .public();
73 assert!(mutation.describe().contains("pub mod"));
74 assert!(mutation.describe().contains("models"));
75 }
76}