1use std::ops::Deref;
8
9use vapoursynth4_sys::vs_make_version;
10
11use crate::ffi;
12
13use self::error::ApiNotFound;
14
15#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
16#[repr(transparent)]
17pub struct Api(*const ffi::VSAPI);
18
19impl Api {
20 #[cfg(feature = "link-vs")]
26 pub fn new(major: u16, minor: u16) -> Result<Self, ApiNotFound> {
27 let ptr = unsafe { ffi::getVapourSynthAPI(vs_make_version(major, minor)) };
28 if ptr.is_null() {
29 Err(ApiNotFound { major, minor })
30 } else {
31 Ok(Self(ptr))
32 }
33 }
34
35 pub(crate) unsafe fn from_ptr(ptr: *const ffi::VSAPI) -> Self {
36 Self(ptr)
37 }
38}
39
40impl Deref for Api {
41 type Target = ffi::VSAPI;
42
43 fn deref(&self) -> &Self::Target {
44 unsafe { &*self.0 }
45 }
46}
47
48#[cfg(feature = "link-vs")]
49impl Default for Api {
50 fn default() -> Self {
56 Self::new(ffi::VAPOURSYNTH_API_MAJOR, ffi::VAPOURSYNTH_API_MINOR).unwrap()
57 }
58}
59
60#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
61#[repr(transparent)]
62pub struct VssApi(*const ffi::VSSCRIPTAPI);
63
64impl VssApi {
65 #[cfg(feature = "link-vsscript")]
71 pub fn new(major: u16, minor: u16) -> Result<Self, ApiNotFound> {
72 let ptr = unsafe { ffi::getVSScriptAPI(vs_make_version(major, minor)) };
73 (!ptr.is_null())
74 .then_some(Self(ptr))
75 .ok_or(ApiNotFound { major, minor })
76 }
77
78 #[allow(unused)]
79 pub(crate) unsafe fn from_ptr(ptr: *const ffi::VSSCRIPTAPI) -> Self {
80 Self(ptr.cast_mut())
81 }
82
83 #[cfg(all(feature = "link-vsscript", feature = "vsscript-43"))]
92 #[must_use]
93 pub fn last_error() -> Option<std::ffi::CString> {
94 let ptr = unsafe { ffi::getVSScriptAPILastError() };
95 (!ptr.is_null()).then(|| unsafe { std::ffi::CStr::from_ptr(ptr) }.to_owned())
96 }
97}
98
99impl Deref for VssApi {
100 type Target = ffi::VSSCRIPTAPI;
101
102 fn deref(&self) -> &Self::Target {
103 unsafe { &*self.0 }
104 }
105}
106
107#[cfg(feature = "link-vsscript")]
108impl Default for VssApi {
109 fn default() -> Self {
115 Self::new(ffi::VSSCRIPT_API_MAJOR, ffi::VSSCRIPT_API_MINOR).unwrap()
116 }
117}
118
119pub mod error {
120 use thiserror::Error;
121
122 #[derive(Error, Debug, Clone, Copy, PartialEq, Eq, Hash)]
123 #[error(
124 "Request API with version {major}.{minor} failed. \
125 Please check if the version is supported by the linked VapourSynth library."
126 )]
127 pub struct ApiNotFound {
128 pub major: u16,
129 pub minor: u16,
130 }
131}