Skip to main content

webc/
version.rs

1use std::{
2    fmt::{self, Display, Formatter},
3    ops::Deref,
4};
5
6/// A WEBC file's version number.
7#[derive(
8    Debug, Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, serde::Serialize, serde::Deserialize,
9)]
10#[repr(transparent)]
11pub struct Version(pub [u8; 3]);
12
13impl Version {
14    /// The identifier for WEBC v1.
15    pub const V1: Version = Version([b'0', b'0', b'1']);
16    /// The identifier for WEBC v2.
17    pub const V2: Version = Version([b'0', b'0', b'2']);
18    /// The identifier for WEBC v3.
19    pub const V3: Version = Version([b'0', b'0', b'3']);
20    /// The identifier for the latest WEBC version.
21    pub const LATEST: Version = Version::V3;
22
23    #[cfg(feature = "v1")]
24    pub(crate) const fn len(&self) -> usize {
25        self.0.len()
26    }
27}
28
29impl Default for Version {
30    fn default() -> Self {
31        Version::LATEST
32    }
33}
34
35impl Display for Version {
36    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
37        write!(f, "{}", self.0.escape_ascii())
38    }
39}
40
41impl Deref for Version {
42    type Target = [u8; 3];
43
44    fn deref(&self) -> &Self::Target {
45        &self.0
46    }
47}
48
49impl PartialEq<[u8; 3]> for Version {
50    fn eq(&self, other: &[u8; 3]) -> bool {
51        self.0 == *other
52    }
53}
54
55impl PartialEq<&[u8; 3]> for Version {
56    fn eq(&self, other: &&[u8; 3]) -> bool {
57        self.0 == **other
58    }
59}
60
61impl PartialEq<&[u8]> for Version {
62    fn eq(&self, other: &&[u8]) -> bool {
63        self.0 == *other
64    }
65}
66
67impl PartialEq<[u8]> for Version {
68    fn eq(&self, other: &[u8]) -> bool {
69        self.0 == *other
70    }
71}
72
73impl PartialEq<Version> for [u8; 3] {
74    fn eq(&self, other: &Version) -> bool {
75        other == self
76    }
77}
78
79impl PartialEq<Version> for &[u8; 3] {
80    fn eq(&self, other: &Version) -> bool {
81        other == self
82    }
83}
84
85impl PartialEq<Version> for &[u8] {
86    fn eq(&self, other: &Version) -> bool {
87        other == self
88    }
89}
90
91impl PartialEq<Version> for [u8] {
92    fn eq(&self, other: &Version) -> bool {
93        other == self
94    }
95}
96
97impl From<[u8; 3]> for Version {
98    fn from(raw: [u8; 3]) -> Self {
99        Version(raw)
100    }
101}
102
103impl From<&[u8; 3]> for Version {
104    fn from(raw: &[u8; 3]) -> Self {
105        Version::from(*raw)
106    }
107}
108
109impl AsRef<[u8]> for Version {
110    fn as_ref(&self) -> &[u8] {
111        &self.0
112    }
113}
114
115impl AsRef<[u8; 3]> for Version {
116    fn as_ref(&self) -> &[u8; 3] {
117        &self.0
118    }
119}