use crate::wad::DecodeError;
use crate::wad::ffi::{doom, halflife, quake};
use core::hint::unreachable_unchecked;
use oct::IntoOcts;
#[repr(u8)]
#[derive(
Clone,
Copy,
Debug,
Eq,
PartialEq,
)]
pub(super) enum Tag {
Wad = 1,
Wad2 = 2,
Wad3 = 3,
}
impl Tag {
#[allow(dead_code)]
#[inline]
pub fn from_magic(magic: [i8; 4]) -> Result<Self, DecodeError> {
match magic.as_octs() {
| b"IWAD"
| b"DAWI"
| b"PWAD"
| b"DAWP"
=> Ok(Self::Wad),
| b"WAD2"
| b"2DAW"
=> Ok(Self::Wad2),
b"WAD3" => Ok(Self::Wad3),
_ => Err(DecodeError::unknown_magic(magic)),
}
}
#[inline]
pub unsafe fn from_magic_unchecked(magic: [i8; 4]) -> Self {
match magic.as_octs() {
| b"IWAD"
| b"DAWI"
| b"PWAD"
| b"DAWP"
=> Self::Wad,
| b"WAD2"
| b"2DAW"
=> Self::Wad2,
b"WAD3" => Self::Wad3,
_ => unsafe { unreachable_unchecked() },
}
}
#[inline]
#[must_use]
pub fn header_size(self) -> usize {
match self {
Self::Wad => size_of::<doom::wadinfo_t>(),
Self::Wad2 => size_of::<quake::wadinfo_t>(),
Self::Wad3 => size_of::<halflife::wadinfo_t>(),
}
}
#[inline]
#[must_use]
pub fn lump_size(self) -> usize {
match self {
Self::Wad => size_of::<doom::filelump_t>(),
Self::Wad2 => size_of::<quake::lumpinfo_t>(),
Self::Wad3 => size_of::<halflife::lumpinfo_t>(),
}
}
}