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

use crate::wad::ffi::quake;

use core::num::NonZero;

/// A compression format.
#[repr(transparent)]
#[derive(
	Clone,
	Copy,
	Debug,
	Eq,
	PartialEq,
)]
pub struct Compression(NonZero<i8>);

impl Compression {
	/// Quake's LZSS format.
	pub const LZSS: Self = Self::from_i8(quake::CMP_LZSS).unwrap();

	/// Constructs a compression from an [`i8`] value.
	///
	/// If the provided value is zero, this function
	/// will always return [`None`].
	#[inline(always)]
	#[must_use]
	pub const fn from_i8(value: i8) -> Option<Self> {
		match NonZero::new(value) {
			Some(value) => Some(Self(value)),
			_           => None,
		}
	}

	/// Interprets the compression format as an [`i8`]
	/// value.
	///
	/// This value is guaranteed to never be zero.
	#[inline(always)]
	#[must_use]
	pub const fn to_i8(self) -> i8 {
		self.0.get()
	}
}

impl From<Compression> for i8 {
	#[inline(always)]
	fn from(value: Compression) -> Self {
		value.to_i8()
	}
}