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
use crate::{
	prelude::*,
	spa::{SpaPod, SpaPodParser},
};

impl SpaPodParser {
	#[doc(alias = "wp_spa_pod_parser_new_object")]
	pub fn new_object(pod: &SpaPod) -> (Self, Option<&'static str>) {
		unsafe {
			// TODO: this needs a lifetime to attach the parser's lifetime to `pod`
			// TODO: this can return back `wp_spa_id_value_short_name(wp_spa_id_table_find_value (table, id))`
			// via second parameter
			let mut id_name = ptr::null();
			let res = from_glib_full(ffi::wp_spa_pod_parser_new_object(pod.to_glib_none().0, &mut id_name));
			let id_name = match id_name {
				id_name if id_name.is_null() => None,
				id_name => Some(CStr::from_ptr(id_name)),
			};
			let id_name = id_name.and_then(|id_name| match id_name.to_str() {
				Ok(str) => Some(str),
				Err(e) => {
					wp_warning!("failed to parse spa pod ID name as UTF-8: {id_name:?}");
					None
				},
			});
			(res, id_name)
		}
	}

	#[doc(alias = "wp_spa_pod_parser_get_bytes")]
	#[doc(alias = "get_bytes")]
	pub fn bytes(&self) -> Option<&[u8]> {
		let mut data = ptr::null();
		let mut len = 0;
		unsafe {
			if from_glib(ffi::wp_spa_pod_parser_get_bytes(
				self.to_glib_none().0,
				&mut data,
				&mut len,
			)) {
				Some(slice::from_raw_parts(data as *const u8, len as usize))
			} else {
				None
			}
		}
	}

	#[doc(alias = "wp_spa_pod_parser_get_pointer")]
	#[doc(alias = "get_pointer")]
	pub fn pointer(&self) -> Option<gconstpointer> {
		let mut data = ptr::null();
		unsafe {
			if from_glib(ffi::wp_spa_pod_parser_get_pointer(self.to_glib_none().0, &mut data)) {
				Some(data)
			} else {
				None
			}
		}
	}
}