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
/*
 * SPDX-License-Identifier: MIT
 */
use core::slice;
use std::collections::HashMap;
use std::ffi::{CStr, CString};
use std::fmt::{Debug, Formatter};
use std::ops::Deref;
use std::ptr::{null, null_mut, slice_from_raw_parts, NonNull};
use std::slice::Iter;

use bitflags::bitflags;
use spa_sys::{spa_dict, spa_dict_item};

use pipewire_wrapper_proc_macro::{RawWrapper, Wrapper};

use crate::wrapper::{RawWrapper, Wrapper};

#[derive(RawWrapper)]
#[repr(transparent)]
pub struct DictRef {
    #[raw]
    raw: spa_sys::spa_dict,
}

pub type DictRefIterator<'a> = std::slice::Iter<'a, DictItemRef>;

impl DictRef {
    pub fn flags(&self) -> Flags {
        Flags::from_bits_retain(self.raw.flags)
    }

    pub fn n_items(&self) -> u32 {
        self.raw.n_items
    }

    pub fn items(&self) -> &[DictItemRef] {
        unsafe {
            slice::from_raw_parts(
                self.raw.items as *const DictItemRef,
                self.raw.n_items as usize,
            )
        }
    }

    pub fn iter(&self) -> DictRefIterator {
        self.items().iter()
    }

    pub(crate) unsafe fn from_items(items: &[DictItemRef], flags: Flags) -> Self {
        Self {
            raw: spa_dict {
                flags: flags.bits(),
                n_items: items.len() as u32,
                items: items.as_ptr().cast(),
            },
        }
    }
}

#[derive(RawWrapper)]
#[repr(transparent)]
pub struct DictItemRef {
    #[raw]
    raw: spa_sys::spa_dict_item,
}

impl DictItemRef {
    pub fn key(&self) -> &CStr {
        unsafe { CStr::from_ptr(self.raw.key) }
    }

    pub fn value(&self) -> &CStr {
        unsafe { CStr::from_ptr(self.raw.value) }
    }

    pub(crate) unsafe fn from_tuple<'a>(value: &'a (&'a CStr, &'a CStr)) -> Self
    where
        Self: 'a,
    {
        DictItemRef::from_raw(spa_dict_item {
            key: value.0.as_ptr(),
            value: value.1.as_ptr(),
        })
    }
}

impl Debug for DictItemRef {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("DictItemRef")
            .field("key", &self.key())
            .field("value", &self.value())
            .finish()
    }
}

bitflags! {
    #[derive(Debug, PartialEq, Eq, Clone, Copy)]
    #[repr(transparent)]
    pub struct Flags: u32 {
        const SORTED = spa_sys::SPA_DICT_FLAG_SORTED;
    }
}

impl From<&DictRef> for HashMap<CString, CString> {
    fn from(value: &DictRef) -> Self {
        HashMap::from_iter(
            value
                .iter()
                .map(|p| (CString::from(p.key()), CString::from(p.value()))),
        )
    }
}

impl Debug for DictRef {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        f.debug_map()
            .entries(self.iter().map(|item| (item.key(), item.value())))
            .finish()
    }
}