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
use std::{ffi::CStr, fmt::Display};

use crate::{api, ffi};

pub type VideoFormat = ffi::VSVideoFormat;
pub type AudioFormat = ffi::VSAudioFormat;

struct FormatName {
    buffer: [u8; 32],
}

impl FormatName {
    fn new() -> Self {
        Self { buffer: [0; 32] }
    }
}

impl Display for FormatName {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let cstr = unsafe { CStr::from_bytes_until_nul(&self.buffer).unwrap_unchecked() };
        cstr.to_str()
            .map_err(|_| std::fmt::Error)
            .and_then(|s| f.write_str(s))
    }
}

pub trait FormatExt {
    fn name(&self) -> Option<String>;
}

impl FormatExt for VideoFormat {
    fn name(&self) -> Option<String> {
        let mut buffer = FormatName::new();
        if 0 == unsafe { (api().getVideoFormatName)(self, buffer.buffer.as_mut_ptr().cast()) } {
            None
        } else {
            Some(buffer.to_string())
        }
    }
}

impl FormatExt for AudioFormat {
    fn name(&self) -> Option<String> {
        let mut buffer = FormatName::new();
        if 0 == unsafe { (api().getAudioFormatName)(self, buffer.buffer.as_mut_ptr().cast()) } {
            None
        } else {
            Some(buffer.to_string())
        }
    }
}