vapoursynth4_rs/
api.rs

1/*
2 This Source Code Form is subject to the terms of the Mozilla Public
3 License, v. 2.0. If a copy of the MPL was not distributed with this
4 file, You can obtain one at http://mozilla.org/MPL/2.0/.
5*/
6
7use std::ops::Deref;
8
9#[cfg(feature = "link-library")]
10use vapoursynth4_sys::vs_make_version;
11
12use crate::ffi;
13
14#[cfg(feature = "link-library")]
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    /// Creates a new `Api` instance with the specified major and minor version.
23    ///
24    /// # Errors
25    ///
26    /// Returns `ApiNotFound` if the requested API version is not supported by the linked `VapourSynth` library.
27    #[cfg(feature = "link-library")]
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    pub(crate) unsafe fn from_ptr(ptr: *const ffi::VSAPI) -> Self {
38        Self(ptr)
39    }
40}
41
42impl Deref for Api {
43    type Target = ffi::VSAPI;
44
45    fn deref(&self) -> &Self::Target {
46        unsafe { &*self.0 }
47    }
48}
49
50#[cfg(feature = "link-library")]
51impl Default for Api {
52    /// Creates a new `Api` instance with the default version.
53    ///
54    /// # Panics
55    ///
56    /// Internal error indicates that something went wrong with the linked `VapourSynth` library.
57    #[must_use]
58    fn default() -> Self {
59        Self::new(ffi::VAPOURSYNTH_API_MAJOR, ffi::VAPOURSYNTH_API_MINOR).unwrap()
60    }
61}
62
63#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
64#[repr(transparent)]
65pub struct VssApi(*const ffi::VSSCRIPTAPI);
66
67impl VssApi {
68    /// Creates a new `VssApi` instance with the specified major and minor version.
69    ///
70    /// # Errors
71    ///
72    /// Returns `ApiNotFound` if the requested API version is not supported by the linked `VapourSynth` library.
73    #[cfg(feature = "link-library")]
74    pub fn new(major: u16, minor: u16) -> Result<Self, ApiNotFound> {
75        let ptr = unsafe { ffi::getVSScriptAPI(vs_make_version(major, minor)) };
76        (!ptr.is_null())
77            .then_some(Self(ptr))
78            .ok_or(ApiNotFound { major, minor })
79    }
80
81    #[allow(unused)]
82    pub(crate) unsafe fn from_ptr(ptr: *const ffi::VSSCRIPTAPI) -> Self {
83        Self(ptr.cast_mut())
84    }
85}
86
87impl Deref for VssApi {
88    type Target = ffi::VSSCRIPTAPI;
89
90    fn deref(&self) -> &Self::Target {
91        unsafe { &*self.0 }
92    }
93}
94
95#[cfg(feature = "link-library")]
96impl Default for VssApi {
97    /// Creates a new `Api` instance with the default version.
98    ///
99    /// # Panics
100    ///
101    /// Internal error indicates that something went wrong with the linked `VapourSynth` library.
102    #[must_use]
103    fn default() -> Self {
104        Self::new(ffi::VSSCRIPT_API_MAJOR, ffi::VSSCRIPT_API_MINOR).unwrap()
105    }
106}
107
108pub mod error {
109    use thiserror::Error;
110
111    #[derive(Error, Debug, Clone, Copy, PartialEq, Eq, Hash)]
112    #[error(
113        "Request API with version {major}.{minor} failed. \
114        Please check if the version is supported by the linked VapourSynth library."
115    )]
116    pub struct ApiNotFound {
117        pub major: u16,
118        pub minor: u16,
119    }
120}