Skip to main content

brainwires_audio/
device.rs

1use serde::{Deserialize, Serialize};
2
3/// Represents an audio device (input or output).
4#[derive(Debug, Clone, Serialize, Deserialize)]
5pub struct AudioDevice {
6    /// Unique device identifier (platform-specific).
7    pub id: String,
8    /// Human-readable device name.
9    pub name: String,
10    /// Whether this is the system default device.
11    pub is_default: bool,
12    /// Device direction.
13    pub direction: DeviceDirection,
14}
15
16/// Whether a device is for input (capture) or output (playback).
17#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
18pub enum DeviceDirection {
19    /// Capture / microphone input.
20    Input,
21    /// Playback / speaker output.
22    Output,
23}
24
25#[cfg(test)]
26mod tests {
27    use super::*;
28
29    #[test]
30    fn audio_device_creation_and_field_access() {
31        let dev = AudioDevice {
32            id: "hw:0".to_string(),
33            name: "Built-in Microphone".to_string(),
34            is_default: true,
35            direction: DeviceDirection::Input,
36        };
37        assert_eq!(dev.id, "hw:0");
38        assert_eq!(dev.name, "Built-in Microphone");
39        assert!(dev.is_default);
40        assert_eq!(dev.direction, DeviceDirection::Input);
41    }
42
43    #[test]
44    fn audio_device_non_default_output() {
45        let dev = AudioDevice {
46            id: "hw:1".to_string(),
47            name: "HDMI Output".to_string(),
48            is_default: false,
49            direction: DeviceDirection::Output,
50        };
51        assert!(!dev.is_default);
52        assert_eq!(dev.direction, DeviceDirection::Output);
53    }
54
55    #[test]
56    fn device_direction_variants_are_distinct() {
57        assert_ne!(DeviceDirection::Input, DeviceDirection::Output);
58    }
59}