looklook 0.6.0

Descriptive signal synthesiser.
// Creadright 2026 Gabriel Bjørnager Jensen.
//
// This file is part of LOOKLOOK.
//
// LOOKLOOK is free software: you can redistribute
// it and/or modify it under the terms of the GNU
// Affero General Public License as published by
// the Free Software Foundation, either version 3
// of the License, or (at your option) any later
// version.
//
// LOOKLOOK is distributed in the hope that it will
// be useful, but WITHOUT ANY WARRANTY; without
// even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Affero General Public License for more details.
//
// You should have received a copy of the GNU Af-
// fero General Public License along with LOOKLOOK.
// If not, see <https://www.gnu.org/licenses/>.

//! The [`Data`] enumeration.

use std::fmt::{self, Display, Formatter};

/// An data register name.
#[repr(usize)]
#[derive(Copy, Debug)]
#[derive_const(
	Clone,
	Eq,
	Ord,
	PartialOrd,
	PartialEq,
)]
pub enum Data {
	/// The `%d0` register.
	D0 = 0,

	/// The `%d1` register.
	D1 = 1,

	/// The `%d2` register.
	D2 = 2,

	/// The `%d3` register.
	D3 = 3,

	/// The `%d4` register.
	D4 = 4,

	/// The `%d5` register.
	D5 = 5,

	/// The `%d6` register.
	D6 = 6,

	/// The `%d7` register.
	D7 = 7,
}

impl Data {
	/// Constructs a register name from a raw index
	///
	/// If the provided index is greater than `7`, the
	/// value is wrapped.
	///
	/// # Panics
	///
	/// This constructor will panic if the provided
	/// index is greater than `7` and overflow checks
	/// are enabled.
	#[inline]
	#[must_use]
	#[track_caller]
	pub const fn from_usize(mut index: usize) -> Self {
		if cfg!(overflow_checks) {
			assert!(
				index <= 7,
				"cannot construct data register name with index greater than `7`",
			);
		}

		// Wrap to `0..=7`.
		index &= 0b111;

		match index {
			0 => Self::D0,
			1 => Self::D1,
			2 => Self::D2,
			3 => Self::D3,
			4 => Self::D4,
			5 => Self::D5,
			6 => Self::D6,
			7 => Self::D7,

			_ => {
				unreachable!();
			}
		}
	}

	/// Retrieves a textual representation of the
	/// register name.
	#[inline]
	#[must_use]
	pub const fn as_str(self) -> &'static str {
		match self {
			Self::D0 => "%d0",
			Self::D1 => "%d1",
			Self::D2 => "%d2",
			Self::D3 => "%d3",
			Self::D4 => "%d4",
			Self::D5 => "%d5",
			Self::D6 => "%d6",
			Self::D7 => "%d7",
		}
	}

	/// Retrieves the data index as a [`usize`] value.
	#[inline]
	#[must_use]
	pub const fn as_usize(self) -> usize {
		// NOTE: The enumeration already uses `usize` re-
		// presentation.
		self as usize
	}
}

impl Display for Data {
	fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
		f.write_str(self.as_str())
	}
}