use core::fmt::Debug;
use core::fmt::Display;
use core::fmt::Formatter;
use core::fmt::Result;
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Endian {
Little = 0,
Big = 1,
#[default]
Native = 2,
}
pub const LE: Endian = Endian::Little;
pub const BE: Endian = Endian::Big;
pub const NE: Endian = Endian::Native;
impl Endian {
pub fn from_utf16_bom(bytes: [u8; 2]) -> Option<Self> {
match bytes {
[0xFE, 0xFF] => Some(Endian::Big),
[0xFF, 0xFE] => Some(Endian::Little),
_ => None,
}
}
pub fn into_utf16_bom(&self) -> [u8; 2] {
match self {
Endian::Big => [0xFE, 0xFF],
Endian::Little => [0xFF, 0xFE],
#[cfg(target_endian = "big")]
Endian::Native => [0xFE, 0xFF],
#[cfg(target_endian = "little")]
Endian::Native => [0xFF, 0xFE],
}
}
pub fn from_utf32_bom(bytes: [u8; 4]) -> Option<Self> {
match bytes {
[0x00, 0x00, 0xFE, 0xFF] => Some(Endian::Big),
[0xFF, 0xFE, 0x00, 0x00] => Some(Endian::Little),
_ => None,
}
}
pub fn into_utf32_bom(&self) -> [u8; 4] {
match self {
Endian::Big => [0x00, 0x00, 0xFE, 0xFF],
Endian::Little => [0xFF, 0xFE, 0x00, 0x00],
#[cfg(target_endian = "big")]
Endian::Native => [0x00, 0x00, 0xFE, 0xFF],
#[cfg(target_endian = "little")]
Endian::Native => [0xFF, 0xFE, 0x00, 0x00],
}
}
}
impl Into<&'static str> for Endian {
fn into(self) -> &'static str {
match self {
Endian::Little => "Endian::Little",
Endian::Big => "Endian::Big",
Endian::Native => "Endian::Native",
}
}
}
impl Display for Endian {
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
let val: &'static str = (*self).into();
core::fmt::Display::fmt(val, f)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum SeekFrom {
Start(usize),
End(isize),
Current(isize),
}
impl Into<String> for SeekFrom {
fn into(self) -> String {
match self {
SeekFrom::Start(shift) => format!("SeekFrom::Start({})", shift),
SeekFrom::End(shift) => format!("SeekFrom::End({})", shift),
SeekFrom::Current(shift) => format!("SeekFrom::Current({})", shift),
}
}
}
impl Display for SeekFrom {
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
let val: String = (*self).into();
std::fmt::Display::fmt(&val, f)
}
}