use crate::consts::appconsts;
use crate::{Error, Result};
#[cfg(feature = "uniffi")]
use crate::error::UniffiResult;
#[repr(transparent)]
#[derive(Debug, PartialEq)]
#[cfg_attr(feature = "uniffi", derive(uniffi::Object))]
pub struct InfoByte(u8);
impl InfoByte {
pub fn new(version: u8, is_sequence_start: bool) -> Result<Self> {
if version > appconsts::MAX_SHARE_VERSION {
Err(Error::MaxShareVersionExceeded(version))
} else {
let prefix = version << 1;
let sequence_start = if is_sequence_start { 1 } else { 0 };
Ok(Self(prefix + sequence_start))
}
}
pub fn version(&self) -> u8 {
self.0 >> 1
}
pub fn is_sequence_start(&self) -> bool {
self.0 % 2 == 1
}
pub fn as_u8(&self) -> u8 {
self.0
}
pub(crate) fn from_raw(byte: u8) -> Result<Self> {
let version = byte >> 1;
if version > appconsts::MAX_SHARE_VERSION {
Err(Error::MaxShareVersionExceeded(version))
} else {
Ok(InfoByte(byte))
}
}
pub(crate) fn from_raw_unchecked(byte: u8) -> Self {
InfoByte(byte)
}
}
#[cfg(feature = "uniffi")]
#[uniffi::export]
impl InfoByte {
#[uniffi::constructor(name = "create")]
pub fn uniffi_new(version: u8, is_sequence_start: bool) -> UniffiResult<Self> {
Ok(InfoByte::new(version, is_sequence_start)?)
}
#[uniffi::method(name = "version")]
pub fn uniffi_version(&self) -> u8 {
self.version()
}
#[uniffi::method(name = "is_sequence_start")]
pub fn uniffi_is_sequence_start(&self) -> bool {
self.is_sequence_start()
}
#[uniffi::method(name = "as_u8")]
pub fn uniffi_as_u8(&self) -> u8 {
self.as_u8()
}
}