libspa/utils/
direction.rs

1// Copyright The pipewire-rs Contributors.
2// SPDX-License-Identifier: MIT
3
4//! SPA direction.
5
6#[derive(Copy, Clone, PartialEq, Eq)]
7pub struct Direction(spa_sys::spa_direction);
8
9#[allow(non_upper_case_globals)]
10impl Direction {
11    pub const Input: Self = Self(spa_sys::SPA_DIRECTION_INPUT);
12    pub const Output: Self = Self(spa_sys::SPA_DIRECTION_OUTPUT);
13
14    pub fn from_raw(raw: spa_sys::spa_direction) -> Self {
15        Self(raw)
16    }
17
18    pub fn as_raw(&self) -> spa_sys::spa_direction {
19        self.0
20    }
21
22    /// Return a new [`Direction`] in the opposite direction, turning Input to Output, and Output to Input.
23    ///
24    /// An unknown/invalid direction is unchanged.
25    pub fn reverse(&self) -> Self {
26        match *self {
27            Self::Input => Self::Output,
28            Self::Output => Self::Input,
29            _ => *self,
30        }
31    }
32}
33
34impl std::fmt::Debug for Direction {
35    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
36        let name = format!(
37            "Direction::{}",
38            match *self {
39                Self::Input => "Input",
40                Self::Output => "Output",
41                _ => "Unknown",
42            }
43        );
44        f.write_str(&name)
45    }
46}
47
48#[cfg(test)]
49mod tests {
50    use super::*;
51
52    #[test]
53    fn as_raw() {
54        assert_eq!(Direction::Input.as_raw(), spa_sys::SPA_DIRECTION_INPUT);
55        assert_eq!(Direction::Output.as_raw(), spa_sys::SPA_DIRECTION_OUTPUT);
56    }
57
58    #[test]
59    fn from_raw() {
60        assert_eq!(
61            Direction::Input,
62            Direction::from_raw(spa_sys::SPA_DIRECTION_INPUT)
63        );
64        assert_eq!(
65            Direction::Output,
66            Direction::from_raw(spa_sys::SPA_DIRECTION_OUTPUT)
67        );
68    }
69
70    #[test]
71    fn reverse() {
72        assert_eq!(Direction::Output.reverse(), Direction::Input);
73        assert_eq!(Direction::Input.reverse(), Direction::Output);
74    }
75}