graphics_api_version/
lib.rs

1#![deny(missing_docs)]
2
3//! A library for storing graphics API versions.
4
5/// A graphics API developed by Khronos Group.
6/// See https://en.wikipedia.org/wiki/OpenGL for more information.
7pub const OPENGL: &str = "OpenGL";
8/// A graphics API developed by Khronos Group.
9/// See https://en.wikipedia.org/wiki/Vulkan_(API) for more information.
10pub const VULKAN: &str = "Vulkan";
11/// A graphics API developed by Microsoft.
12/// See https://en.wikipedia.org/wiki/DirectX for more information.
13pub const DIRECTX: &str = "DirectX";
14/// A graphics API developed by Apple.
15/// See https://en.wikipedia.org/wiki/Metal_%28API%29 for more information.
16pub const METAL: &str = "Metal";
17
18use std::borrow::Cow;
19use std::error::Error;
20
21/// Stores graphics API version.
22#[derive(Clone, Debug, PartialEq, PartialOrd, Eq, Ord)]
23pub struct Version {
24    /// A string identifying the API.
25    pub api: Cow<'static, str>,
26    /// Major version.
27    pub major: u32,
28    /// Minor version.
29    pub minor: u32,
30}
31
32impl Version {
33    /// Creates a new OpenGL version.
34    #[must_use]
35    pub fn opengl(major: u32, minor: u32) -> Version {
36        Version {
37            api: OPENGL.into(),
38            major,
39            minor,
40        }
41    }
42
43    /// Creates a new Vulkan version.
44    #[must_use]
45    pub fn vulkan(major: u32, minor: u32) -> Version {
46        Version {
47            api: VULKAN.into(),
48            major,
49            minor,
50        }
51    }
52
53    /// Creates a new DirectX version.
54    #[must_use]
55    pub fn directx(major: u32, minor: u32) -> Version {
56        Version {
57            api: DIRECTX.into(),
58            major,
59            minor,
60        }
61    }
62
63    /// Creates a new Metal version.
64    #[must_use]
65    pub fn metal(major: u32, minor: u32) -> Version {
66        Version {
67            api: METAL.into(),
68            major,
69            minor,
70        }
71    }
72
73    /// Returns `true` if the API is OpenGL, `false` otherwise.
74    #[must_use]
75    pub fn is_opengl(&self) -> bool {self.api == OPENGL}
76
77    /// Returns `true` if the API is Vulkan, `false` otherwise.
78    #[must_use]
79    pub fn is_vulkan(&self) -> bool {self.api == VULKAN}
80
81    /// Returns `true` if the API is DirectX, `false` otherwise.
82    #[must_use]
83    pub fn is_directx(&self) -> bool {self.api == DIRECTX}
84
85    /// Returns `true` if the API is metal, `false` otherwise.
86    #[must_use]
87    pub fn is_metal(&self) -> bool {self.api == METAL}
88}
89
90/// An error for when a graphics API is unsupported.
91#[derive(Debug)]
92pub struct UnsupportedGraphicsApiError {
93    /// The requested graphics API.
94    pub found: Cow<'static, str>,
95    /// A list of supported graphics APIs.
96    pub expected: Vec<Cow<'static, str>>,
97}
98
99impl std::fmt::Display for UnsupportedGraphicsApiError {
100    fn fmt(&self, w: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
101        let mut list = String::new();
102        for ex in &self.expected {
103            list.push_str(&format!("{}, ", ex));
104        }
105        write!(w, "Unsupported graphics API: Expected {}found {}", list, self.found)
106    }
107}
108
109impl Error for UnsupportedGraphicsApiError {}
110
111#[cfg(test)]
112mod tests {
113    use super::*;
114
115    #[test]
116    fn test_it() {
117        let a = Version::opengl(3, 2);
118        let b = Version::opengl(4, 0);
119        assert!(b > a);
120    }
121}