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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#[cfg(feature = "v0_4_2")]
use crate::pw::PropertiesItem;
use {
	crate::{
		prelude::*,
		pw::{Properties, ToPipewirePropertyString},
	},
	libspa_sys::spa_dict,
	pipewire_sys::pw_properties,
};

impl Properties {
	pub fn new_clone(props: &Self) -> Properties {
		unsafe { Self::new_wrap_mut(props.to_pw_properties().as_ptr()) }
	}

	#[doc(alias = "wp_properties_new_copy")]
	pub unsafe fn new_copy(props: &pw_properties) -> Properties {
		from_glib_full(ffi::wp_properties_new_copy(props))
	}

	#[doc(alias = "wp_properties_new_copy_dict")]
	pub unsafe fn new_copy_dict(dict: &spa_dict) -> Properties {
		from_glib_full(ffi::wp_properties_new_copy_dict(dict))
	}

	#[doc(alias = "wp_properties_new_wrap")]
	pub unsafe fn new_wrap(props: *const pw_properties) -> Properties {
		from_glib_full(ffi::wp_properties_new_wrap(props))
	}

	#[doc(alias = "wp_properties_new_take")]
	pub unsafe fn new_wrap_mut(props: *mut pw_properties) -> Properties {
		from_glib_full(ffi::wp_properties_new_take(props))
	}

	#[doc(alias = "wp_properties_new_wrap_dict")]
	pub unsafe fn new_wrap_dict(dict: *const spa_dict) -> Properties {
		from_glib_full(ffi::wp_properties_new_wrap_dict(dict))
	}

	#[doc(alias = "wp_properties_add_from_dict")]
	pub unsafe fn add_from_dict(&self, dict: &spa_dict) -> usize {
		ffi::wp_properties_add_from_dict(self.to_glib_none().0, dict) as usize
	}

	#[doc(alias = "wp_properties_peek_dict")]
	pub fn peek_dict(&self) -> &spa_dict {
		unsafe { &*ffi::wp_properties_peek_dict(self.to_glib_none().0) }
	}

	#[doc(alias = "wp_properties_to_pw_properties")]
	pub fn to_pw_properties(&self) -> NonNull<pw_properties> {
		unsafe { NonNull::new_unchecked(ffi::wp_properties_to_pw_properties(self.to_glib_none().0)) }
	}

	#[doc(alias = "wp_properties_unref_and_take_pw_properties")]
	pub fn unref_and_take_pw_properties(self) -> NonNull<pw_properties> {
		unsafe { NonNull::new_unchecked(ffi::wp_properties_unref_and_take_pw_properties(self.to_glib_full())) }
	}

	#[doc(alias = "wp_properties_update_from_dict")]
	pub unsafe fn update_from_dict(&self, dict: &spa_dict) -> usize {
		ffi::wp_properties_update_from_dict(self.to_glib_none().0, dict) as usize
	}

	pub fn remove(&self, key: &str) -> bool {
		self.set(key, None) != 0
	}

	pub fn insert<V: ToPipewirePropertyString>(&self, key: &str, value: V) {
		self.set(key, Some(value.pipewire_string().as_ref()));
	}

	#[cfg(feature = "v0_4_2")]
	pub fn iter(&self) -> iter::Map<ValueIterator<PropertiesItem>, fn(PropertiesItem) -> (String, String)> {
		self.items().into_iter().map(PropertiesItem::into)
	}

	#[cfg(feature = "v0_4_2")]
	pub fn items(&self) -> IntoValueIterator<PropertiesItem> {
		IntoValueIterator::with_inner(self.items_iterator().unwrap())
	}

	#[cfg(feature = "v0_4_2")]
	pub fn keys(&self) -> impl Iterator<Item = String> {
		self.items().into_iter().map(|kv| kv.key_string())
	}

	#[cfg(feature = "v0_4_2")]
	pub fn values(&self) -> impl Iterator<Item = String> {
		self.items().into_iter().map(|kv| kv.value_string())
	}
}

#[cfg(feature = "v0_4_2")]
mod properties_item {
	use crate::PropertiesItem;

	impl PropertiesItem {
		pub fn key_string(&self) -> String {
			self.key().unwrap().into()
		}

		pub fn value_string(&self) -> String {
			self.value().unwrap().into()
		}

		pub fn key_value(&self) -> (String, String) {
			(self.key_string(), self.value_string())
		}
	}

	impl Into<(String, String)> for PropertiesItem {
		fn into(self) -> (String, String) {
			self.key_value()
		}
	}

	impl<'a> Into<(String, String)> for &'a PropertiesItem {
		fn into(self) -> (String, String) {
			self.key_value()
		}
	}
}

impl FromIterator<(String, String)> for Properties {
	fn from_iter<T: IntoIterator<Item = (String, String)>>(iter: T) -> Self {
		let mut props = Self::new();
		props.extend(iter);
		props
	}
}

impl<K: AsRef<str>, V: ToPipewirePropertyString> Extend<(K, V)> for Properties {
	fn extend<T: IntoIterator<Item = (K, V)>>(&mut self, iter: T) {
		for (k, v) in iter {
			self.insert(k.as_ref(), v)
		}
	}
}

#[cfg(feature = "v0_4_2")]
impl<'a> IntoIterator for &'a Properties {
	type Item = (String, String);
	type IntoIter = iter::Map<ValueIterator<PropertiesItem>, fn(PropertiesItem) -> Self::Item>;

	fn into_iter(self) -> Self::IntoIter {
		self.iter()
	}
}

#[cfg(feature = "v0_4_2")]
impl IntoIterator for Properties {
	type Item = (String, String);
	type IntoIter = iter::Map<ValueIterator<PropertiesItem>, fn(PropertiesItem) -> Self::Item>;

	fn into_iter(self) -> Self::IntoIter {
		self.iter()
	}
}

#[cfg(feature = "v0_4_2")]
impl Debug for Properties {
	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
		struct DebugProps<'a>(&'a Properties);
		impl Debug for DebugProps<'_> {
			fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
				f.debug_map().entries(self.0).finish()
			}
		}
		let props = DebugProps(self);
		f.debug_tuple("wp::Properties").field(&props).finish()
	}
}

#[cfg(not(feature = "v0_4_2"))]
impl Debug for Properties {
	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
		// TODO?
		f.debug_tuple("wp::Properties").finish()
	}
}

impl Default for Properties {
	fn default() -> Self {
		Self::new()
	}
}