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
use std::ops::Deref;

use anyhow::bail;

use crate::result::SQLiteError;

/// # Page Size (2 Bytes)
///  The two-byte value beginning at offset 16 determines the page size of the
/// database. For SQLite versions 3.7.0.1 (2010-08-04) and earlier, this value
/// is interpreted as a big-endian integer and must be a power of two between
/// 512 and 32768, inclusive. Beginning with SQLite version 3.7.1 (2010-08-23),
/// a page size of 65536 bytes is supported. The value 65536 will not fit in a
/// two-byte integer, so to specify a 65536-byte page size, the value at offset
/// 16 is 0x00 0x01. This value can be interpreted as a big-endian 1 and
/// thought of as a magic number to represent the 65536 page size. Or one can
/// view the two-byte field as a little endian number and say that it
/// represents the page size divided by 256. These two interpretations of the
/// page-size field are equivalent.
#[derive(Debug)]
pub struct PageSize(u32);
impl PageSize {
    pub fn get(&self) -> u32 {
        self.0
    }
}
impl Deref for PageSize {
    type Target=u32;

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

impl<'a> TryFrom<&'a [u8]> for PageSize {
    type Error = SQLiteError;

    fn try_from(payload: &'a [u8]) -> Result<Self, Self::Error> {
        use std::ops::Not;

        const VALID_SIZE: usize = 2;

        if payload.len() != VALID_SIZE {
            bail!("Invalid size for MagicHeaderString");
        }

        let buf: [u8; 2] = payload.try_into()?;

        let page_size = u16::from_be_bytes(buf);

        if page_size == 1 {
            Ok(Self(65_536))
        } else {
            if page_size < 512 {
                bail!("Page size [{page_size}] can't be less than 512");
            }
            if page_size.is_power_of_two().not() {
                bail!("Page size must be power of two");
            }

            Ok(Self(page_size.into()))
        }
    }
}