graphics_api_version/
lib.rs1#![deny(missing_docs)]
2
3pub const OPENGL: &str = "OpenGL";
8pub const VULKAN: &str = "Vulkan";
11pub const DIRECTX: &str = "DirectX";
14pub const METAL: &str = "Metal";
17
18use std::borrow::Cow;
19use std::error::Error;
20
21#[derive(Clone, Debug, PartialEq, PartialOrd, Eq, Ord)]
23pub struct Version {
24 pub api: Cow<'static, str>,
26 pub major: u32,
28 pub minor: u32,
30}
31
32impl Version {
33 #[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 #[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 #[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 #[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 #[must_use]
75 pub fn is_opengl(&self) -> bool {self.api == OPENGL}
76
77 #[must_use]
79 pub fn is_vulkan(&self) -> bool {self.api == VULKAN}
80
81 #[must_use]
83 pub fn is_directx(&self) -> bool {self.api == DIRECTX}
84
85 #[must_use]
87 pub fn is_metal(&self) -> bool {self.api == METAL}
88}
89
90#[derive(Debug)]
92pub struct UnsupportedGraphicsApiError {
93 pub found: Cow<'static, str>,
95 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}