Skip to main content

oxabl_cache_format/
pack_header.rs

1//! `OXPK` pack header.
2
3use crate::cursor::Cursor;
4use crate::error::Result;
5use crate::frame::{read_header, write_header};
6use crate::ids::CacheId;
7use crate::writer::Writer;
8
9const MAGIC: [u8; 4] = *b"OXPK";
10
11/// Length of the pack header; the first segment starts here.
12pub const PACK_HEADER_LEN: usize = 24;
13
14/// The header of a pack object.
15#[derive(Debug, Clone, Copy, PartialEq, Eq)]
16pub struct PackHeader {
17    pub cache_id: CacheId,
18}
19
20impl PackHeader {
21    pub fn encode(&self) -> [u8; PACK_HEADER_LEN] {
22        let mut w = Writer::with_capacity(PACK_HEADER_LEN);
23        write_header(&mut w, MAGIC, 0);
24        w.u16(0);
25        w.bytes(&self.cache_id.0);
26        let mut out = [0u8; PACK_HEADER_LEN];
27        out.copy_from_slice(&w.into_vec());
28        out
29    }
30
31    pub fn decode(bytes: &[u8]) -> Result<Self> {
32        let mut c = Cursor::new(bytes);
33        read_header(&mut c, MAGIC, 0)?;
34        c.reserved_u16()?;
35        let cache_id = CacheId(c.array()?);
36        c.finish()?;
37        Ok(Self { cache_id })
38    }
39}