use crate::{Error, Result};
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
pub enum ParsePolicy {
#[default]
Strict,
Compatible,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum Layout {
Legacy3,
Legacy4,
V4_1,
}
impl Layout {
pub(crate) fn has_section_sizes(self) -> bool {
self == Self::V4_1
}
pub(crate) fn has_assets(self) -> bool {
self == Self::V4_1
}
pub(crate) fn has_precision(self) -> bool {
self == Self::V4_1
}
pub(crate) fn rigid_body_marker_names(self) -> bool {
self != Self::Legacy3
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Version {
pub major: u8,
pub minor: u8,
pub build: u8,
pub revision: u8,
}
impl Version {
pub const V4_1: Self = Self::new(4, 1, 0, 0);
pub fn validate(self) -> Result<()> {
self.layout().map(|_| ())
}
pub(crate) fn layout(self) -> Result<Layout> {
match (self.major, self.minor) {
(3, 0 | 1) => Ok(Layout::Legacy3),
(4, 0) => Ok(Layout::Legacy4),
(4, 1) => Ok(Layout::V4_1),
_ => Err(Error::UnsupportedVersion {
major: self.major,
minor: self.minor,
}),
}
}
#[must_use]
pub const fn new(major: u8, minor: u8, build: u8, revision: u8) -> Self {
Self {
major,
minor,
build,
revision,
}
}
#[must_use]
pub const fn from_bytes(b: [u8; 4]) -> Self {
Self::new(b[0], b[1], b[2], b[3])
}
#[must_use]
pub const fn to_bytes(self) -> [u8; 4] {
[self.major, self.minor, self.build, self.revision]
}
#[must_use]
pub const fn at_least(self, major: u8, minor: u8) -> bool {
self.major > major || (self.major == major && self.minor >= minor)
}
}
impl core::fmt::Display for Version {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(
f,
"{}.{}.{}.{}",
self.major, self.minor, self.build, self.revision
)
}
}