use super::traits::{Name, ParseBytes};
use crate::{
field_parsing_error, impl_name,
result::{SQLiteError, SQLiteResult},
};
use core::fmt::Display;
#[derive(Debug)]
pub enum DatabaseTextEncoding {
Utf8,
Utf16Le,
Utf16Be,
}
impl From<&DatabaseTextEncoding> for u32 {
fn from(value: &DatabaseTextEncoding) -> Self {
match value {
DatabaseTextEncoding::Utf8 => 1,
DatabaseTextEncoding::Utf16Le => 2,
DatabaseTextEncoding::Utf16Be => 3,
}
}
}
impl TryFrom<u32> for DatabaseTextEncoding {
type Error = SQLiteError;
fn try_from(value: u32) -> Result<Self, Self::Error> {
match value {
1 => Ok(Self::Utf8),
2 => Ok(Self::Utf16Le),
3 => Ok(Self::Utf16Be),
_ => Err(field_parsing_error! {Self::NAME}),
}
}
}
impl Display for DatabaseTextEncoding {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
let number = u32::from(self);
let name = match self {
DatabaseTextEncoding::Utf8 => "utf8",
DatabaseTextEncoding::Utf16Le => "utf16le",
DatabaseTextEncoding::Utf16Be => "utf16le",
};
write!(f, "{number} ({name})")
}
}
impl_name! {DatabaseTextEncoding}
impl ParseBytes for DatabaseTextEncoding {
const LENGTH_BYTES: usize = 4;
fn parsing_handler(bytes: &[u8]) -> SQLiteResult<Self> {
let buf: [u8; Self::LENGTH_BYTES] = bytes.try_into()?;
let value = u32::from_be_bytes(buf);
value.try_into()
}
}