ryo_mutations/basic/
enum_def.rs1use ryo_source::pure::{PureField, PureFields, PureTupleField, PureType, PureVis};
4use ryo_symbol::SymbolId;
5
6use crate::Mutation;
7
8#[derive(Debug, Clone)]
10pub struct AddEnumMutation {
11 pub parent: SymbolId,
13 pub name: String,
14 pub variants: Vec<(String, PureFields)>, pub is_pub: bool,
16 pub derives: Vec<String>,
17}
18
19impl AddEnumMutation {
20 pub fn new(parent: SymbolId, name: impl Into<String>) -> Self {
21 Self {
22 parent,
23 name: name.into(),
24 variants: Vec::new(),
25 is_pub: false,
26 derives: Vec::new(),
27 }
28 }
29
30 pub fn with_variant(mut self, name: impl Into<String>) -> Self {
31 self.variants.push((name.into(), PureFields::Unit));
32 self
33 }
34
35 pub fn with_tuple_variant(mut self, name: impl Into<String>, types: Vec<String>) -> Self {
36 let fields: Vec<PureTupleField> = types
37 .into_iter()
38 .map(|t| PureTupleField {
39 attrs: Vec::new(),
40 vis: PureVis::Private,
41 ty: PureType::Path(t),
42 })
43 .collect();
44 self.variants.push((name.into(), PureFields::Tuple(fields)));
45 self
46 }
47
48 pub fn with_struct_variant(
49 mut self,
50 name: impl Into<String>,
51 fields: Vec<(String, String)>,
52 ) -> Self {
53 let pure_fields: Vec<PureField> = fields
54 .into_iter()
55 .map(|(n, t)| PureField {
56 attrs: Vec::new(),
57 vis: PureVis::Private,
58 name: n,
59 ty: PureType::Path(t),
60 })
61 .collect();
62 self.variants
63 .push((name.into(), PureFields::Named(pure_fields)));
64 self
65 }
66
67 pub fn public(mut self) -> Self {
68 self.is_pub = true;
69 self
70 }
71
72 pub fn with_derives(mut self, derives: Vec<String>) -> Self {
73 self.derives = derives;
74 self
75 }
76}
77
78impl Mutation for AddEnumMutation {
79 fn describe(&self) -> String {
80 let vis = if self.is_pub { "pub " } else { "" };
81 format!(
82 "Add {}enum {} with {} variants to {}",
83 vis,
84 self.name,
85 self.variants.len(),
86 self.parent
87 )
88 }
89
90 fn mutation_type(&self) -> &'static str {
91 "AddEnum"
92 }
93
94 fn box_clone(&self) -> Box<dyn Mutation> {
95 Box::new(self.clone())
96 }
97}
98
99#[derive(Debug, Clone)]
101pub struct RemoveEnumMutation {
102 pub symbol_id: SymbolId,
103}
104
105impl RemoveEnumMutation {
106 pub fn new(symbol_id: SymbolId) -> Self {
107 Self { symbol_id }
108 }
109}
110
111impl Mutation for RemoveEnumMutation {
112 fn describe(&self) -> String {
113 format!("Remove enum {}", self.symbol_id)
114 }
115
116 fn mutation_type(&self) -> &'static str {
117 "RemoveEnum"
118 }
119
120 fn box_clone(&self) -> Box<dyn Mutation> {
121 Box::new(self.clone())
122 }
123}
124
125#[derive(Debug, Clone)]
127pub struct AddVariantMutation {
128 pub enum_id: SymbolId,
129 pub variant_name: String,
130 pub fields: PureFields,
131}
132
133impl AddVariantMutation {
134 pub fn new(enum_id: SymbolId, variant_name: impl Into<String>, fields: PureFields) -> Self {
135 Self {
136 enum_id,
137 variant_name: variant_name.into(),
138 fields,
139 }
140 }
141
142 pub fn unit(enum_id: SymbolId, variant_name: impl Into<String>) -> Self {
143 Self::new(enum_id, variant_name, PureFields::Unit)
144 }
145
146 pub fn tuple(enum_id: SymbolId, variant_name: impl Into<String>, types: Vec<String>) -> Self {
147 let fields: Vec<PureTupleField> = types
148 .into_iter()
149 .map(|t| PureTupleField {
150 attrs: Vec::new(),
151 vis: PureVis::Private,
152 ty: PureType::Path(t),
153 })
154 .collect();
155 Self::new(enum_id, variant_name, PureFields::Tuple(fields))
156 }
157
158 pub fn struct_variant(
159 enum_id: SymbolId,
160 variant_name: impl Into<String>,
161 fields: Vec<(String, String)>,
162 ) -> Self {
163 let pure_fields: Vec<PureField> = fields
164 .into_iter()
165 .map(|(n, t)| PureField {
166 attrs: Vec::new(),
167 vis: PureVis::Private,
168 name: n,
169 ty: PureType::Path(t),
170 })
171 .collect();
172 Self::new(enum_id, variant_name, PureFields::Named(pure_fields))
173 }
174}
175
176impl Mutation for AddVariantMutation {
177 fn describe(&self) -> String {
178 format!("Add variant {} to enum {}", self.variant_name, self.enum_id)
179 }
180
181 fn mutation_type(&self) -> &'static str {
182 "AddVariant"
183 }
184
185 fn box_clone(&self) -> Box<dyn Mutation> {
186 Box::new(self.clone())
187 }
188}
189
190#[derive(Debug, Clone)]
192pub struct RemoveVariantMutation {
193 pub enum_id: SymbolId,
195 pub variant_name: String,
196}
197
198impl RemoveVariantMutation {
199 pub fn new(enum_id: SymbolId, variant_name: impl Into<String>) -> Self {
200 Self {
201 enum_id,
202 variant_name: variant_name.into(),
203 }
204 }
205}
206
207impl Mutation for RemoveVariantMutation {
208 fn describe(&self) -> String {
209 format!(
210 "Remove variant {} from enum {}",
211 self.variant_name, self.enum_id
212 )
213 }
214
215 fn mutation_type(&self) -> &'static str {
216 "RemoveVariant"
217 }
218
219 fn box_clone(&self) -> Box<dyn Mutation> {
220 Box::new(self.clone())
221 }
222}