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)]

//! A library for storing graphics API versions.

/// A graphics API developed by Khronos Group.
/// See https://en.wikipedia.org/wiki/OpenGL for more information.
pub const OPENGL: &'static str = "OpenGL";
/// A graphics API developed by Khronos Group.
/// See https://en.wikipedia.org/wiki/Vulkan_(API) for more information.
pub const VULKAN: &'static str = "Vulkan";
/// A graphics API developed by Microsoft.
/// See https://en.wikipedia.org/wiki/DirectX for more information.
pub const DIRECTX: &'static str = "DirectX";
/// A graphics API developed by Apple.
/// See https://en.wikipedia.org/wiki/Metal_%28API%29 for more information.
pub const METAL: &'static str = "Metal";

use std::borrow::Cow;
use std::error::Error;

/// Stores graphics API version.
#[derive(Clone, Debug, PartialEq, PartialOrd, Eq, Ord)]
pub struct Version {
    /// A string identifying the API.
    pub api: Cow<'static, str>,
    /// Major version.
    pub major: u32,
    /// Minor version.
    pub minor: u32,
}

impl Version {
    /// Creates a new OpenGL version.
    pub fn opengl(major: u32, minor: u32) -> Version {
        Version {
            api: OPENGL.into(),
            major,
            minor,
        }
    }

    /// Creates a new Vulkan version.
    pub fn vulkan(major: u32, minor: u32) -> Version {
        Version {
            api: VULKAN.into(),
            major,
            minor,
        }
    }

    /// Creates a new DirectX version.
    pub fn directx(major: u32, minor: u32) -> Version {
        Version {
            api: DIRECTX.into(),
            major,
            minor,
        }
    }

    /// Creates a new Metal version.
    pub fn metal(major: u32, minor: u32) -> Version {
        Version {
            api: METAL.into(),
            major,
            minor,
        }
    }

    /// Returns `true` if the API is OpenGL, `false` otherwise.
    pub fn is_opengl(&self) -> bool {self.api == OPENGL}

    /// Returns `true` if the API is Vulkan, `false` otherwise.
    pub fn is_vulkan(&self) -> bool {self.api == VULKAN}

    /// Returns `true` if the API is DirectX, `false` otherwise.
    pub fn is_directx(&self) -> bool {self.api == DIRECTX}

    /// Returns `true` if the API is metal, `false` otherwise.
    pub fn is_metal(&self) -> bool {self.api == METAL}
}

/// An error for when a graphics API is unsupported.
#[derive(Debug)]
pub struct UnsupportedGraphicsApiError {
    /// The requiested graphics API.
    pub found: Cow<'static, str>,
    /// A list of supported graphics APIs.
    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);
    }
}