sq3_rs/file_header/
version_valid_for.rs

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