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 [`Inner`] type.

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

use alloc::vec::Vec;

/// Dependend fields for [`Builder`].
///
/// [`Builder`]: crate::wad::Builder
///
/// This type allows for generalising `Builder`
/// objects for multiple WAD versions.
#[repr(u8)]
#[derive(Clone, Debug)]
pub(super) enum Inner {
	/// DOOM WAD fields.
	Wad {
		/// Specifies whether the file is internal (`false`) or a patch (`true`).
		is_pwad: bool,

		/// The WAD directory.
		directory: Vec<doom::filelump_t>,
	} = Tag::Wad as u8,

	/// WAD2 fields.
	Wad2 {
		/// The WAD directory.
		directory: Vec<quake::lumpinfo_t>,
	} = Tag::Wad2 as u8,

	/// WAD3 fields.
	Wad3 {
		/// The WAD directory.
		directory: Vec<halflife::lumpinfo_t>,
	} = Tag::Wad3 as u8,
}

impl Inner {
	/// Retrieves the WAD tag.
	#[inline]
	#[must_use]
	pub fn tag(&self) -> Tag {
		match *self {
			Self::Wad { .. }  => Tag::Wad,
			Self::Wad2 { .. } => Tag::Wad2,
			Self::Wad3 { .. } => Tag::Wad3,
		}
	}

	/// Creates the appropriate magic.
	#[inline]
	#[must_use]
	pub fn magic(&self) -> [i8; 4] {
		let bytes = match *self {
			Self::Wad  { is_pwad: false, .. }  => b"IWAD",
			Self::Wad  { is_pwad: true, .. }   => b"PWAD",
			Self::Wad2 { .. }                  => b"WAD2",
			Self::Wad3 { .. }                  => b"WAD3",
		};

		bytes.map(u8::cast_signed)
	}

	/// Retrieves the count of lumps in the directory.
	#[inline]
	#[must_use]
	pub fn lump_count(&self) -> usize {
		match *self {
			Self::Wad { ref directory, .. }  => directory.len(),
			Self::Wad2 { ref directory, .. } => directory.len(),
			Self::Wad3 { ref directory, .. } => directory.len(),
		}
	}

	/// Retrieves the directory.
	#[inline]
	#[must_use]
	pub fn directory(&self) -> RawLumpSlice<'_> {
		match *self {
			Self::Wad { ref directory, .. } => {
				directory.as_slice().into()
			}

			Self::Wad2 { ref directory, .. } => {
				directory.as_slice().into()
			}

			Self::Wad3 { ref directory, .. } => {
				directory.as_slice().into()
			}
		}
	}
}