ot_tools_io/projects/
options.rs

1/*
2SPDX-License-Identifier: GPL-3.0-or-later
3Copyright © 2024 Mike Robeson [dijksterhuis]
4*/
5
6//! Enums for Octatrack values related to Projects.
7
8use crate::{OptionEnumValueConvert, OtToolsIoErrors, RBoxErr};
9use serde::{Deserialize, Serialize};
10
11/// Sample Slot options for Projects.
12#[derive(Serialize, Deserialize, PartialEq, Debug, Clone, Eq, Hash)]
13pub enum ProjectSampleSlotType {
14    /// Static machine slot
15    Static,
16
17    /// Flex machine slot
18    Flex,
19
20    /// Recorder buffer slot
21    RecorderBuffer,
22}
23
24impl OptionEnumValueConvert for ProjectSampleSlotType {
25    type T = ProjectSampleSlotType;
26    type V = String;
27
28    fn from_value(v: &Self::V) -> RBoxErr<Self::T> {
29        match v.to_ascii_uppercase().as_str() {
30            "STATIC" => Ok(ProjectSampleSlotType::Static),
31            "FLEX" => Ok(ProjectSampleSlotType::Flex),
32            "RECORDER" => Ok(ProjectSampleSlotType::RecorderBuffer),
33            _ => Err(OtToolsIoErrors::NoMatchingOptionEnumValue.into()),
34        }
35    }
36
37    fn value(&self) -> RBoxErr<Self::V> {
38        match self {
39            ProjectSampleSlotType::Static => Ok("STATIC".to_string()),
40            ProjectSampleSlotType::Flex => Ok("FLEX".to_string()),
41            ProjectSampleSlotType::RecorderBuffer => Ok("RECORDER".to_string()),
42        }
43    }
44}
45
46/// MIDI Channel options in Project Settings Menu
47/// (only use when `Disabled` is an option).
48#[derive(PartialEq, Debug, Clone, Default, Serialize, Deserialize, Copy)]
49pub enum ProjectMidiChannels {
50    /// No MIDI Channel selected -- Project Menu -> Control -> Midi -> Sync
51    #[default]
52    Disabled,
53
54    /// MIDI CH 1
55    One,
56
57    /// MIDI CH 2
58    Two,
59
60    /// MIDI CH 3
61    Three,
62
63    /// MIDI CH 4
64    Four,
65
66    /// MIDI CH 5
67    Five,
68
69    /// MIDI CH 6
70    Six,
71
72    /// MIDI CH 7
73    Seven,
74
75    /// MIDI CH 8
76    Eight,
77
78    /// MIDI CH 9
79    Nine,
80
81    /// MIDI CH 10
82    Ten,
83
84    /// MIDI CH 11
85    Eleven,
86
87    /// MIDI CH 12
88    Twelve,
89
90    /// MIDI CH 13
91    Thirteen,
92
93    /// MIDI CH 14
94    Fourteen,
95
96    /// MIDI CH 15
97    Fifteen,
98
99    /// MIDI CH 16
100    Sixteen,
101}
102
103impl OptionEnumValueConvert for ProjectMidiChannels {
104    type T = ProjectMidiChannels;
105    type V = i8;
106
107    fn from_value(v: &Self::V) -> RBoxErr<Self::T> {
108        match v {
109            -1 => Ok(Self::Disabled),
110            1 => Ok(Self::One),
111            2 => Ok(Self::Two),
112            3 => Ok(Self::Three),
113            4 => Ok(Self::Four),
114            5 => Ok(Self::Five),
115            6 => Ok(Self::Six),
116            7 => Ok(Self::Seven),
117            8 => Ok(Self::Eight),
118            9 => Ok(Self::Nine),
119            10 => Ok(Self::Ten),
120            11 => Ok(Self::Eleven),
121            12 => Ok(Self::Twelve),
122            13 => Ok(Self::Thirteen),
123            14 => Ok(Self::Fourteen),
124            15 => Ok(Self::Fifteen),
125            16 => Ok(Self::Sixteen),
126            _ => Err(OtToolsIoErrors::NoMatchingOptionEnumValue.into()),
127        }
128    }
129
130    fn value(&self) -> RBoxErr<Self::V> {
131        match self {
132            Self::Disabled => Ok(-1),
133            Self::One => Ok(1),
134            Self::Two => Ok(2),
135            Self::Three => Ok(3),
136            Self::Four => Ok(4),
137            Self::Five => Ok(5),
138            Self::Six => Ok(6),
139            Self::Seven => Ok(7),
140            Self::Eight => Ok(8),
141            Self::Nine => Ok(9),
142            Self::Ten => Ok(10),
143            Self::Eleven => Ok(11),
144            Self::Twelve => Ok(12),
145            Self::Thirteen => Ok(13),
146            Self::Fourteen => Ok(14),
147            Self::Fifteen => Ok(15),
148            Self::Sixteen => Ok(16),
149        }
150    }
151}
152
153/// "Specification" tests ... ie. guarantee that enum values match correct values.
154#[cfg(test)]
155#[allow(unused_imports)]
156mod test_spec {
157
158    mod ot_sample_slot_type {
159
160        mod value {
161
162            // NOTE: @dijksterhuis: have to import the trait to use it
163            use crate::projects::options::ProjectSampleSlotType;
164            use crate::OptionEnumValueConvert;
165
166            #[test]
167            fn test_static() {
168                assert_eq!(ProjectSampleSlotType::Static.value().unwrap(), "STATIC");
169            }
170            #[test]
171            fn test_flex() {
172                assert_eq!(ProjectSampleSlotType::Flex.value().unwrap(), "FLEX");
173            }
174            #[test]
175            fn test_recorder() {
176                assert_eq!(
177                    ProjectSampleSlotType::RecorderBuffer.value().unwrap(),
178                    "RECORDER"
179                );
180            }
181        }
182
183        mod from_value {
184
185            // NOTE: @dijksterhuis: have to import the trait to use it
186            use crate::projects::options::ProjectSampleSlotType;
187            use crate::OptionEnumValueConvert;
188
189            #[test]
190            fn test_error() {
191                assert!(
192                    ProjectSampleSlotType::from_value(&"SOME INCORRECT STRING".to_string())
193                        .is_err(),
194                );
195            }
196
197            #[test]
198            fn test_static_upper() {
199                assert_eq!(
200                    ProjectSampleSlotType::Static,
201                    ProjectSampleSlotType::from_value(&"STATIC".to_string()).unwrap(),
202                );
203            }
204
205            #[test]
206            fn test_static_lower() {
207                assert_eq!(
208                    ProjectSampleSlotType::Static,
209                    ProjectSampleSlotType::from_value(&"static".to_string()).unwrap(),
210                );
211            }
212
213            #[test]
214            fn test_flex_upper() {
215                assert_eq!(
216                    ProjectSampleSlotType::Flex,
217                    ProjectSampleSlotType::from_value(&"FLEX".to_string()).unwrap(),
218                );
219            }
220
221            #[test]
222            fn test_flex_lower() {
223                assert_eq!(
224                    ProjectSampleSlotType::Flex,
225                    ProjectSampleSlotType::from_value(&"flex".to_string()).unwrap(),
226                );
227            }
228
229            #[test]
230            fn test_recorder_upper() {
231                assert_eq!(
232                    ProjectSampleSlotType::RecorderBuffer,
233                    ProjectSampleSlotType::from_value(&"RECORDER".to_string()).unwrap(),
234                );
235            }
236
237            #[test]
238            fn test_recorder_lower() {
239                assert_eq!(
240                    ProjectSampleSlotType::RecorderBuffer,
241                    ProjectSampleSlotType::from_value(&"recorder".to_string()).unwrap(),
242                );
243            }
244        }
245    }
246
247    mod ot_proj_settings_midi_channels {
248
249        mod value {
250            use crate::projects::options::ProjectMidiChannels;
251            use crate::OptionEnumValueConvert;
252
253            #[test]
254            fn test_disabled() {
255                assert_eq!(ProjectMidiChannels::Disabled.value().unwrap(), -1);
256            }
257            #[test]
258            fn test_1() {
259                assert_eq!(ProjectMidiChannels::One.value().unwrap(), 1);
260            }
261            #[test]
262            fn test_2() {
263                assert_eq!(ProjectMidiChannels::Two.value().unwrap(), 2);
264            }
265            #[test]
266            fn test_3() {
267                assert_eq!(ProjectMidiChannels::Three.value().unwrap(), 3);
268            }
269            #[test]
270            fn test_4() {
271                assert_eq!(ProjectMidiChannels::Four.value().unwrap(), 4);
272            }
273            #[test]
274            fn test_5() {
275                assert_eq!(ProjectMidiChannels::Five.value().unwrap(), 5);
276            }
277            #[test]
278            fn test_6() {
279                assert_eq!(ProjectMidiChannels::Six.value().unwrap(), 6);
280            }
281            #[test]
282            fn test_7() {
283                assert_eq!(ProjectMidiChannels::Seven.value().unwrap(), 7);
284            }
285            #[test]
286            fn test_8() {
287                assert_eq!(ProjectMidiChannels::Eight.value().unwrap(), 8);
288            }
289            #[test]
290            fn test_9() {
291                assert_eq!(ProjectMidiChannels::Nine.value().unwrap(), 9);
292            }
293            #[test]
294            fn test_10() {
295                assert_eq!(ProjectMidiChannels::Ten.value().unwrap(), 10);
296            }
297            #[test]
298            fn test_11() {
299                assert_eq!(ProjectMidiChannels::Eleven.value().unwrap(), 11);
300            }
301            #[test]
302            fn test_12() {
303                assert_eq!(ProjectMidiChannels::Twelve.value().unwrap(), 12);
304            }
305            #[test]
306            fn test_13() {
307                assert_eq!(ProjectMidiChannels::Thirteen.value().unwrap(), 13);
308            }
309            #[test]
310            fn test_14() {
311                assert_eq!(ProjectMidiChannels::Fourteen.value().unwrap(), 14);
312            }
313            #[test]
314            fn test_15() {
315                assert_eq!(ProjectMidiChannels::Fifteen.value().unwrap(), 15);
316            }
317            #[test]
318            fn test_16() {
319                assert_eq!(ProjectMidiChannels::Sixteen.value().unwrap(), 16);
320            }
321        }
322
323        mod from_value {
324
325            use crate::projects::options::ProjectMidiChannels;
326            use crate::OptionEnumValueConvert;
327
328            #[test]
329            fn test_error_1() {
330                assert!(ProjectMidiChannels::from_value(&100).is_err());
331            }
332            #[test]
333            fn test_error_2() {
334                assert!(ProjectMidiChannels::from_value(&0).is_err());
335            }
336            #[test]
337            fn test_disabled() {
338                assert_eq!(
339                    ProjectMidiChannels::Disabled,
340                    ProjectMidiChannels::from_value(&-1).unwrap()
341                );
342            }
343            #[test]
344            fn test_1() {
345                assert_eq!(
346                    ProjectMidiChannels::One,
347                    ProjectMidiChannels::from_value(&1).unwrap()
348                );
349            }
350            #[test]
351            fn test_2() {
352                assert_eq!(
353                    ProjectMidiChannels::Two,
354                    ProjectMidiChannels::from_value(&2).unwrap()
355                );
356            }
357            #[test]
358            fn test_3() {
359                assert_eq!(
360                    ProjectMidiChannels::Three,
361                    ProjectMidiChannels::from_value(&3).unwrap()
362                );
363            }
364            #[test]
365            fn test_4() {
366                assert_eq!(
367                    ProjectMidiChannels::Four,
368                    ProjectMidiChannels::from_value(&4).unwrap()
369                );
370            }
371            #[test]
372            fn test_5() {
373                assert_eq!(
374                    ProjectMidiChannels::Five,
375                    ProjectMidiChannels::from_value(&5).unwrap()
376                );
377            }
378            #[test]
379            fn test_6() {
380                assert_eq!(
381                    ProjectMidiChannels::Six,
382                    ProjectMidiChannels::from_value(&6).unwrap()
383                );
384            }
385            #[test]
386            fn test_7() {
387                assert_eq!(
388                    ProjectMidiChannels::Seven,
389                    ProjectMidiChannels::from_value(&7).unwrap()
390                );
391            }
392            #[test]
393            fn test_8() {
394                assert_eq!(
395                    ProjectMidiChannels::Eight,
396                    ProjectMidiChannels::from_value(&8).unwrap()
397                );
398            }
399            #[test]
400            fn test_9() {
401                assert_eq!(
402                    ProjectMidiChannels::Nine,
403                    ProjectMidiChannels::from_value(&9).unwrap()
404                );
405            }
406            #[test]
407            fn test_10() {
408                assert_eq!(
409                    ProjectMidiChannels::Ten,
410                    ProjectMidiChannels::from_value(&10).unwrap()
411                );
412            }
413            #[test]
414            fn test_11() {
415                assert_eq!(
416                    ProjectMidiChannels::Eleven,
417                    ProjectMidiChannels::from_value(&11).unwrap()
418                );
419            }
420            #[test]
421            fn test_12() {
422                assert_eq!(
423                    ProjectMidiChannels::Twelve,
424                    ProjectMidiChannels::from_value(&12).unwrap()
425                );
426            }
427            #[test]
428            fn test_13() {
429                assert_eq!(
430                    ProjectMidiChannels::Thirteen,
431                    ProjectMidiChannels::from_value(&13).unwrap()
432                );
433            }
434            #[test]
435            fn test_14() {
436                assert_eq!(
437                    ProjectMidiChannels::Fourteen,
438                    ProjectMidiChannels::from_value(&14).unwrap()
439                );
440            }
441            #[test]
442            fn test_15() {
443                assert_eq!(
444                    ProjectMidiChannels::Fifteen,
445                    ProjectMidiChannels::from_value(&15).unwrap()
446                );
447            }
448            #[test]
449            fn test_16() {
450                assert_eq!(
451                    ProjectMidiChannels::Sixteen,
452                    ProjectMidiChannels::from_value(&16).unwrap()
453                );
454            }
455        }
456    }
457}