Skip to main content

rodecaster_protocol/names/
core.rs

1//! Model-independent core vocabulary: [`DeviceModel`], [`MixOutput`],
2//! [`Source`], and the model-parameterized [`Fader`]. These pin the wire's bare
3//! positional ordinals to named entities; see the [`super`] module docs for why.
4
5use std::fmt;
6use std::str::FromStr;
7
8/// Which RODECaster a fullSync came from. Detected from the `SYSTEM` node;
9/// selects the per-model [`Fader`] layout.
10#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
11pub enum DeviceModel {
12    /// RODECaster Pro II: 6 physical + 3 virtual faders.
13    Pro2,
14    /// RODECaster Duo: 4 physical + 5 virtual faders.
15    Duo,
16}
17
18impl DeviceModel {
19    /// Map the `SYSTEM.boardType` integer (`0` = Pro II, `1` = Duo). Returns
20    /// `None` for an unrecognized board so [`DeviceModel::detect`] can fall back
21    /// to the name.
22    pub fn from_board_type(board_type: i64) -> Option<Self> {
23        match board_type {
24            0 => Some(Self::Pro2),
25            1 => Some(Self::Duo),
26            _ => None,
27        }
28    }
29
30    /// Map the `SYSTEM.systemName` string: contains "duo" (case-insensitive) =>
31    /// Duo, else Pro II. Firmware 1.6.8 omits this property; newer firmware
32    /// includes it, which is why [`DeviceModel::detect`] prefers `boardType`.
33    pub fn from_system_name(system_name: &str) -> Self {
34        if system_name.to_lowercase().contains("duo") {
35            Self::Duo
36        } else {
37            Self::Pro2
38        }
39    }
40
41    /// Detect the model from the `SYSTEM` node's `boardType` and `systemName`.
42    /// `boardType` is authoritative (present and correct on every firmware
43    /// observed); `systemName` is the fallback; with neither, default to Pro II.
44    pub fn detect(board_type: Option<i64>, system_name: Option<&str>) -> Self {
45        if let Some(model) = board_type.and_then(Self::from_board_type) {
46            return model;
47        }
48        if let Some(name) = system_name {
49            return Self::from_system_name(name);
50        }
51        Self::Pro2
52    }
53}
54
55impl fmt::Display for DeviceModel {
56    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
57        f.write_str(match self {
58            Self::Pro2 => "RODECaster Pro II",
59            Self::Duo => "RODECaster Duo",
60        })
61    }
62}
63
64/// A mix output bus: where audio is routed *to*. Model-independent (13 buses).
65/// The protocol mix index equals enum order (`Headphone1` = 0 .. `CallMe3` =
66/// 12); it is the second dimension of the source-major mix matrix.
67#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
68pub enum MixOutput {
69    Headphone1,
70    Headphone2,
71    Headphone3,
72    Headphone4,
73    Speaker,
74    Recording,
75    Bluetooth,
76    Usb1,
77    Chat,
78    Usb2,
79    CallMe1,
80    CallMe2,
81    CallMe3,
82}
83
84impl MixOutput {
85    /// The protocol mix index (0..=12).
86    pub fn to_protocol(self) -> u8 {
87        match self {
88            Self::Headphone1 => 0,
89            Self::Headphone2 => 1,
90            Self::Headphone3 => 2,
91            Self::Headphone4 => 3,
92            Self::Speaker => 4,
93            Self::Recording => 5,
94            Self::Bluetooth => 6,
95            Self::Usb1 => 7,
96            Self::Chat => 8,
97            Self::Usb2 => 9,
98            Self::CallMe1 => 10,
99            Self::CallMe2 => 11,
100            Self::CallMe3 => 12,
101        }
102    }
103
104    /// Inverse of [`MixOutput::to_protocol`]; `None` for an out-of-range index.
105    pub fn from_protocol(idx: u8) -> Option<Self> {
106        Some(match idx {
107            0 => Self::Headphone1,
108            1 => Self::Headphone2,
109            2 => Self::Headphone3,
110            3 => Self::Headphone4,
111            4 => Self::Speaker,
112            5 => Self::Recording,
113            6 => Self::Bluetooth,
114            7 => Self::Usb1,
115            8 => Self::Chat,
116            9 => Self::Usb2,
117            10 => Self::CallMe1,
118            11 => Self::CallMe2,
119            12 => Self::CallMe3,
120            _ => return None,
121        })
122    }
123
124    /// Human-readable label for UIs and logs.
125    pub fn label(self) -> &'static str {
126        match self {
127            Self::Headphone1 => "Headphone 1",
128            Self::Headphone2 => "Headphone 2",
129            Self::Headphone3 => "Headphone 3",
130            Self::Headphone4 => "Headphone 4",
131            Self::Speaker => "Speaker",
132            Self::Recording => "Recording",
133            Self::Bluetooth => "Bluetooth",
134            Self::Usb1 => "USB 1",
135            Self::Chat => "Chat",
136            Self::Usb2 => "USB 2",
137            Self::CallMe1 => "CallMe 1",
138            Self::CallMe2 => "CallMe 2",
139            Self::CallMe3 => "CallMe 3",
140        }
141    }
142}
143
144/// Error returned when parsing an invalid mix output name.
145#[derive(Debug, Clone, PartialEq, Eq)]
146pub struct ParseMixOutputError(pub String);
147
148impl fmt::Display for ParseMixOutputError {
149    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
150        write!(
151            f,
152            "unknown mix output: {} (try: hp1, speaker, bt, cm1)",
153            self.0
154        )
155    }
156}
157
158impl std::error::Error for ParseMixOutputError {}
159
160impl FromStr for MixOutput {
161    type Err = ParseMixOutputError;
162    fn from_str(s: &str) -> Result<Self, Self::Err> {
163        match s.to_lowercase().as_str() {
164            "headphone1" | "hp1" => Ok(Self::Headphone1),
165            "headphone2" | "hp2" => Ok(Self::Headphone2),
166            "headphone3" | "hp3" => Ok(Self::Headphone3),
167            "headphone4" | "hp4" => Ok(Self::Headphone4),
168            "speaker" | "spk" | "monitor" => Ok(Self::Speaker),
169            "recording" | "rec" => Ok(Self::Recording),
170            "bluetooth" | "bt" => Ok(Self::Bluetooth),
171            "usb1" => Ok(Self::Usb1),
172            "chat" => Ok(Self::Chat),
173            "usb2" => Ok(Self::Usb2),
174            "callme1" | "cm1" => Ok(Self::CallMe1),
175            "callme2" | "cm2" => Ok(Self::CallMe2),
176            "callme3" | "cm3" => Ok(Self::CallMe3),
177            _ => Err(ParseMixOutputError(s.to_string())),
178        }
179    }
180}
181
182impl fmt::Display for MixOutput {
183    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
184        f.write_str(match self {
185            Self::Headphone1 => "headphone1",
186            Self::Headphone2 => "headphone2",
187            Self::Headphone3 => "headphone3",
188            Self::Headphone4 => "headphone4",
189            Self::Speaker => "speaker",
190            Self::Recording => "recording",
191            Self::Bluetooth => "bluetooth",
192            Self::Usb1 => "usb1",
193            Self::Chat => "chat",
194            Self::Usb2 => "usb2",
195            Self::CallMe1 => "callme1",
196            Self::CallMe2 => "callme2",
197            Self::CallMe3 => "callme3",
198        })
199    }
200}
201
202/// An audio source: where audio comes *from*. Model-independent (19 sources,
203/// one shared device picker across the Pro family). The protocol source index
204/// equals enum order (`Combo1` = 0 .. `CallMe3` = 18); it is the first
205/// dimension (source-major) of the mix matrix.
206///
207/// `CallMe1..=CallMe3` (16..=18) are return channels addressed outside the
208/// regular matrix on some firmware; see [`Source::is_callme`].
209#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
210pub enum Source {
211    Combo1,
212    Combo2,
213    Combo3,
214    Combo4,
215    Combo1_2,
216    Combo2_3,
217    Combo3_4,
218    Usb1,
219    Chat,
220    Usb2,
221    Bluetooth,
222    SoundPad,
223    VirtualGame,
224    VirtualMusic,
225    VirtualA,
226    VirtualB,
227    CallMe1,
228    CallMe2,
229    CallMe3,
230}
231
232impl Source {
233    /// The protocol source index (0..=18).
234    pub fn to_protocol(self) -> u8 {
235        match self {
236            Self::Combo1 => 0,
237            Self::Combo2 => 1,
238            Self::Combo3 => 2,
239            Self::Combo4 => 3,
240            Self::Combo1_2 => 4,
241            Self::Combo2_3 => 5,
242            Self::Combo3_4 => 6,
243            Self::Usb1 => 7,
244            Self::Chat => 8,
245            Self::Usb2 => 9,
246            Self::Bluetooth => 10,
247            Self::SoundPad => 11,
248            Self::VirtualGame => 12,
249            Self::VirtualMusic => 13,
250            Self::VirtualA => 14,
251            Self::VirtualB => 15,
252            Self::CallMe1 => 16,
253            Self::CallMe2 => 17,
254            Self::CallMe3 => 18,
255        }
256    }
257
258    /// Inverse of [`Source::to_protocol`]; `None` for an out-of-range index.
259    pub fn from_protocol(idx: u8) -> Option<Self> {
260        Some(match idx {
261            0 => Self::Combo1,
262            1 => Self::Combo2,
263            2 => Self::Combo3,
264            3 => Self::Combo4,
265            4 => Self::Combo1_2,
266            5 => Self::Combo2_3,
267            6 => Self::Combo3_4,
268            7 => Self::Usb1,
269            8 => Self::Chat,
270            9 => Self::Usb2,
271            10 => Self::Bluetooth,
272            11 => Self::SoundPad,
273            12 => Self::VirtualGame,
274            13 => Self::VirtualMusic,
275            14 => Self::VirtualA,
276            15 => Self::VirtualB,
277            16 => Self::CallMe1,
278            17 => Self::CallMe2,
279            18 => Self::CallMe3,
280            _ => return None,
281        })
282    }
283
284    /// CallMe return channels are routed through a dedicated request path
285    /// outside the regular mix matrix on firmware where they sit past
286    /// `Layout::source_count`. Encoders use this to pick the wire form.
287    pub fn is_callme(self) -> bool {
288        matches!(self, Self::CallMe1 | Self::CallMe2 | Self::CallMe3)
289    }
290
291    /// Human-readable label for UIs and logs.
292    pub fn label(self) -> &'static str {
293        match self {
294            Self::Combo1 => "Combo 1",
295            Self::Combo2 => "Combo 2",
296            Self::Combo3 => "Combo 3",
297            Self::Combo4 => "Combo 4",
298            Self::Combo1_2 => "Combo 1+2",
299            Self::Combo2_3 => "Combo 2+3",
300            Self::Combo3_4 => "Combo 3+4",
301            Self::Usb1 => "USB 1",
302            Self::Chat => "Chat",
303            Self::Usb2 => "USB 2",
304            Self::Bluetooth => "Bluetooth",
305            Self::SoundPad => "Sound Pad",
306            Self::VirtualGame => "Game",
307            Self::VirtualMusic => "Music",
308            Self::VirtualA => "Virtual A",
309            Self::VirtualB => "Virtual B",
310            Self::CallMe1 => "CallMe 1",
311            Self::CallMe2 => "CallMe 2",
312            Self::CallMe3 => "CallMe 3",
313        }
314    }
315}
316
317/// Error returned when parsing an invalid input source name.
318#[derive(Debug, Clone, PartialEq, Eq)]
319pub struct ParseSourceError(pub String);
320
321impl fmt::Display for ParseSourceError {
322    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
323        write!(f, "unknown source: {} (try: combo1, bt, game, cm1)", self.0)
324    }
325}
326
327impl std::error::Error for ParseSourceError {}
328
329impl FromStr for Source {
330    type Err = ParseSourceError;
331    fn from_str(s: &str) -> Result<Self, Self::Err> {
332        let clean = s.to_lowercase().replace([' ', '_', '-'], "");
333        match clean.as_str() {
334            "combo1" | "mic1" => Ok(Self::Combo1),
335            "combo2" | "mic2" => Ok(Self::Combo2),
336            "combo3" | "mic3" => Ok(Self::Combo3),
337            "combo4" | "mic4" => Ok(Self::Combo4),
338            "combo12" | "combo1+2" => Ok(Self::Combo1_2),
339            "combo23" | "combo2+3" => Ok(Self::Combo2_3),
340            "combo34" | "combo3+4" => Ok(Self::Combo3_4),
341            "usb1" => Ok(Self::Usb1),
342            "chat" => Ok(Self::Chat),
343            "usb2" => Ok(Self::Usb2),
344            "bluetooth" | "bt" => Ok(Self::Bluetooth),
345            "soundpad" | "pad" => Ok(Self::SoundPad),
346            "virtualgame" | "game" | "vgame" => Ok(Self::VirtualGame),
347            "virtualmusic" | "music" | "vmusic" => Ok(Self::VirtualMusic),
348            "virtuala" | "va" | "a" => Ok(Self::VirtualA),
349            "virtualb" | "vb" | "b" => Ok(Self::VirtualB),
350            "callme1" | "cm1" | "caller1" => Ok(Self::CallMe1),
351            "callme2" | "cm2" | "caller2" => Ok(Self::CallMe2),
352            "callme3" | "cm3" | "caller3" => Ok(Self::CallMe3),
353            _ => Err(ParseSourceError(s.to_string())),
354        }
355    }
356}
357
358impl fmt::Display for Source {
359    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
360        f.write_str(match self {
361            Self::Combo1 => "combo1",
362            Self::Combo2 => "combo2",
363            Self::Combo3 => "combo3",
364            Self::Combo4 => "combo4",
365            Self::Combo1_2 => "combo1_2",
366            Self::Combo2_3 => "combo2_3",
367            Self::Combo3_4 => "combo3_4",
368            Self::Usb1 => "usb1",
369            Self::Chat => "chat",
370            Self::Usb2 => "usb2",
371            Self::Bluetooth => "bluetooth",
372            Self::SoundPad => "soundpad",
373            Self::VirtualGame => "game",
374            Self::VirtualMusic => "music",
375            Self::VirtualA => "virtuala",
376            Self::VirtualB => "virtualb",
377            Self::CallMe1 => "callme1",
378            Self::CallMe2 => "callme2",
379            Self::CallMe3 => "callme3",
380        })
381    }
382}
383
384/// A fader strip: a physical or virtual channel the user mixes. The strip <->
385/// wire-index map is **model-dependent**, so [`Fader::to_index`] and
386/// [`Fader::from_index`] both take a [`DeviceModel`]:
387///
388/// - Pro II: `Physical1..=Physical6` => 0..=5, `Virtual1..=Virtual3` => 6..=8.
389/// - Duo: `Physical1..=Physical4` => 0..=3, `Virtual1..=Virtual5` => 4..=8.
390///
391/// A variant absent on a model (e.g. `Physical6` on the Duo) has no index
392/// there; the conversions return `None`.
393#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
394pub enum Fader {
395    Physical1,
396    Physical2,
397    Physical3,
398    Physical4,
399    Physical5,
400    Physical6,
401    Virtual1,
402    Virtual2,
403    Virtual3,
404    Virtual4,
405    Virtual5,
406}
407
408impl Fader {
409    /// The wire fader/channel index for this strip on `model`, or `None` if the
410    /// strip does not exist on that model.
411    pub fn to_index(self, model: DeviceModel) -> Option<u8> {
412        use DeviceModel::{Duo, Pro2};
413        use Fader::*;
414        let idx = match (model, self) {
415            (Pro2, Physical1) => 0,
416            (Pro2, Physical2) => 1,
417            (Pro2, Physical3) => 2,
418            (Pro2, Physical4) => 3,
419            (Pro2, Physical5) => 4,
420            (Pro2, Physical6) => 5,
421            (Pro2, Virtual1) => 6,
422            (Pro2, Virtual2) => 7,
423            (Pro2, Virtual3) => 8,
424            (Pro2, Virtual4 | Virtual5) => return None,
425
426            (Duo, Physical1) => 0,
427            (Duo, Physical2) => 1,
428            (Duo, Physical3) => 2,
429            (Duo, Physical4) => 3,
430            (Duo, Physical5 | Physical6) => return None,
431            (Duo, Virtual1) => 4,
432            (Duo, Virtual2) => 5,
433            (Duo, Virtual3) => 6,
434            (Duo, Virtual4) => 7,
435            (Duo, Virtual5) => 8,
436        };
437        Some(idx)
438    }
439
440    /// Inverse of [`Fader::to_index`]: the strip at wire index `idx` on `model`,
441    /// or `None` if no strip maps there (e.g. index 9, the master channel).
442    pub fn from_index(model: DeviceModel, idx: u8) -> Option<Self> {
443        use DeviceModel::{Duo, Pro2};
444        use Fader::*;
445        Some(match (model, idx) {
446            (Pro2, 0) => Physical1,
447            (Pro2, 1) => Physical2,
448            (Pro2, 2) => Physical3,
449            (Pro2, 3) => Physical4,
450            (Pro2, 4) => Physical5,
451            (Pro2, 5) => Physical6,
452            (Pro2, 6) => Virtual1,
453            (Pro2, 7) => Virtual2,
454            (Pro2, 8) => Virtual3,
455
456            (Duo, 0) => Physical1,
457            (Duo, 1) => Physical2,
458            (Duo, 2) => Physical3,
459            (Duo, 3) => Physical4,
460            (Duo, 4) => Virtual1,
461            (Duo, 5) => Virtual2,
462            (Duo, 6) => Virtual3,
463            (Duo, 7) => Virtual4,
464            (Duo, 8) => Virtual5,
465
466            _ => return None,
467        })
468    }
469
470    /// `true` if this strip corresponds to a physical analog slide potentiometer
471    /// on `model`, as opposed to a virtual fader strip adjusted in software.
472    pub fn is_physical(self, model: DeviceModel) -> bool {
473        use DeviceModel::{Duo, Pro2};
474        use Fader::*;
475        match model {
476            Pro2 => matches!(
477                self,
478                Physical1 | Physical2 | Physical3 | Physical4 | Physical5 | Physical6
479            ),
480            Duo => matches!(self, Physical1 | Physical2 | Physical3 | Physical4),
481        }
482    }
483
484    /// Human-readable label for UIs and logs.
485    pub fn label(self) -> &'static str {
486        match self {
487            Self::Physical1 => "Fader 1",
488            Self::Physical2 => "Fader 2",
489            Self::Physical3 => "Fader 3",
490            Self::Physical4 => "Fader 4",
491            Self::Physical5 => "Fader 5",
492            Self::Physical6 => "Fader 6",
493            Self::Virtual1 => "Virtual 1",
494            Self::Virtual2 => "Virtual 2",
495            Self::Virtual3 => "Virtual 3",
496            Self::Virtual4 => "Virtual 4",
497            Self::Virtual5 => "Virtual 5",
498        }
499    }
500}
501
502/// Error returned when parsing an invalid fader name.
503#[derive(Debug, Clone, PartialEq, Eq)]
504pub struct ParseFaderError(pub String);
505
506impl fmt::Display for ParseFaderError {
507    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
508        write!(f, "unknown fader: {} (try: p1, fader1, v1)", self.0)
509    }
510}
511
512impl std::error::Error for ParseFaderError {}
513
514impl FromStr for Fader {
515    type Err = ParseFaderError;
516    fn from_str(s: &str) -> Result<Self, Self::Err> {
517        match s.to_lowercase().as_str() {
518            "physical1" | "p1" | "fader1" => Ok(Self::Physical1),
519            "physical2" | "p2" | "fader2" => Ok(Self::Physical2),
520            "physical3" | "p3" | "fader3" => Ok(Self::Physical3),
521            "physical4" | "p4" | "fader4" => Ok(Self::Physical4),
522            "physical5" | "p5" | "fader5" => Ok(Self::Physical5),
523            "physical6" | "p6" | "fader6" => Ok(Self::Physical6),
524            "virtual1" | "v1" | "vfader1" => Ok(Self::Virtual1),
525            "virtual2" | "v2" | "vfader2" => Ok(Self::Virtual2),
526            "virtual3" | "v3" | "vfader3" => Ok(Self::Virtual3),
527            "virtual4" | "v4" | "vfader4" => Ok(Self::Virtual4),
528            "virtual5" | "v5" | "vfader5" => Ok(Self::Virtual5),
529            _ => Err(ParseFaderError(s.to_string())),
530        }
531    }
532}
533
534impl fmt::Display for Fader {
535    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
536        f.write_str(match self {
537            Self::Physical1 => "physical1",
538            Self::Physical2 => "physical2",
539            Self::Physical3 => "physical3",
540            Self::Physical4 => "physical4",
541            Self::Physical5 => "physical5",
542            Self::Physical6 => "physical6",
543            Self::Virtual1 => "virtual1",
544            Self::Virtual2 => "virtual2",
545            Self::Virtual3 => "virtual3",
546            Self::Virtual4 => "virtual4",
547            Self::Virtual5 => "virtual5",
548        })
549    }
550}
551
552#[cfg(test)]
553mod tests {
554    use super::*;
555
556    #[test]
557    fn device_model_prefers_board_type_over_name() {
558        // boardType wins even if the name disagrees.
559        assert_eq!(
560            DeviceModel::detect(Some(1), Some("RODECaster Pro II")),
561            DeviceModel::Duo
562        );
563        assert_eq!(DeviceModel::detect(Some(0), None), DeviceModel::Pro2);
564    }
565
566    #[test]
567    fn device_model_falls_back_to_name_then_default() {
568        // Unrecognized board -> use the name.
569        assert_eq!(
570            DeviceModel::detect(Some(99), Some("rodecaster duo")),
571            DeviceModel::Duo
572        );
573        // No board, no name -> default Pro II (matches fw 1.6.8 captures).
574        assert_eq!(DeviceModel::detect(None, None), DeviceModel::Pro2);
575        // Name only.
576        assert_eq!(
577            DeviceModel::detect(None, Some("Some Duo")),
578            DeviceModel::Duo
579        );
580        assert_eq!(DeviceModel::detect(None, Some("Pro II")), DeviceModel::Pro2);
581    }
582
583    #[test]
584    fn source_protocol_roundtrips_all_19() {
585        for i in 0..=18u8 {
586            let s = Source::from_protocol(i).expect("valid source index");
587            assert_eq!(s.to_protocol(), i);
588        }
589        assert_eq!(Source::from_protocol(19), None);
590    }
591
592    #[test]
593    fn mix_output_protocol_roundtrips_all_13() {
594        for i in 0..=12u8 {
595            let m = MixOutput::from_protocol(i).expect("valid mix index");
596            assert_eq!(m.to_protocol(), i);
597        }
598        assert_eq!(MixOutput::from_protocol(13), None);
599    }
600
601    #[test]
602    fn source_callme_classification() {
603        assert!(Source::CallMe1.is_callme());
604        assert!(Source::CallMe3.is_callme());
605        assert!(!Source::Combo1.is_callme());
606        assert!(!Source::VirtualB.is_callme());
607    }
608
609    #[test]
610    fn fader_index_roundtrips_per_model() {
611        // Every wire index 0..=8 maps to exactly one strip on each model and back.
612        for model in [DeviceModel::Pro2, DeviceModel::Duo] {
613            for idx in 0..=8u8 {
614                let f = Fader::from_index(model, idx).expect("strip at index");
615                assert_eq!(f.to_index(model), Some(idx), "{model:?} idx {idx}");
616            }
617            // Index 9 (master channel) has no fader on either model.
618            assert_eq!(Fader::from_index(model, 9), None);
619        }
620    }
621
622    #[test]
623    fn fader_layout_differs_by_model() {
624        // Pro II: 6 physical then 3 virtual.
625        assert_eq!(Fader::Physical6.to_index(DeviceModel::Pro2), Some(5));
626        assert_eq!(Fader::Virtual1.to_index(DeviceModel::Pro2), Some(6));
627        assert_eq!(Fader::Virtual3.to_index(DeviceModel::Pro2), Some(8));
628        // Pro II has no Virtual4/5.
629        assert_eq!(Fader::Virtual4.to_index(DeviceModel::Pro2), None);
630        assert_eq!(Fader::Virtual5.to_index(DeviceModel::Pro2), None);
631
632        // Duo: 4 physical then 5 virtual.
633        assert_eq!(Fader::Physical4.to_index(DeviceModel::Duo), Some(3));
634        assert_eq!(Fader::Virtual1.to_index(DeviceModel::Duo), Some(4));
635        assert_eq!(Fader::Virtual5.to_index(DeviceModel::Duo), Some(8));
636        // Duo has no Physical5/6.
637        assert_eq!(Fader::Physical5.to_index(DeviceModel::Duo), None);
638        assert_eq!(Fader::Physical6.to_index(DeviceModel::Duo), None);
639    }
640
641    #[test]
642    fn fader_is_physical_per_model() {
643        assert!(Fader::Physical6.is_physical(DeviceModel::Pro2));
644        assert!(!Fader::Virtual1.is_physical(DeviceModel::Pro2));
645        assert!(Fader::Physical4.is_physical(DeviceModel::Duo));
646        // Physical5/6 don't exist on the Duo, so they aren't hardware faders there.
647        assert!(!Fader::Physical5.is_physical(DeviceModel::Duo));
648    }
649
650    #[test]
651    fn display_roundtrips_through_fromstr() {
652        // Each canonical Display string parses back to the same variant.
653        let sources = [
654            Source::Combo1,
655            Source::Combo3_4,
656            Source::Bluetooth,
657            Source::VirtualGame,
658            Source::CallMe3,
659        ];
660        for s in sources {
661            assert_eq!(s.to_string().parse::<Source>(), Ok(s));
662        }
663        let mixes = [
664            MixOutput::Headphone1,
665            MixOutput::Speaker,
666            MixOutput::CallMe2,
667        ];
668        for m in mixes {
669            assert_eq!(m.to_string().parse::<MixOutput>(), Ok(m));
670        }
671        let faders = [
672            Fader::Physical1,
673            Fader::Physical6,
674            Fader::Virtual1,
675            Fader::Virtual5,
676        ];
677        for fd in faders {
678            assert_eq!(fd.to_string().parse::<Fader>(), Ok(fd));
679        }
680    }
681
682    #[test]
683    fn fromstr_aliases() {
684        assert_eq!("mic1".parse::<Source>(), Ok(Source::Combo1));
685        assert_eq!("combo12".parse::<Source>(), Ok(Source::Combo1_2));
686        assert_eq!("bt".parse::<Source>(), Ok(Source::Bluetooth));
687        assert_eq!("hp3".parse::<MixOutput>(), Ok(MixOutput::Headphone3));
688        assert_eq!("spk".parse::<MixOutput>(), Ok(MixOutput::Speaker));
689        assert_eq!("p1".parse::<Fader>(), Ok(Fader::Physical1));
690        assert_eq!("fader4".parse::<Fader>(), Ok(Fader::Physical4));
691        assert_eq!("v5".parse::<Fader>(), Ok(Fader::Virtual5));
692
693        let err_src = "nonsense".parse::<Source>().unwrap_err();
694        assert_eq!(err_src, ParseSourceError("nonsense".to_string()));
695        assert_eq!(
696            err_src.to_string(),
697            "unknown source: nonsense (try: combo1, bt, game, cm1)"
698        );
699
700        let err_mix = "nonsense".parse::<MixOutput>().unwrap_err();
701        assert_eq!(err_mix, ParseMixOutputError("nonsense".to_string()));
702        assert_eq!(
703            err_mix.to_string(),
704            "unknown mix output: nonsense (try: hp1, speaker, bt, cm1)"
705        );
706
707        let err_fader = "nonsense".parse::<Fader>().unwrap_err();
708        assert_eq!(err_fader, ParseFaderError("nonsense".to_string()));
709        assert_eq!(
710            err_fader.to_string(),
711            "unknown fader: nonsense (try: p1, fader1, v1)"
712        );
713    }
714}