Skip to main content

ryo_mutations/basic/
create_mod.rs

1//! CreateModMutation
2//!
3//! Create a new module with content.
4
5use ryo_symbol::SymbolId;
6
7use crate::Mutation;
8
9/// Create a new module with content
10#[derive(Debug, Clone)]
11pub struct CreateModMutation {
12    /// Parent module SymbolId
13    pub parent: SymbolId,
14    /// Name of the new module
15    pub name: String,
16    /// Content of the new module (raw source code)
17    pub content: String,
18    /// Whether the module is public
19    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        // Create a registry and register a parent module
66        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}