use super::traits::{Name, ParseBytes};
use crate::{
field_parsing_error, impl_name,
result::{SQLiteError, SQLiteResult},
};
use core::fmt::Display;
#[derive(Debug)]
pub struct FileFormatVersionNumbers {
write_version: FileFormatWriteVersion,
read_version: FileFormatReadVersion,
}
impl FileFormatVersionNumbers {
pub fn write_version(&self) -> &FileFormatWriteVersion {
&self.write_version
}
pub fn read_version(&self) -> &FileFormatReadVersion {
&self.read_version
}
}
impl_name! {FileFormatVersionNumbers}
impl ParseBytes for FileFormatVersionNumbers {
const LENGTH_BYTES: usize = 2;
fn parsing_handler(bytes: &[u8]) -> SQLiteResult<Self> {
let write_version = FileFormatWriteVersion::parsing_handler(&[bytes[0]])?;
let read_version = FileFormatReadVersion::parsing_handler(&[bytes[1]])?;
Ok(Self {
write_version,
read_version,
})
}
}
#[derive(Debug)]
pub enum FileFormatWriteVersion {
Legacy,
WAL,
}
impl From<&FileFormatWriteVersion> for u8 {
fn from(value: &FileFormatWriteVersion) -> Self {
match value {
FileFormatWriteVersion::Legacy => 1,
FileFormatWriteVersion::WAL => 2,
}
}
}
impl_name! {FileFormatWriteVersion}
impl ParseBytes for FileFormatWriteVersion {
const LENGTH_BYTES: usize = 1;
fn parsing_handler(bytes: &[u8]) -> SQLiteResult<Self> {
let one_byte = *bytes.first().ok_or(field_parsing_error! {Self::NAME})?;
match one_byte {
1 => Ok(Self::Legacy),
2 => Ok(Self::WAL),
_ => Err(field_parsing_error! {Self::NAME}),
}
}
}
impl Display for FileFormatWriteVersion {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "{}", u8::from(self))
}
}
#[derive(Debug)]
pub enum FileFormatReadVersion {
Legacy,
WAL,
}
impl From<&FileFormatReadVersion> for u8 {
fn from(value: &FileFormatReadVersion) -> Self {
match value {
FileFormatReadVersion::Legacy => 1,
FileFormatReadVersion::WAL => 2,
}
}
}
impl_name! {FileFormatReadVersion}
impl ParseBytes for FileFormatReadVersion {
const LENGTH_BYTES: usize = 1;
fn parsing_handler(bytes: &[u8]) -> SQLiteResult<Self> {
let one_byte = *bytes.first().ok_or(field_parsing_error! {Self::NAME})?;
match one_byte {
1 => Ok(Self::Legacy),
2 => Ok(Self::WAL),
_ => Err(field_parsing_error! {Self::NAME}),
}
}
}
impl Display for FileFormatReadVersion {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "{}", u8::from(self))
}
}