Skip to main content

ryo_mutations/basic/
field.rs

1//! Field mutations: AddFieldMutation, RemoveFieldMutation, AddStructLiteralFieldMutation, RemoveStructLiteralFieldMutation
2
3use ryo_symbol::SymbolId;
4
5use crate::Mutation;
6
7/// Add a field to an existing struct
8#[derive(Debug, Clone)]
9pub struct AddFieldMutation {
10    pub struct_id: SymbolId,
11    pub field_name: String,
12    pub field_type: String,
13    pub is_pub: bool,
14}
15
16impl AddFieldMutation {
17    pub fn new(
18        struct_id: SymbolId,
19        field_name: impl Into<String>,
20        field_type: impl Into<String>,
21    ) -> Self {
22        Self {
23            struct_id,
24            field_name: field_name.into(),
25            field_type: field_type.into(),
26            is_pub: false,
27        }
28    }
29
30    pub fn public(mut self) -> Self {
31        self.is_pub = true;
32        self
33    }
34}
35
36impl Mutation for AddFieldMutation {
37    fn describe(&self) -> String {
38        let vis = if self.is_pub { "pub " } else { "" };
39        format!(
40            "Add {}{}: {} to struct {}",
41            vis, self.field_name, self.field_type, self.struct_id
42        )
43    }
44
45    fn mutation_type(&self) -> &'static str {
46        "AddField"
47    }
48
49    fn box_clone(&self) -> Box<dyn Mutation> {
50        Box::new(self.clone())
51    }
52}
53
54/// Remove a field from a struct
55#[derive(Debug, Clone)]
56pub struct RemoveFieldMutation {
57    /// SymbolId for the target struct (required, O(1) access)
58    pub struct_id: SymbolId,
59    pub field_name: String,
60}
61
62impl RemoveFieldMutation {
63    pub fn new(struct_id: SymbolId, field_name: impl Into<String>) -> Self {
64        Self {
65            struct_id,
66            field_name: field_name.into(),
67        }
68    }
69}
70
71impl Mutation for RemoveFieldMutation {
72    fn describe(&self) -> String {
73        format!("Remove field '{}' from {}", self.field_name, self.struct_id)
74    }
75
76    fn mutation_type(&self) -> &'static str {
77        "RemoveField"
78    }
79
80    fn box_clone(&self) -> Box<dyn Mutation> {
81        Box::new(self.clone())
82    }
83}
84
85/// Add a field to all struct literals of a given struct type
86#[derive(Debug, Clone)]
87pub struct AddStructLiteralFieldMutation {
88    /// SymbolId of the struct definition (required, O(1) lookup)
89    pub struct_id: SymbolId,
90    /// The field name to add
91    pub field_name: String,
92    /// The value expression (e.g., "None", "Default::default()")
93    pub value: String,
94}
95
96impl AddStructLiteralFieldMutation {
97    pub fn new(
98        struct_id: SymbolId,
99        field_name: impl Into<String>,
100        value: impl Into<String>,
101    ) -> Self {
102        Self {
103            struct_id,
104            field_name: field_name.into(),
105            value: value.into(),
106        }
107    }
108}
109
110impl Mutation for AddStructLiteralFieldMutation {
111    fn describe(&self) -> String {
112        format!(
113            "Add field '{}' = {} to struct literals ({})",
114            self.field_name, self.value, self.struct_id
115        )
116    }
117
118    fn mutation_type(&self) -> &'static str {
119        "AddStructLiteralField"
120    }
121
122    fn box_clone(&self) -> Box<dyn Mutation> {
123        Box::new(self.clone())
124    }
125}
126
127/// Remove a field from all struct literals of a given struct type
128#[derive(Debug, Clone)]
129pub struct RemoveStructLiteralFieldMutation {
130    /// SymbolId of the struct definition (required, O(1) lookup)
131    pub struct_id: SymbolId,
132    /// The field name to remove
133    pub field_name: String,
134}
135
136impl RemoveStructLiteralFieldMutation {
137    pub fn new(struct_id: SymbolId, field_name: impl Into<String>) -> Self {
138        Self {
139            struct_id,
140            field_name: field_name.into(),
141        }
142    }
143}
144
145impl Mutation for RemoveStructLiteralFieldMutation {
146    fn describe(&self) -> String {
147        format!(
148            "Remove field '{}' from struct literals ({})",
149            self.field_name, self.struct_id
150        )
151    }
152
153    fn mutation_type(&self) -> &'static str {
154        "RemoveStructLiteralField"
155    }
156
157    fn box_clone(&self) -> Box<dyn Mutation> {
158        Box::new(self.clone())
159    }
160}