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
use crate::store::Store;
use crc32fast::hash;
use osrs_bytes::WriteExt;
use thiserror::Error;

#[derive(Error, Debug)]
pub enum ChecksumTableError {
    #[error("io error: {0}")]
    Io(#[from] std::io::Error),
    #[error("store error: {0}")]
    Store(#[from] crate::store::StoreError),
}

pub struct ChecksumTable {
    entries: Vec<u32>,
}

impl ChecksumTable {
    pub fn write(&self) -> Result<(), ChecksumTableError> {
        let mut buf = Vec::new();

        for entry in &self.entries {
            buf.write_u32(*entry)?;
        }

        let mut checksum: u32 = 1234;
        for entry in &self.entries {
            checksum = (checksum << 1).wrapping_add(*entry)
        }

        Ok(())
    }

    pub fn create(store: Box<dyn Store>) -> Result<ChecksumTable, ChecksumTableError> {
        let mut entries = Vec::new();
        let mut next_archive = 0;

        for archive in store.list(0)? {
            let entry = hash(&store.read(0, archive)?);

            for _ in next_archive..archive {
                //entries.push(0);
            }

            entries.push(entry);
            next_archive = archive + 1;
        }

        Ok(ChecksumTable { entries })
    }
}