sq3_rs/file_header/
magic_header_string.rs

1use std::fmt::Debug;
2
3use sq3_derive::Name;
4use sq3_parser::TypeName;
5
6use crate::{field_parsing_error, result::SqliteResult, traits::ParseBytes};
7
8const SQLITE3_FILE_FORMAT_MAGIC_STRING: [u8; 16] = [
9    0x53, 0x51, 0x4c, 0x69, 0x74, 0x65, 0x20, 0x66, 0x6f, 0x72, 0x6d, 0x61, 0x74, 0x20, 0x33, 0x00,
10];
11
12/// # Magic Header String (16 Bytes)
13///
14///  Every valid Sqlite database file begins with the following
15/// 16 bytes (in hex): `53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00`.
16/// This byte sequence corresponds to the UTF-8 string `Sqlite format 3`
17/// including the nul terminator character at the end.
18
19#[derive(Name)]
20pub struct MagicHeaderString([u8; 16]);
21impl Default for MagicHeaderString {
22    fn default() -> Self {
23        Self(SQLITE3_FILE_FORMAT_MAGIC_STRING)
24    }
25}
26
27impl Debug for MagicHeaderString {
28    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
29        f.debug_tuple(Self::NAME).finish()
30    }
31}
32
33impl ParseBytes for MagicHeaderString {
34    const LENGTH_BYTES: usize = 16;
35
36    fn parsing_handler(bytes: &[u8]) -> SqliteResult<Self> {
37        for (idx, byte) in SQLITE3_FILE_FORMAT_MAGIC_STRING.iter().enumerate() {
38            if bytes.get(idx) != Some(byte) {
39                return Err(field_parsing_error! {Self::NAME.into()});
40            }
41        }
42
43        Ok(Self(SQLITE3_FILE_FORMAT_MAGIC_STRING))
44    }
45}