1use std::ops::Deref;
8
9#[cfg(any(feature = "link-vs", feature = "link-vsscript"))]
10use vapoursynth4_sys::vs_make_version;
11
12use crate::ffi;
13
14#[cfg(any(feature = "link-vs", feature = "link-vsscript"))]
15use self::error::ApiNotFound;
16
17#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
18#[repr(transparent)]
19pub struct Api(*const ffi::VSAPI);
20
21impl Api {
22 #[cfg(feature = "link-vs")]
28 pub fn new(major: u16, minor: u16) -> Result<Self, ApiNotFound> {
29 let ptr = unsafe { ffi::getVapourSynthAPI(vs_make_version(major, minor)) };
30 if ptr.is_null() {
31 Err(ApiNotFound { major, minor })
32 } else {
33 Ok(Self(ptr))
34 }
35 }
36
37 #[cfg(feature = "vulkan")]
39 #[must_use]
40 pub fn vulkan(&self) -> &ffi::VSVULKANAPI {
41 unsafe { &*(self.getVulkanAPI)() }
42 }
43
44 pub(crate) unsafe fn from_ptr(ptr: *const ffi::VSAPI) -> Self {
45 Self(ptr)
46 }
47}
48
49impl Deref for Api {
50 type Target = ffi::VSAPI;
51
52 fn deref(&self) -> &Self::Target {
53 unsafe { &*self.0 }
54 }
55}
56
57#[cfg(feature = "link-vs")]
58impl Default for Api {
59 fn default() -> Self {
65 Self::new(ffi::VAPOURSYNTH_API_MAJOR, ffi::VAPOURSYNTH_API_MINOR).unwrap()
66 }
67}
68
69#[cfg(feature = "vsscript")]
70#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
71#[repr(transparent)]
72pub struct VssApi(*const ffi::VSSCRIPTAPI);
73
74#[cfg(feature = "vsscript")]
75impl VssApi {
76 #[cfg(feature = "link-vsscript")]
82 pub fn new(major: u16, minor: u16) -> Result<Self, ApiNotFound> {
83 let ptr = unsafe { ffi::getVSScriptAPI(vs_make_version(major, minor)) };
84 (!ptr.is_null())
85 .then_some(Self(ptr))
86 .ok_or(ApiNotFound { major, minor })
87 }
88
89 #[allow(unused)]
90 pub(crate) unsafe fn from_ptr(ptr: *const ffi::VSSCRIPTAPI) -> Self {
91 Self(ptr.cast_mut())
92 }
93
94 #[cfg(all(feature = "link-vsscript", feature = "vsscript-43"))]
103 #[must_use]
104 pub fn last_error() -> Option<std::ffi::CString> {
105 let ptr = unsafe { ffi::getVSScriptAPILastError() };
106 (!ptr.is_null()).then(|| unsafe { std::ffi::CStr::from_ptr(ptr) }.to_owned())
107 }
108}
109
110#[cfg(feature = "vsscript")]
111impl Deref for VssApi {
112 type Target = ffi::VSSCRIPTAPI;
113
114 fn deref(&self) -> &Self::Target {
115 unsafe { &*self.0 }
116 }
117}
118
119#[cfg(feature = "link-vsscript")]
120impl Default for VssApi {
121 fn default() -> Self {
127 Self::new(ffi::VSSCRIPT_API_MAJOR, ffi::VSSCRIPT_API_MINOR).unwrap()
128 }
129}
130
131pub mod error {
132 use thiserror::Error;
133
134 #[derive(Error, Debug, Clone, Copy, PartialEq, Eq, Hash)]
135 #[error(
136 "Request API with version {major}.{minor} failed. \
137 Please check if the version is supported by the linked VapourSynth library."
138 )]
139 pub struct ApiNotFound {
140 pub major: u16,
141 pub minor: u16,
142 }
143}
144
145#[cfg(all(test, feature = "link-vs"))]
146mod tests {
147 use super::*;
148
149 #[test]
150 fn rejects_unsupported_core_api_versions() {
151 let error = Api::new(99, 0).unwrap_err();
152 assert_eq!(
153 error,
154 ApiNotFound {
155 major: 99,
156 minor: 0
157 }
158 );
159 assert_eq!(
160 Api::new(ffi::VAPOURSYNTH_API_MAJOR, ffi::VAPOURSYNTH_API_MINOR).unwrap(),
161 Api::default()
162 );
163 }
164}