use crate::traits::{Name, ParseBytes};
use crate::{
field_parsing_error, impl_name,
result::{SqliteError, SqliteResult},
};
#[derive(Debug, Default, PartialEq, Eq)]
pub enum SchemaFormat {
Format1,
Format2,
Format3,
#[default]
Format4,
}
impl TryFrom<u32> for SchemaFormat {
type Error = SqliteError;
fn try_from(value: u32) -> Result<Self, Self::Error> {
match value {
1 => Ok(Self::Format1),
2 => Ok(Self::Format2),
3 => Ok(Self::Format3),
4 => Ok(Self::Format4),
_ => Err(field_parsing_error! {Self::NAME.into()}),
}
}
}
impl From<&SchemaFormat> for u32 {
fn from(value: &SchemaFormat) -> Self {
match value {
SchemaFormat::Format1 => 1,
SchemaFormat::Format2 => 2,
SchemaFormat::Format3 => 3,
SchemaFormat::Format4 => 4,
}
}
}
impl_name! {SchemaFormat}
impl ParseBytes for SchemaFormat {
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()
}
}