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
use crate::{
	prelude::*,
	pw::{self, PipewireObject, Port},
};

impl Port {
	pub fn node_id(&self) -> Result<u32, Error> {
		self.pw_property(pw::PW_KEY_NODE_ID)
	}

	#[doc(alias = "port_id")]
	pub fn port_index(&self) -> Result<u32, Error> {
		self.pw_property(pw::PW_KEY_PORT_ID)
	}
}

impl Display for Port {
	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
		if let Some(res) = self.with_pw_property(pw::PW_KEY_PORT_ALIAS, |name| f.write_str(name)) {
			return res
		}

		if let Ok(node) = self.node_id() {
			write!(f, "{node}")?;
		}
		if let Ok(index) = self.port_index() {
			write!(f, ".{index}")?;
		}

		if let Some(res) = self.with_pw_property(pw::PW_KEY_PORT_NAME, |name| write!(f, ":{name}")) {
			return res
		}

		write!(f, "pw.port({})", AsRef::<PipewireObject>::as_ref(self))
	}
}