oct 0.28.2

Octonary transcodings.
Documentation
// Copyright 2024-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 [`RawError`] type.

use crate::io::{ErrorKind, SimpleError, UnpackedError};

#[cfg(feature = "alloc")]
use crate::io::CustomError;

#[cfg(feature = "std")]
use crate::io::RawOsError;

#[cfg(all(not(target_os = "uefi"), target_pointer_width = "64"))]
use crate::io::PackedError;

#[cfg(feature = "alloc")]
use alloc::boxed::Box;

/// A raw, input/output error.
#[repr(transparent)]
#[derive(Clone, Copy, Debug)]
pub(super) struct RawError {
	/// The packed error.
	#[cfg(all(not(target_os = "uefi"), target_pointer_width = "64"))]
	packed: PackedError,

	/// The unpacked error.
	#[cfg(any(not(target_pointer_width = "64"), target_os = "uefi"))]
	unpacked: UnpackedError,
}

impl RawError {
	/// Constructs a new [`Simple`] error.
	///
	/// [`Simple`]: UnpackedError::Simple
	#[inline]
	#[must_use]
	pub const fn new_simple(message: &'static SimpleError) -> Self {
		#[cfg(all(not(target_os = "uefi"), target_pointer_width = "64"))]
		{ Self { packed: PackedError::new_simple(message) } }

		#[cfg(any(not(target_pointer_width = "64"), target_os = "uefi"))]
		{ Self { unpacked: UnpackedError::Simple(message) } }
	}

	/// Constructs a new [`Inline`] error.
	///
	/// [`Inline`]: UnpackedError::Inline
	#[inline]
	#[must_use]
	pub fn new_inline(kind: ErrorKind) -> Self {
		#[cfg(all(not(target_os = "uefi"), target_pointer_width = "64"))]
		{ Self { packed: PackedError::new_inline(kind) } }

		#[cfg(any(not(target_pointer_width = "64"), target_os = "uefi"))]
		{ Self { unpacked: UnpackedError::Inline(kind) } }
	}

	/// Constructs a new [`Custom`] error.
	///
	/// [`Custom`]: UnpackedError::Custom
	#[cfg(feature = "alloc")]
	#[inline]
	#[must_use]
	pub fn new_custom(custom: Box<CustomError>) -> Self {
		#[cfg(all(not(target_os = "uefi"), target_pointer_width = "64"))]
		{ Self { packed: PackedError::new_custom(custom) } }

		#[cfg(any(not(target_pointer_width = "64"), target_os = "uefi"))]
		{
			let custom = Box::into_raw(custom);
			{ Self { unpacked: UnpackedError::Custom(custom) } }
		}
	}

	/// Constructs a new [`Os`] error.
	///
	/// [`Os`]: UnpackedError::Os
	#[cfg(feature = "std")]
	#[inline]
	#[must_use]
	pub fn new_os(raw: RawOsError) -> Self {
		#[cfg(all(not(target_os = "uefi"), target_pointer_width = "64"))]
		{ Self { packed: PackedError::new_os(raw) } }

		#[cfg(any(not(target_pointer_width = "64"), target_os = "uefi"))]
		{ Self { unpacked: UnpackedError::Os(raw) } }
	}

	/// Unpacks the packed error.
	#[inline]
	#[must_use]
	pub fn unpack(self) -> UnpackedError {
		#[cfg(all(not(target_os = "uefi"), target_pointer_width = "64"))]
		{ self.packed.unpack() }

		#[cfg(any(not(target_pointer_width = "64"), target_os = "uefi"))]
		{ self.unpacked }
	}
}