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
/*
 * SPDX-License-Identifier: MIT
 */
use std::io::{Seek, Write};

use pipewire_wrapper_proc_macro::object_type_impl;

use crate::enum_wrapper;
use crate::spa::pod::id::{PodIdRef, PodIdType};
use crate::spa::pod::object::{PodPropKeyType, PodPropRef};
use crate::spa::pod::{BasicTypePod, PodError, PodIntRef, PodResult};
use crate::wrapper::RawWrapper;

#[repr(u32)]
#[derive(Debug)]
#[object_type_impl(OBJECT_PARAM_IO)]
pub enum ParamIOType<'a> {
    ID(&'a PodIdRef<IOType>) = ParamIO::ID.raw,
    SIZE(&'a PodIntRef) = ParamIO::SIZE.raw,
}

impl<'a> TryFrom<&'a PodPropRef<'a, ParamIOType<'a>>> for ParamIOType<'a> {
    type Error = PodError;

    fn try_from(value: &'a PodPropRef<'a, ParamIOType<'a>>) -> Result<Self, Self::Error> {
        unsafe {
            match ParamIO::from_raw(value.raw.key) {
                ParamIO::ID => Ok(ParamIOType::ID(value.pod().cast()?)),
                ParamIO::SIZE => Ok(ParamIOType::SIZE(value.pod().cast()?)),
                _ => Err(PodError::UnknownPodTypeToDowncast),
            }
        }
    }
}

impl<'a> PodPropKeyType<'a> for ParamIOType<'a> {
    fn write_prop<W>(&self, buffer: &mut W) -> PodResult<()>
    where
        W: Write + Seek,
    {
        match self {
            ParamIOType::ID(pod) => Self::write_pod_prop(buffer, ParamIO::ID.raw, 0, pod),
            ParamIOType::SIZE(pod) => Self::write_pod_prop(buffer, ParamIO::SIZE.raw, 0, pod),
        }
    }
}

enum_wrapper!(
    ParamIO,
    spa_sys::spa_param_io,
    _START: spa_sys::SPA_PARAM_IO_START,
    ID: spa_sys::SPA_PARAM_IO_id,
    SIZE: spa_sys::SPA_PARAM_IO_size,
);

enum_wrapper!(
    IOType,
    spa_sys::spa_io_type,
    INVALID: spa_sys::SPA_IO_Invalid,
    BUFFERS: spa_sys::SPA_IO_Buffers,
    RANGE: spa_sys::SPA_IO_Range,
    CLOCK: spa_sys::SPA_IO_Clock,
    LATENCY: spa_sys::SPA_IO_Latency,
    CONTROL: spa_sys::SPA_IO_Control,
    NOTIFY: spa_sys::SPA_IO_Notify,
    POSITION: spa_sys::SPA_IO_Position,
    RATEMATCH: spa_sys::SPA_IO_RateMatch,
    MEMORY: spa_sys::SPA_IO_Memory,
);
impl PodIdType for IOType {}