goldforge 0.8.1

Library for handling file formats used by GoldSrc and related engines.
Documentation
// Copyright 2025-2026 Gabriel Bjørnager Jensen.
//
// This Source Code Form is subject to the terms of
// the Mozilla Public License, v. 2.0. If a copy of
// the MPL was not distributed with this file, you
// can obtain one at:
// <https://mozilla.org/MPL/2.0/>.

//! The [`RawLumpSlice`] type.

use crate::wad::{RawLumpSliceIter, RawLump, Tag};
use crate::wad::ffi::{doom, halflife, quake};

use oct::IntoOcts;

/// A raw lump slice.
#[derive(
	Clone,
	Copy,
	Debug,
	Default,
)]
pub(super) struct RawLumpSlice<'a>(Inner<'a>);

impl<'a> RawLumpSlice<'a> {
	/// Creates an iterator over the directory.
	#[inline]
	pub fn iter(&self) -> RawLumpSliceIter<'a> {
		match self.0 {
			Inner::Wad(directory)  => directory.iter().into(),
			Inner::Wad2(directory) => directory.iter().into(),
			Inner::Wad3(directory) => directory.iter().into(),
		}
	}

	/// Creates an iterator over the directory.
	#[cfg_attr(not(feature = "alloc"), expect(dead_code))]
	#[inline]
	#[must_use]
	pub fn inner_as_bytes(&self) -> &[u8] {
		match self.0 {
			Inner::Wad(directory)  => directory.as_octs(),
			Inner::Wad2(directory) => directory.as_octs(),
			Inner::Wad3(directory) => directory.as_octs(),
		}
	}
}

impl<'a> From<&'a [doom::filelump_t]> for RawLumpSlice<'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 RawLumpSlice<'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 RawLumpSlice<'a> {
	#[inline(always)]
	fn from(value: &'a [quake::lumpinfo_t]) -> Self {
		let inner = Inner::Wad2(value);
		Self(inner)
	}
}

impl<'a> IntoIterator for RawLumpSlice<'a> {
	type Item = RawLump<'a>;

	type IntoIter = RawLumpSliceIter<'a>;

	#[inline]
	fn into_iter(self) -> Self::IntoIter {
		self.iter()
	}
}

/// See [`RawLumpSlice`].
#[repr(u8)]
#[derive(Clone, Copy, Debug)]
enum Inner<'a> {
	/// A DOOM WAD lump slice.
	Wad(&'a [doom::filelump_t]) = Tag::Wad as u8,

	/// A WAD2 lump slice.
	Wad2(&'a [quake::lumpinfo_t]) = Tag::Wad2 as u8,

	/// A WAD2 lump slice.
	Wad3(&'a [halflife::lumpinfo_t]) = Tag::Wad3 as u8,
}

impl Default for Inner<'_> {
	#[inline]
	fn default() -> Self {
		Inner::Wad(Default::default())
	}
}