sqlite_rs/header/
version_valid_for.rs

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