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
/*
 * SPDX-License-Identifier: MIT
 */
use std::fmt::{Debug, Formatter};
use std::io::{Seek, Write};
use std::marker::PhantomData;
use std::mem::size_of;

use spa_sys::spa_pod;

use pipewire_wrapper_proc_macro::RawWrapper;

use crate::spa::pod::pod_buf::{AllocPod, PodBuf};
use crate::spa::pod::restricted::{PodHeader, PodRawValue, PrimitiveValue};
use crate::spa::pod::{FromValue, PodResult, PodValue, SizedPod, WritePod, WriteValue, POD_ALIGN};
use crate::spa::type_::Type;
use crate::wrapper::RawWrapper;

use super::restricted::{write_align_padding, write_header, write_value};

#[derive(RawWrapper)]
#[repr(transparent)]
pub struct PodIdRef<T: PodIdType = u32> {
    #[raw]
    raw: spa_sys::spa_pod_id,
    phantom: PhantomData<T>,
}

pub trait PodIdType
where
    Self: From<u32>,
    Self: Into<u32>,
    Self: Debug,
    Self: Clone,
{
    fn to_alloc_pod(&self) -> AllocPod<PodIdRef<Self>> {
        PodIdRef::from_value(self).unwrap()
    }
}

impl PodIdType for u32 {}

impl PodIdType for Type {}

impl<T: PodIdType> PodRawValue for PodIdRef<T>
where
    T: PodIdType,
{
    type RawValue = u32;

    fn raw_value_ptr(&self) -> *const Self::RawValue {
        &self.raw.value
    }

    fn parse_raw_value(ptr: *const Self::RawValue, size: usize) -> PodResult<Self::Value> {
        unsafe { Ok((*ptr).into()) }
    }
}

impl<T: PodIdType> PodValue for PodIdRef<T>
where
    T: PodIdType,
{
    type Value = T;
    fn value(&self) -> PodResult<Self::Value> {
        Self::parse_raw_value(self.raw_value_ptr(), self.pod_header().size as usize)
    }
}

impl<T: PodIdType> PrimitiveValue for PodIdRef<T> {}

impl<T: PodIdType> WritePod for PodIdRef<T>
where
    T: PodIdType,
{
    fn write_pod<W>(buffer: &mut W, value: &<Self as PodValue>::Value) -> PodResult<()>
    where
        W: Write + Seek,
    {
        write_header(
            buffer,
            size_of::<u32>() as u32,
            <PodIdRef<T>>::static_type(),
        )?;
        Self::write_raw_value(buffer, value)?;
        write_align_padding(buffer)?;
        Ok(())
    }
}

impl<T: PodIdType> WriteValue for PodIdRef<T>
where
    T: PodIdType,
{
    fn write_raw_value<W>(buffer: &mut W, value: &<Self as PodValue>::Value) -> PodResult<()>
    where
        W: Write + Seek,
    {
        let raw_value: u32 = value.clone().into();
        write_value(buffer, &raw_value)
    }
}

impl<T: PodIdType> PodHeader for PodIdRef<T> {
    fn pod_header(&self) -> &spa_pod {
        &self.raw.pod
    }

    fn static_type() -> Type {
        Type::ID
    }
}

impl<T: PodIdType> Debug for PodIdRef<T> {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        unsafe {
            f.debug_struct("PodIdType")
                .field("pod.type", &self.pod_type())
                .field("pod.size", &self.pod_size())
                .field("value", &self.value())
                .finish()
        }
    }
}

#[test]
fn test_from_value() {
    let allocated_pod = PodBuf::<PodIdRef<Type>>::from_value(&Type::POINTER)
        .unwrap()
        .into_pod();
    assert_eq!(allocated_pod.as_pod().as_ptr().align_offset(POD_ALIGN), 0);
    assert_eq!(allocated_pod.as_pod().pod_size(), 12);
    assert_eq!(allocated_pod.as_pod().pod_header().size, 4);
    assert_eq!(allocated_pod.as_pod().pod_header().type_, Type::ID.raw);
    assert_eq!(allocated_pod.as_pod().value().unwrap(), Type::POINTER);
}