coven_format/
floor_pointer.rs1use crate::error::{exact_length, Error};
8use crate::path::ObjectPath;
9use crate::value::Slot;
10use crate::wire::{decode_frame_value, wire_struct, Decoder, Encoder, Wire};
11use coven_foundation::id_source::{DeviceId, KeyId, StoreId};
12
13#[derive(Clone, Copy, Debug, PartialEq, Eq)]
16pub struct FloorPointerPrefix {
17 pub key: KeyId,
19 pub first: Slot,
21 pub device: DeviceId,
23 pub number: Slot,
25 pub body: [u8; 32],
27}
28
29impl FloorPointerPrefix {
30 pub const LENGTH: usize = 3 + 16 + 8 + 8 + 8 + 32;
32
33 pub fn encode(&self) -> Result<Vec<u8>, Error> {
35 let mut out = Encoder::new();
36 out.bytes(&crate::FormatVersion::CURRENT.prefix(crate::kinds::FLOOR_POINTER))?;
37 self.key.put(&mut out)?;
38 self.first.put(&mut out)?;
39 self.device.put(&mut out)?;
40 self.number.put(&mut out)?;
41 self.body.put(&mut out)?;
42 Ok(out.bytes)
43 }
44
45 pub fn decode(bytes: &[u8]) -> Result<Self, Error> {
47 crate::sealed::prefix(bytes, crate::kinds::FLOOR_POINTER)?;
48 exact_length(bytes.len(), Self::LENGTH)?;
49 let mut input = Decoder::new(&bytes[3..])?;
50 let prefix = Self {
51 key: KeyId::get(&mut input)?,
52 first: <Slot as Wire>::get(&mut input)?,
53 device: DeviceId::get(&mut input)?,
54 number: <Slot as Wire>::get(&mut input)?,
55 body: <[u8; 32]>::get(&mut input)?,
56 };
57 input.finish()?;
58 Ok(prefix)
59 }
60
61 pub fn body_path(&self, store: StoreId) -> ObjectPath {
63 ObjectPath::floor_body(store, self.device, self.number.into())
64 }
65}
66
67#[derive(Clone, Copy, Debug, PartialEq, Eq)]
70pub struct FloorPointerHeader {
71 pub author: DeviceId,
73 pub at: u64,
75}
76wire_struct!(FloorPointerHeader, author, at);
77
78impl FloorPointerHeader {
79 pub fn encode(&self) -> Result<Vec<u8>, Error> {
81 crate::encode_frame(crate::kinds::FLOOR_POINTER_HEADER, self)
82 }
83 pub fn decode(bytes: &[u8]) -> Result<Self, Error> {
85 decode_frame_value(bytes, crate::kinds::FLOOR_POINTER_HEADER, "floor pointer header")
86 }
87}
88
89#[cfg(test)]
90#[path = "floor_pointer_tests.rs"]
91mod tests;