1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
use crate::{
    binary::Node,
    common::{Bytes32, Position},
};

pub type Primitive = (u64, Bytes32);

pub trait PrimitiveView {
    fn position(&self) -> Position;
    fn hash(&self) -> &Bytes32;
}

impl PrimitiveView for Primitive {
    fn position(&self) -> Position {
        Position::from_in_order_index(self.0)
    }

    fn hash(&self) -> &Bytes32 {
        &self.1
    }
}

impl From<&Node> for Primitive {
    fn from(node: &Node) -> Self {
        (node.position().in_order_index(), *node.hash())
    }
}

impl From<Primitive> for Node {
    fn from(primitive: Primitive) -> Self {
        let position = primitive.position();
        let hash = *primitive.hash();
        Node::new(position, hash)
    }
}