1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#![deny(missing_docs)]
pub const OPENGL: &'static str = "OpenGL";
pub const VULKAN: &'static str = "Vulkan";
pub const DIRECTX: &'static str = "DirectX";
pub const METAL: &'static str = "Metal";
use std::borrow::Cow;
use std::error::Error;
#[derive(Clone, Debug, PartialEq, PartialOrd, Eq, Ord)]
pub struct Version {
pub api: Cow<'static, str>,
pub major: u32,
pub minor: u32,
}
impl Version {
pub fn opengl(major: u32, minor: u32) -> Version {
Version {
api: OPENGL.into(),
major,
minor,
}
}
pub fn vulkan(major: u32, minor: u32) -> Version {
Version {
api: VULKAN.into(),
major,
minor,
}
}
pub fn directx(major: u32, minor: u32) -> Version {
Version {
api: DIRECTX.into(),
major,
minor,
}
}
pub fn metal(major: u32, minor: u32) -> Version {
Version {
api: METAL.into(),
major,
minor,
}
}
pub fn is_opengl(&self) -> bool {self.api == OPENGL}
pub fn is_vulkan(&self) -> bool {self.api == VULKAN}
pub fn is_directx(&self) -> bool {self.api == DIRECTX}
pub fn is_metal(&self) -> bool {self.api == METAL}
}
#[derive(Debug)]
pub struct UnsupportedGraphicsApiError {
pub found: Cow<'static, str>,
pub expected: Vec<Cow<'static, str>>,
}
impl std::fmt::Display for UnsupportedGraphicsApiError {
fn fmt(&self, w: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
let mut list = String::new();
for ex in &self.expected {
list.push_str(&format!("{}, ", ex));
}
write!(w, "Unsupported graphics API: Expected {}found {}", list, self.found)
}
}
impl Error for UnsupportedGraphicsApiError {}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_it() {
let a = Version::opengl(3, 2);
let b = Version::opengl(4, 0);
assert!(b > a);
}
}