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::object::{PodPropKeyType, PodPropRef};
use crate::spa::pod::struct_::PodStructRef;
use crate::spa::pod::{BasicTypePod, PodError, PodResult};
use crate::wrapper::RawWrapper;

#[repr(u32)]
#[derive(Debug)]
#[allow(non_camel_case_types)]
#[object_type_impl(OBJECT_PROFILER)]
pub enum ProfilerType<'a> {
    INFO(&'a PodStructRef) = Profiler::INFO.raw,
    CLOCK(&'a PodStructRef) = Profiler::CLOCK.raw,
    DRIVER_BLOCK(&'a PodStructRef) = Profiler::DRIVER_BLOCK.raw,
    FOLLOWER_BLOCK(&'a PodStructRef) = Profiler::FOLLOWER_BLOCK.raw,
}

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

    fn try_from(value: &'a PodPropRef<'a, ProfilerType<'a>>) -> Result<Self, Self::Error> {
        unsafe {
            match Profiler::from_raw(value.raw.key) {
                Profiler::INFO => Ok(ProfilerType::INFO(value.pod().cast()?)),
                Profiler::CLOCK => Ok(ProfilerType::CLOCK(value.pod().cast()?)),
                Profiler::DRIVER_BLOCK => Ok(ProfilerType::DRIVER_BLOCK(value.pod().cast()?)),
                Profiler::FOLLOWER_BLOCK => Ok(ProfilerType::FOLLOWER_BLOCK(value.pod().cast()?)),
                _ => Err(PodError::UnknownPodTypeToDowncast),
            }
        }
    }
}

impl<'a> PodPropKeyType<'a> for ProfilerType<'a> {
    fn write_prop<W>(&self, buffer: &mut W) -> PodResult<()>
    where
        W: Write + Seek,
    {
        match self {
            ProfilerType::INFO(pod) => Self::write_pod_prop(buffer, Profiler::INFO.raw, 0, pod),
            ProfilerType::CLOCK(pod) => Self::write_pod_prop(buffer, Profiler::CLOCK.raw, 0, pod),
            ProfilerType::DRIVER_BLOCK(pod) => {
                Self::write_pod_prop(buffer, Profiler::DRIVER_BLOCK.raw, 0, pod)
            }
            ProfilerType::FOLLOWER_BLOCK(pod) => {
                Self::write_pod_prop(buffer, Profiler::FOLLOWER_BLOCK.raw, 0, pod)
            }
        }
    }
}

enum_wrapper!(
    Profiler,
    spa_sys::spa_profiler,
    _START: spa_sys::SPA_PROFILER_START,
    _START_DRIVER: spa_sys::SPA_PROFILER_START_Driver,
    INFO: spa_sys::SPA_PROFILER_info,
    CLOCK: spa_sys::SPA_PROFILER_clock,
    DRIVER_BLOCK: spa_sys::SPA_PROFILER_driverBlock,
    _START_FOLLOWER: spa_sys::SPA_PROFILER_START_Follower,
    FOLLOWER_BLOCK: spa_sys::SPA_PROFILER_followerBlock,
    _START_CUSTOM: spa_sys::SPA_PROFILER_START_CUSTOM,
);