sqlite_rs/header/reserved_bytes_per_page.rs
1use crate::traits::{Name, ParseBytes};
2use crate::{field_parsing_error, impl_name, result::SqliteResult};
3
4use core::ops::Deref;
5
6/// # Reserved bytes per page (1 Byte)
7///
8/// Sqlite has the ability to set aside a small number of extra bytes at the
9/// end of every page for use by extensions. These extra bytes are used, for
10/// example, by the Sqlite Encryption Extension to store a nonce and/or
11/// cryptographic checksum associated with each page. The "reserved space" size
12/// in the 1-byte integer at offset 20 is the number of bytes of space at the
13/// end of each page to reserve for extensions. **This value is usually 0.** *The
14/// value can be odd.*
15///
16/// The **"usable size"** of a database page is the page size specified by the
17/// 2-byte integer at offset 16 in the header less the "reserved" space size
18/// recorded in the 1-byte integer at offset 20 in the header. The usable size
19/// of a page might be an odd number.
20///
21/// However, *the usable size is not allowed to be less than `480`*. In other words, if the page size is 512, then the
22/// reserved space size cannot exceed 32.
23///
24/// "This value is usually 0."
25///
26/// Reference: https://www.sqlite.org/fileformat2.html#resbyte
27#[derive(Debug, Default, PartialEq, Eq)]
28pub struct ReservedBytesPerPage(u8);
29impl Deref for ReservedBytesPerPage {
30 type Target = u8;
31
32 fn deref(&self) -> &Self::Target {
33 &self.0
34 }
35}
36
37impl_name! {ReservedBytesPerPage}
38
39impl ParseBytes for ReservedBytesPerPage {
40 const LENGTH_BYTES: usize = 1;
41
42 fn parsing_handler(bytes: &[u8]) -> SqliteResult<Self> {
43 let reserved_bytes_per_page = *bytes
44 .first()
45 .ok_or(field_parsing_error! {Self::NAME.into()})?;
46
47 Ok(Self(reserved_bytes_per_page))
48 }
49}