1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
use std::fmt;

// Macro generating the ObjectType enum
macro_rules! object_type {
    ($( ($x:ident, $version:ident) ),*) => {
        #[derive(Debug, Eq, PartialEq, Clone)]
        pub enum ObjectType {
            $($x,)*
            Other(String),
        }

        impl ObjectType {
            pub(crate) fn from_str(s: &str) -> ObjectType {
                match s {
                    $(
                    concat!("PipeWire:Interface:", stringify!($x)) => ObjectType::$x,
                    )*
                    s => ObjectType::Other(s.to_string()),
                }
            }

            pub fn to_str(&self) -> &str {
                match self {
                    $(
                        ObjectType::$x => concat!("PipeWire:Interface:", stringify!($x)),
                    )*
                    ObjectType::Other(s) => s,
                }
            }

            pub(crate) fn client_version(&self) -> u32 {
                match self {
                    $(
                        ObjectType::$x => pw_sys::$version,
                    )*
                    ObjectType::Other(_) => panic!("Invalid object type"),
                }
            }
        }

        impl fmt::Display for ObjectType {
            fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
                write!(f, "{}", self.to_str())
            }
        }
    };
}

object_type![
    // Id, API version
    (Client, PW_VERSION_CLIENT),
    (ClientEndpoint, PW_VERSION_CLIENT_ENDPOINT),
    (ClientNode, PW_VERSION_CLIENT_NODE),
    (ClientSession, PW_VERSION_CLIENT_SESSION),
    (Core, PW_VERSION_CORE),
    (Device, PW_VERSION_DEVICE),
    (Endpoint, PW_VERSION_ENDPOINT),
    (EndpointLink, PW_VERSION_ENDPOINT_LINK),
    (EndpointStream, PW_VERSION_ENDPOINT_STREAM),
    (Factory, PW_VERSION_FACTORY),
    (Link, PW_VERSION_LINK),
    (Metadata, PW_VERSION_METADATA),
    (Module, PW_VERSION_MODULE),
    (Node, PW_VERSION_NODE),
    (Port, PW_VERSION_PORT),
    (Profiler, PW_VERSION_PROFILER),
    (Registry, PW_VERSION_REGISTRY),
    (Session, PW_VERSION_SESSION)
];