celestia_types/share/
info_byte.rs1use crate::consts::appconsts;
2use crate::{Error, Result};
3
4#[repr(transparent)]
14#[derive(Debug, PartialEq)]
15pub struct InfoByte(u8);
16
17impl InfoByte {
18 pub fn new(version: u8, is_sequence_start: bool) -> Result<Self> {
20 if version > appconsts::MAX_SHARE_VERSION {
21 Err(Error::MaxShareVersionExceeded(version))
22 } else {
23 let prefix = version << 1;
24 let sequence_start = if is_sequence_start { 1 } else { 0 };
25 Ok(Self(prefix + sequence_start))
26 }
27 }
28
29 pub fn version(&self) -> u8 {
31 self.0 >> 1
32 }
33
34 pub fn is_sequence_start(&self) -> bool {
36 self.0 % 2 == 1
37 }
38
39 pub fn as_u8(&self) -> u8 {
41 self.0
42 }
43
44 pub(crate) fn from_raw(byte: u8) -> Result<Self> {
45 let version = byte >> 1;
46 if version > appconsts::MAX_SHARE_VERSION {
47 Err(Error::MaxShareVersionExceeded(version))
48 } else {
49 Ok(InfoByte(byte))
50 }
51 }
52
53 pub(crate) fn from_raw_unchecked(byte: u8) -> Self {
54 InfoByte(byte)
55 }
56}