sqlite_rs/header/
magic_header_string.rs

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