use crate::{
common::{error::DeserializeError, Bytes32, Prefix, PrefixError},
sparse::Node,
};
pub type Primitive = (u32, u8, Bytes32, Bytes32);
trait PrimitiveView {
fn height(&self) -> u32;
fn prefix(&self) -> Result<Prefix, PrefixError>;
fn bytes_lo(&self) -> &Bytes32;
fn bytes_hi(&self) -> &Bytes32;
}
impl PrimitiveView for Primitive {
fn height(&self) -> u32 {
self.0
}
fn prefix(&self) -> Result<Prefix, PrefixError> {
Prefix::try_from(self.1)
}
fn bytes_lo(&self) -> &Bytes32 {
&self.2
}
fn bytes_hi(&self) -> &Bytes32 {
&self.3
}
}
impl From<&Node> for Primitive {
fn from(node: &Node) -> Self {
(node.height(), node.prefix() as u8, *node.bytes_lo(), *node.bytes_hi())
}
}
impl TryFrom<Primitive> for Node {
type Error = DeserializeError;
fn try_from(primitive: Primitive) -> Result<Self, Self::Error> {
let height = primitive.height();
let prefix = primitive.prefix()?;
let bytes_lo = *primitive.bytes_lo();
let bytes_hi = *primitive.bytes_hi();
let node = Self::new(height, prefix, bytes_lo, bytes_hi);
Ok(node)
}
}