use crate::wad::Tag;
use crate::wad::ffi::{doom, halflife, quake};
use core::fmt::{self, Debug, Formatter};
#[derive(Clone, Copy)]
pub(super) struct RawLump<'a>(Inner<'a>);
impl<'a> RawLump<'a> {
#[inline(always)]
#[must_use]
pub fn filepos(self) -> i32 {
#[expect(clippy::match_same_arms)]
match self.0 {
Inner::Wad(&doom::filelump_t { filepos, .. }) => filepos,
Inner::Wad2(&quake::lumpinfo_t { filepos, .. }) => filepos,
Inner::Wad3(&halflife::lumpinfo_t { filepos, .. }) => filepos,
}
}
#[inline(always)]
#[must_use]
pub fn disksize(self) -> i32 {
#[expect(clippy::match_same_arms)]
match self.0 {
Inner::Wad(&doom::filelump_t { size, .. }) => size,
Inner::Wad2(&quake::lumpinfo_t { disksize, .. }) => disksize,
Inner::Wad3(&halflife::lumpinfo_t { disksize, .. }) => disksize,
}
}
#[inline(always)]
#[must_use]
pub fn size(self) -> i32 {
#[expect(clippy::match_same_arms)]
match self.0 {
Inner::Wad(&doom::filelump_t { size, .. }) => size,
Inner::Wad2(&quake::lumpinfo_t { size, .. }) => size,
Inner::Wad3(&halflife::lumpinfo_t { size, .. }) => size,
}
}
#[inline(always)]
#[must_use]
pub fn r#type(self) -> i8 {
#[expect(clippy::match_same_arms)]
match self.0 {
Inner::Wad2(&quake::lumpinfo_t { r#type, .. }) => r#type,
Inner::Wad3(&halflife::lumpinfo_t { r#type, .. }) => r#type,
_ => quake::TYP_NONE,
}
}
#[inline(always)]
#[must_use]
pub fn compression(self) -> i8 {
#[expect(clippy::match_same_arms)]
match self.0 {
Inner::Wad2(&quake::lumpinfo_t { compression, .. }) => compression,
Inner::Wad3(&halflife::lumpinfo_t {compression, .. }) => compression,
_ => quake::CMP_NONE,
}
}
#[inline(always)]
#[must_use]
pub fn name(self) -> &'a [i8] {
#[expect(clippy::match_same_arms)]
#[expect(clippy::needless_borrowed_reference)]
match self.0 {
Inner::Wad(&doom::filelump_t { ref name, .. }) => name,
Inner::Wad2(&quake::lumpinfo_t { ref name, .. }) => name,
Inner::Wad3(&halflife::lumpinfo_t { ref name, .. }) => name,
}
}
}
impl Debug for RawLump<'_> {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
Debug::fmt(&self.0, f)
}
}
impl<'a> From<&'a doom::filelump_t> for RawLump<'a> {
#[inline(always)]
fn from(value: &'a doom::filelump_t) -> Self {
let inner = Inner::Wad(value);
Self(inner)
}
}
impl<'a> From<&'a halflife::lumpinfo_t> for RawLump<'a> {
#[inline(always)]
fn from(value: &'a halflife::lumpinfo_t) -> Self {
let inner = Inner::Wad3(value);
Self(inner)
}
}
impl<'a> From<&'a quake::lumpinfo_t> for RawLump<'a> {
#[inline(always)]
fn from(value: &'a quake::lumpinfo_t) -> Self {
let inner = Inner::Wad2(value);
Self(inner)
}
}
#[repr(u8)]
#[derive(Clone, Copy)]
enum Inner<'a> {
Wad(&'a doom::filelump_t) = Tag::Wad as u8,
Wad2(&'a quake::lumpinfo_t) = Tag::Wad2 as u8,
Wad3(&'a halflife::lumpinfo_t) = Tag::Wad3 as u8,
}
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),
}
}
}