1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
use crate::traits::ParseBytes;
use crate::{impl_name, result::SQLiteResult};
use core::ops::Deref;

/// Incremental vacuum settings (8 Bytes)
///
///  The two 4-byte big-endian integers at offsets 52 and 64 are used to manage
/// the auto_vacuum and incremental_vacuum modes. If the integer at offset 52
/// is zero then pointer-map (ptrmap) pages are omitted from the database file
/// and neither auto_vacuum nor incremental_vacuum are supported. If the integer
/// at offset 52 is non-zero then it is the page number of the largest root page
/// in the database file, the database file will contain ptrmap pages, and the
/// mode must be either auto_vacuum or incremental_vacuum. In this latter case,
/// the integer at offset 64 is true for incremental_vacuum and false for
/// auto_vacuum. If the integer at offset 52 is zero then the integer at
/// offset 64 must also be zero.
#[derive(Debug, PartialEq, Eq)]
pub struct IncrementalVacuumSettings {
  pub largest_root_btree_page: LargestRootBtreePage,
  pub incremental_vacuum_mode: IncrementalVacuumMode,
}

// TODO:  If the integer at offset 52 is non-zero then it is the page number of
// TODO: the largest root page in the database file, the database file will
// TODO: contain ptrmap pages, and the mode must be either auto_vacuum or
// TODO: incremental_vacuum.

impl IncrementalVacuumSettings {
  pub fn largest_root_btree_page(&self) -> &LargestRootBtreePage {
    &self.largest_root_btree_page
  }

  pub fn incremental_vacuum_mode(&self) -> &IncrementalVacuumMode {
    &self.incremental_vacuum_mode
  }
}

///  #  Largest root b-tree page (4 Bytes)
/// The page number of the largest root b-tree page when in auto-vacuum
/// or incremental-vacuum modes, or zero otherwise.
#[derive(Debug, PartialEq, Eq)]
pub struct LargestRootBtreePage(u32);

impl Deref for LargestRootBtreePage {
  type Target = u32;

  fn deref(&self) -> &Self::Target {
    &self.0
  }
}

impl_name! {LargestRootBtreePage}

impl ParseBytes for LargestRootBtreePage {
  const LENGTH_BYTES: usize = 4;

  fn parsing_handler(bytes: &[u8]) -> SQLiteResult<Self> {
    let buf: [u8; Self::LENGTH_BYTES] = bytes.try_into()?;

    let value = u32::from_be_bytes(buf);

    Ok(Self(value))
  }
}

/// # Incremental-vacuum mode (4 Bytes)
/// True (non-zero) for incremental-vacuum mode. False (zero) otherwise.
#[derive(Debug, PartialEq, Eq)]
pub enum IncrementalVacuumMode {
  True,
  False,
}
impl From<&IncrementalVacuumMode> for bool {
  fn from(value: &IncrementalVacuumMode) -> Self {
    match value {
      IncrementalVacuumMode::True => true,
      IncrementalVacuumMode::False => false,
    }
  }
}
impl From<&IncrementalVacuumMode> for u32 {
  fn from(value: &IncrementalVacuumMode) -> Self {
    match value {
      IncrementalVacuumMode::True => 1,
      IncrementalVacuumMode::False => 0,
    }
  }
}

impl_name! {IncrementalVacuumMode}

impl ParseBytes for IncrementalVacuumMode {
  const LENGTH_BYTES: usize = 4;

  fn parsing_handler(bytes: &[u8]) -> SQLiteResult<Self> {
    let buf: [u8; Self::LENGTH_BYTES] = bytes.try_into()?;

    let number = u32::from_be_bytes(buf);
    let value = if number > 0 { Self::True } else { Self::False };

    Ok(value)
  }
}