use crate::wad::Tag;
use crate::wad::ffi::{doom, halflife, quake};
use core::fmt::{self, Debug, Formatter};
#[derive(Clone, Copy)]
pub(super) struct RawHeader<'a>(Inner<'a>);
impl<'a> RawHeader<'a> {
#[inline(always)]
#[must_use]
pub fn identification(self) -> [i8; 4] {
#[expect(clippy::match_same_arms)]
match self.0 {
Inner::Wad(&doom::wadinfo_t { identification, .. }) => identification,
Inner::Wad2(&quake::wadinfo_t { identification, .. }) => identification,
Inner::Wad3(&halflife::wadinfo_t { identification, .. }) => identification,
}
}
#[inline(always)]
#[must_use]
pub fn numlumps(self) -> i32 {
#[expect(clippy::match_same_arms)]
match self.0 {
Inner::Wad(&doom::wadinfo_t { numlumps, .. }) => numlumps,
Inner::Wad2(&quake::wadinfo_t { numlumps, .. }) => numlumps,
Inner::Wad3(&halflife::wadinfo_t { numlumps, .. }) => numlumps,
}
}
#[inline(always)]
#[must_use]
pub fn infotableofs(self) -> i32 {
#[expect(clippy::match_same_arms)]
match self.0 {
Inner::Wad(&doom::wadinfo_t { infotableofs, .. }) => infotableofs,
Inner::Wad2(&quake::wadinfo_t { infotableofs, .. }) => infotableofs,
Inner::Wad3(&halflife::wadinfo_t { infotableofs, .. }) => infotableofs,
}
}
}
impl Debug for RawHeader<'_> {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
Debug::fmt(&self.0, f)
}
}
impl<'a> From<&'a doom::wadinfo_t> for RawHeader<'a> {
#[inline(always)]
fn from(value: &'a doom::wadinfo_t) -> Self {
let inner = Inner::Wad(value);
Self(inner)
}
}
impl<'a> From<&'a halflife::wadinfo_t> for RawHeader<'a> {
#[inline(always)]
fn from(value: &'a halflife::wadinfo_t) -> Self {
let inner = Inner::Wad3(value);
Self(inner)
}
}
impl<'a> From<&'a quake::wadinfo_t> for RawHeader<'a> {
#[inline(always)]
fn from(value: &'a quake::wadinfo_t) -> Self {
let inner = Inner::Wad2(value);
Self(inner)
}
}
#[repr(i8)]
#[derive(Clone, Copy)]
enum Inner<'a> {
Wad(&'a doom::wadinfo_t) = Tag::Wad as i8,
Wad2(&'a quake::wadinfo_t) = Tag::Wad2 as i8,
Wad3(&'a halflife::wadinfo_t) = Tag::Wad3 as i8,
}
impl Debug for Inner<'_> {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
match *self {
Self::Wad( ref inner) => Debug::fmt(inner, f),
Self::Wad2(ref inner) => Debug::fmt(inner, f),
Self::Wad3(ref inner) => Debug::fmt(inner, f),
}
}
}