1use std::ptr::addr_of;
5
6use crate::{constants::ID_INVALID, pod::SpaTypes};
7
8#[derive(Debug)]
9pub enum CommandError {
10 WrongCommandType,
11}
12
13impl std::fmt::Display for CommandError {
14 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
15 match self {
16 Self::WrongCommandType => f.write_str("Wrong command type"),
17 }
18 }
19}
20
21impl std::error::Error for CommandError {}
22
23#[repr(transparent)]
24pub struct Command(spa_sys::spa_command);
25
26impl Command {
27 pub fn into_raw(self) -> spa_sys::spa_command {
28 self.0
29 }
30
31 pub fn from_raw(raw: spa_sys::spa_command) -> Self {
32 Self(raw)
33 }
34
35 pub fn as_raw_ptr(&self) -> *mut spa_sys::spa_command {
36 addr_of!(self.0).cast_mut()
37 }
38
39 pub fn type_(&self) -> SpaTypes {
40 unsafe { SpaTypes::from_raw(spa_sys::spa_command_type(self.as_raw_ptr())) }
41 }
42
43 pub fn id(&self, type_: SpaTypes) -> Result<u32, CommandError> {
44 let id = unsafe { spa_sys::spa_command_id(self.as_raw_ptr(), type_.as_raw()) };
45
46 if id == ID_INVALID {
47 Err(CommandError::WrongCommandType)
48 } else {
49 Ok(id)
50 }
51 }
52
53 pub fn init(type_: SpaTypes, id: u32) -> Self {
54 Self(unsafe { spa_sys::spa_command_init(type_.as_raw(), id) })
55 }
56}