sqlite_rs/header/write_library_version.rs
1use crate::traits::ParseBytes;
2use crate::VERSION_NUMBER;
3use crate::{impl_name, result::SqliteResult};
4use core::ops::Deref;
5
6/// # Write library version number (4 Bytes)
7///
8/// The 4-byte big-endian integer at offset 96 stores the SQLITE_VERSION_NUMBER
9/// value for the Sqlite library that most recently modified the database file.
10///
11/// > The entries at offsets 92 and 96 were added in later version of the
12/// > SQLite library.
13/// >
14/// > When an older version modifies the file, it will change the change
15/// > counter (offset 24), but not adjust the version-valid-for number or the
16/// > write library version number. So the library version number is no longer
17/// > correct, because a different version last wrote to the file.
18/// >
19/// > The version-valid-for number allows a new library to detect this case: if
20/// > the change counter and the version-valid-for number do not match, then the
21/// > write library version number is outdated, and must be ignored.
22/// >
23/// > **Reference:** https://stackoverflow.com/a/45420823
24#[derive(Debug)]
25pub struct WriteLibraryVersion(u32);
26impl Default for WriteLibraryVersion {
27 fn default() -> Self {
28 Self(*VERSION_NUMBER.get().unwrap_or(&0))
29 }
30}
31impl Deref for WriteLibraryVersion {
32 type Target = u32;
33
34 fn deref(&self) -> &Self::Target {
35 &self.0
36 }
37}
38
39impl_name! {WriteLibraryVersion}
40
41impl ParseBytes for WriteLibraryVersion {
42 const LENGTH_BYTES: usize = 4;
43
44 fn parsing_handler(bytes: &[u8]) -> SqliteResult<Self> {
45 let buf: [u8; Self::LENGTH_BYTES] = bytes.try_into()?;
46
47 let database_size = u32::from_be_bytes(buf);
48
49 Ok(Self(database_size))
50 }
51}