use alloc::format;
use super::ParseBytes;
use crate::result::{SQLiteError, SQLiteResult};
#[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 ParseBytes<&[u8]> for FileFormatVersionNumbers {
fn struct_name() -> &'static str {
"FileFormatVersionNumbers"
}
fn bytes_length() -> 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 ParseBytes<u8> for FileFormatWriteVersion {
fn struct_name() -> &'static str {
"FileFormatWriteVersion"
}
fn bytes_length() -> usize {
1
}
fn parsing_handler(bytes: &[u8]) -> crate::result::SQLiteResult<Self> {
let one_byte = bytes.get(0).ok_or(SQLiteError::Custom(format!(
"Impossible state on parsing {}",
Self::struct_name()
)))?;
match one_byte {
1 => Ok(Self::Legacy),
2 => Ok(Self::WAL),
_ => Err(SQLiteError::msg(
"Invalid payload for FileFormatReadVersion",
)),
}
}
}
#[derive(Debug)]
pub enum FileFormatReadVersion {
Legacy,
WAL,
}
impl ParseBytes<u8> for FileFormatReadVersion {
fn struct_name() -> &'static str {
"FileFormatReadVersion"
}
fn bytes_length() -> usize {
1
}
fn parsing_handler(bytes: &[u8]) -> crate::result::SQLiteResult<Self> {
let one_byte = bytes.get(0).ok_or(SQLiteError::Custom(format!(
"Impossible state on parsing {}",
Self::struct_name()
)))?;
match one_byte {
1 => Ok(Self::Legacy),
2 => Ok(Self::WAL),
_ => Err(SQLiteError::msg(
"Invalid payload for FileFormatReadVersion",
)),
}
}
}