use super::ParseBytes;
use crate::result::SQLiteError;
use alloc::format;
use core::fmt::Debug;
const SQLITE3_FILE_FORMAT_MAGIC_STRING: [u8; 16] = [
0x53, 0x51, 0x4c, 0x69, 0x74, 0x65, 0x20, 0x66, 0x6f, 0x72, 0x6d, 0x61, 0x74,
0x20, 0x33, 0x00,
];
pub struct MagicHeaderString([u8; 16]);
impl MagicHeaderString {
pub fn get(&self) -> [u8; 16] {
self.0
}
}
impl Debug for MagicHeaderString {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
let output = format!("{:02x?}", self.0);
f.debug_tuple("MagicHeaderString").field(&output).finish()
}
}
impl<'a> ParseBytes<&[u8]> for MagicHeaderString {
fn struct_name() -> &'static str {
"MagicHeaderString"
}
fn bytes_length() -> usize {
16
}
fn parsing_handler(bytes: &[u8]) -> crate::result::SQLiteResult<Self> {
for (idx, byte) in SQLITE3_FILE_FORMAT_MAGIC_STRING.iter().enumerate() {
if bytes.get(idx) != Some(byte) {
return Err(SQLiteError::Custom(format!(
"Invalid payload for {}",
Self::struct_name()
)));
}
}
Ok(Self(SQLITE3_FILE_FORMAT_MAGIC_STRING))
}
}