prikk_object/payload/
node.rs1use prikk_error::{PrikkError, Result};
4
5use crate::payload::blob::BlobKind;
6
7pub const NODE_ID_BYTES: usize = 32;
9
10#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
13pub struct NodeId([u8; NODE_ID_BYTES]);
14
15impl NodeId {
16 #[must_use]
20 pub const fn from_bytes(bytes: [u8; NODE_ID_BYTES]) -> Self {
21 Self(bytes)
22 }
23
24 pub fn try_from_bytes(bytes: [u8; NODE_ID_BYTES]) -> Result<Self> {
27 if bytes == [0_u8; NODE_ID_BYTES] {
28 return Err(PrikkError::MalformedData(
29 "node_id must be nonzero".to_string(),
30 ));
31 }
32 Ok(Self(bytes))
33 }
34
35 #[must_use]
37 pub const fn as_bytes(&self) -> &[u8; NODE_ID_BYTES] {
38 &self.0
39 }
40
41 #[must_use]
44 pub fn is_zero(&self) -> bool {
45 self.0 == [0_u8; NODE_ID_BYTES]
46 }
47}
48
49#[derive(Debug, Clone, Copy, PartialEq, Eq)]
52#[repr(u16)]
53pub enum NodeKind {
54 TextFile = 0x0001,
56 BinaryFile = 0x0002,
58 Symlink = 0x0003,
60}
61
62impl NodeKind {
63 #[must_use]
65 pub const fn code(self) -> u16 {
66 self as u16
67 }
68
69 pub fn from_code(code: u16) -> Result<Self> {
71 match code {
72 0x0001 => Ok(Self::TextFile),
73 0x0002 => Ok(Self::BinaryFile),
74 0x0003 => Ok(Self::Symlink),
75 other => Err(PrikkError::MalformedData(format!(
76 "unknown or reserved node_kind code: {other:#06x}"
77 ))),
78 }
79 }
80
81 pub fn from_file_blob_kind(blob_kind: BlobKind) -> Result<Self> {
84 match blob_kind {
85 BlobKind::Text => Ok(Self::TextFile),
86 BlobKind::Binary => Ok(Self::BinaryFile),
87 BlobKind::Snapshot => Err(PrikkError::MalformedData(
88 "file node must not reference a SNAPSHOT blob".to_string(),
89 )),
90 }
91 }
92}