#![deny(missing_docs)]
pub const OPENGL: &str = "OpenGL";
pub const VULKAN: &str = "Vulkan";
pub const DIRECTX: &str = "DirectX";
pub const METAL: &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 {
#[must_use]
pub fn opengl(major: u32, minor: u32) -> Version {
Version {
api: OPENGL.into(),
major,
minor,
}
}
#[must_use]
pub fn vulkan(major: u32, minor: u32) -> Version {
Version {
api: VULKAN.into(),
major,
minor,
}
}
#[must_use]
pub fn directx(major: u32, minor: u32) -> Version {
Version {
api: DIRECTX.into(),
major,
minor,
}
}
#[must_use]
pub fn metal(major: u32, minor: u32) -> Version {
Version {
api: METAL.into(),
major,
minor,
}
}
#[must_use]
pub fn is_opengl(&self) -> bool {self.api == OPENGL}
#[must_use]
pub fn is_vulkan(&self) -> bool {self.api == VULKAN}
#[must_use]
pub fn is_directx(&self) -> bool {self.api == DIRECTX}
#[must_use]
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);
}
}