zerodds_rpc/
evolution_rules.rs1extern crate alloc;
14
15use alloc::vec::Vec;
16
17#[derive(Debug, Clone, Copy, PartialEq, Eq)]
19pub enum Evolution {
20 AddOperation,
22 RemoveOperation,
24 ReorderOperations,
26 ReorderBaseInterfaces,
28 ChangeSignature,
30 DuckTyping,
32 AddRemoveParameter,
34 ReorderParameters,
36 ChangeParameterType,
38 AddRemoveReturnType,
40 ChangeReturnType,
42}
43
44#[derive(Debug, Clone, Copy, PartialEq, Eq)]
46pub enum Mapping {
47 Basic,
49 Enhanced,
51}
52
53#[must_use]
58pub fn is_compatible(mapping: Mapping, evolution: Evolution) -> bool {
59 match (mapping, evolution) {
60 (Mapping::Basic, _) => false,
63
64 (Mapping::Enhanced, Evolution::AddOperation) => true,
67 (Mapping::Enhanced, Evolution::RemoveOperation) => true, (Mapping::Enhanced, Evolution::ReorderOperations) => true,
69 (Mapping::Enhanced, Evolution::ReorderBaseInterfaces) => true,
70 (Mapping::Enhanced, Evolution::DuckTyping) => true,
71 (Mapping::Enhanced, Evolution::AddRemoveParameter) => true,
72 (Mapping::Enhanced, Evolution::ReorderParameters) => true,
73 (Mapping::Enhanced, Evolution::AddRemoveReturnType) => true,
74
75 (Mapping::Enhanced, Evolution::ChangeSignature) => false,
77 (Mapping::Enhanced, Evolution::ChangeParameterType) => false,
78 (Mapping::Enhanced, Evolution::ChangeReturnType) => false,
79 }
80}
81
82#[must_use]
85pub fn compatible_evolutions(mapping: Mapping) -> Vec<Evolution> {
86 [
87 Evolution::AddOperation,
88 Evolution::RemoveOperation,
89 Evolution::ReorderOperations,
90 Evolution::ReorderBaseInterfaces,
91 Evolution::ChangeSignature,
92 Evolution::DuckTyping,
93 Evolution::AddRemoveParameter,
94 Evolution::ReorderParameters,
95 Evolution::ChangeParameterType,
96 Evolution::AddRemoveReturnType,
97 Evolution::ChangeReturnType,
98 ]
99 .iter()
100 .copied()
101 .filter(|e| is_compatible(mapping, *e))
102 .collect()
103}
104
105#[cfg(test)]
106mod tests {
107 use super::*;
108
109 #[test]
112 fn basic_mapping_add_remove_operation_is_breaking() {
113 assert!(!is_compatible(Mapping::Basic, Evolution::AddOperation));
115 assert!(!is_compatible(Mapping::Basic, Evolution::RemoveOperation));
116 }
117
118 #[test]
119 fn basic_mapping_reorder_operations_is_breaking() {
120 assert!(!is_compatible(Mapping::Basic, Evolution::ReorderOperations));
122 assert!(!is_compatible(
123 Mapping::Basic,
124 Evolution::ReorderBaseInterfaces
125 ));
126 }
127
128 #[test]
129 fn basic_mapping_change_signature_is_breaking() {
130 assert!(!is_compatible(Mapping::Basic, Evolution::ChangeSignature));
132 }
133
134 #[test]
135 fn basic_mapping_has_no_compatible_evolutions() {
136 assert!(compatible_evolutions(Mapping::Basic).is_empty());
137 }
138
139 #[test]
142 fn enhanced_mapping_add_operation_is_compatible() {
143 assert!(is_compatible(Mapping::Enhanced, Evolution::AddOperation));
145 }
146
147 #[test]
148 fn enhanced_mapping_remove_operation_is_compatible_with_caveat() {
149 assert!(is_compatible(Mapping::Enhanced, Evolution::RemoveOperation));
151 }
152
153 #[test]
154 fn enhanced_mapping_reorder_operations_is_compatible() {
155 assert!(is_compatible(
157 Mapping::Enhanced,
158 Evolution::ReorderOperations
159 ));
160 assert!(is_compatible(
161 Mapping::Enhanced,
162 Evolution::ReorderBaseInterfaces
163 ));
164 }
165
166 #[test]
167 fn enhanced_mapping_duck_typing_is_compatible() {
168 assert!(is_compatible(Mapping::Enhanced, Evolution::DuckTyping));
170 }
171
172 #[test]
173 fn enhanced_mapping_add_remove_param_is_compatible() {
174 assert!(is_compatible(
176 Mapping::Enhanced,
177 Evolution::AddRemoveParameter
178 ));
179 }
180
181 #[test]
182 fn enhanced_mapping_reorder_params_is_compatible() {
183 assert!(is_compatible(
185 Mapping::Enhanced,
186 Evolution::ReorderParameters
187 ));
188 }
189
190 #[test]
191 fn enhanced_mapping_change_param_type_is_breaking() {
192 assert!(!is_compatible(
195 Mapping::Enhanced,
196 Evolution::ChangeParameterType
197 ));
198 }
199
200 #[test]
201 fn enhanced_mapping_add_remove_return_type_is_compatible() {
202 assert!(is_compatible(
204 Mapping::Enhanced,
205 Evolution::AddRemoveReturnType
206 ));
207 }
208
209 #[test]
210 fn enhanced_mapping_change_return_type_is_breaking() {
211 assert!(!is_compatible(
213 Mapping::Enhanced,
214 Evolution::ChangeReturnType
215 ));
216 }
217
218 #[test]
219 fn enhanced_compatible_evolutions_includes_8_of_11() {
220 let compats = compatible_evolutions(Mapping::Enhanced);
221 assert_eq!(compats.len(), 8);
224 }
225}