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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*
 * SPDX-License-Identifier: MIT
 */
use std::fmt::Debug;
use std::io::Write;
use std::mem::size_of;
use std::ops::Rem;
use std::ptr::addr_of;
use std::slice;

use crate::spa::pod::choice::PodChoiceRef;
use crate::spa::pod::{PodError, PodRef, PodResult, PodValue, POD_ALIGN};
use crate::spa::type_::Type;
use crate::wrapper::RawWrapper;

/// Pod with known size.
pub trait SizedPod {
    /// Pod size including header.
    fn pod_size(&self) -> usize;
}

/// Marker trait for types with value that can be sent by value, not by reference.
pub trait PrimitiveValue {}

pub trait CloneTo {
    /// Write this pod as bytes slice into the `buffer`.
    ///
    /// # Note
    ///
    /// `buffer` must be aligned properly
    fn clone_to(&self, buffer: &mut impl Write) -> PodResult<()>;
}

pub trait PodHeader {
    fn pod_header(&self) -> &spa_sys::spa_pod;

    fn pod_type(&self) -> Type {
        Type::from_raw(self.pod_header().type_)
    }

    fn static_type() -> Type;
}

pub trait WritePod: PodRawValue + Sized {
    fn write_pod<W>(buffer: &mut W, value: &<Self as PodValue>::Value) -> PodResult<()>
    where
        W: std::io::Write + std::io::Seek;
}

pub trait WriteValue: PodRawValue {
    fn write_raw_value<W>(buffer: &mut W, value: &<Self as PodValue>::Value) -> PodResult<()>
    where
        W: std::io::Write + std::io::Seek;
}

/// Basic pod, trait is implemented automatically.
pub trait BasicTypePod
where
    Self: PodHeader,
    Self: RawWrapper,
    Self: Debug,
{
    /// Try to cast this pod to another pod type.
    fn cast<T>(&self) -> PodResult<&T>
    where
        T: BasicTypePod,
    {
        let target_type = T::static_type();
        let pod_type = self.pod_type();
        #[allow(clippy::if_same_then_else)]
        if target_type == PodRef::static_type() || target_type == pod_type {
            unsafe { Ok(self.cast_unchecked()) }
        } else if pod_type == Type::CHOICE {
            let choice: &PodChoiceRef = unsafe { self.cast_unchecked() };
            choice.body().child().cast()
        } else if target_type == Type::CHOICE
            && (pod_type == Type::BOOL
                || pod_type == Type::ID
                || pod_type == Type::INT
                || pod_type == Type::LONG
                || pod_type == Type::FLOAT
                || pod_type == Type::DOUBLE
                || pod_type == Type::RECTANGLE
                || pod_type == Type::FRACTION)
        {
            unsafe { Ok(self.cast_unchecked()) }
        } else {
            Err(PodError::WrongPodTypeToCast(target_type, pod_type))
        }
    }

    unsafe fn cast_unchecked<T>(&self) -> &T
    where
        T: BasicTypePod,
    {
        T::from_raw_ptr(addr_of!(*self) as *const _)
    }
}

/// Pod with the value that can be parsed.
/// The trait is restricted part of [PodValue].
pub trait PodRawValue: PodValue {
    /// Raw C-type value of the pod.
    /// This value with the size must be enough to parse pod.
    /// For example, we can't use [spa_sys::spa_pod_choice_body].child as `RawValue`,
    /// because the `type` and `flags` fields are missing
    type RawValue;

    /// Pointer to the raw value.
    fn raw_value_ptr(&self) -> *const Self::RawValue;

    /// Parse the [Self::Value] from the [Self::RawValue].
    fn parse_raw_value(ptr: *const Self::RawValue, size: usize) -> PodResult<Self::Value>;
}

/// Check whether `buffer` position is properly aligned.
pub fn check_align<W>(buffer: &mut W) -> PodResult<()>
where
    W: std::io::Write + std::io::Seek,
{
    if buffer.stream_position()?.rem(POD_ALIGN as u64) == 0 {
        Ok(())
    } else {
        Err(PodError::PodIsNotAligned)
    }
}

/// Write [spa_sys::spa_pod] with the given values.
/// Align is checked before writing to be sure that `buffer` position is properly aligned.
pub fn write_header<W>(buffer: &mut W, size: u32, type_: Type) -> PodResult<()>
where
    W: std::io::Write + std::io::Seek,
{
    check_align(buffer)?;
    write_value(
        buffer,
        &spa_sys::spa_pod {
            size,
            type_: type_.raw,
        },
    )
}

/// Write any [Sized] value to the `buffer`.
pub fn write_value<W, V>(buffer: &mut W, value: &V) -> PodResult<()>
where
    W: std::io::Write + std::io::Seek,
    V: Sized,
{
    let size = size_of::<V>();
    let ptr = value as *const V as *const u8;
    let slice = unsafe { slice::from_raw_parts(ptr, size) };
    buffer.write_all(slice)?;
    Ok(())
}

/// Write the padding if it's required.
/// Should be used after each pod write for safety.
pub fn write_align_padding<W>(buffer: &mut W) -> PodResult<()>
where
    W: std::io::Write + std::io::Seek,
{
    let position = buffer.stream_position()? as usize;
    let rem = position.rem(POD_ALIGN);
    if rem > 0 {
        let padding_len = POD_ALIGN - rem;
        let padding = vec![0u8; padding_len];
        buffer.write_all(padding.as_slice())?;
    }
    Ok(())
}

/// Count the difference between `buffer` positions before and after usage in `func`.
pub fn write_count_size<W, F>(buffer: &mut W, func: F) -> PodResult<usize>
where
    W: std::io::Write + std::io::Seek,
    F: FnOnce(&mut W) -> PodResult<()>,
{
    let start_pos = buffer.stream_position()?;
    func(buffer)?;
    let end_pos = buffer.stream_position()?;
    Ok((end_pos - start_pos) as usize)
}