1use crate::error::Error;
2use crate::page_layout::PTR_SIZE;
3use std::cmp::{Eq, Ord, Ordering, PartialOrd};
4use std::convert::From;
5use std::convert::TryFrom;
6
7#[derive(Clone, Eq, PartialEq, Debug)]
8pub struct Offset(pub usize);
9
10impl TryFrom<[u8; PTR_SIZE]> for Offset {
12 type Error = Error;
13
14 fn try_from(arr: [u8; PTR_SIZE]) -> Result<Self, Self::Error> {
15 Ok(Offset(usize::from_be_bytes(arr)))
16 }
17}
18
19#[derive(Clone, Eq, PartialEq, PartialOrd, Ord, Debug)]
20pub struct Key(pub String);
21
22#[derive(Clone, Eq, Debug)]
23pub struct KeyValuePair {
24 pub key: String,
25 pub value: String,
26}
27
28impl Ord for KeyValuePair {
29 fn cmp(&self, other: &Self) -> Ordering {
30 self.key.cmp(&other.key)
31 }
32}
33
34impl PartialOrd for KeyValuePair {
35 fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
36 Some(self.cmp(other))
37 }
38}
39
40impl PartialEq for KeyValuePair {
41 fn eq(&self, other: &Self) -> bool {
42 self.key == other.key && self.value == other.value
43 }
44}
45
46impl KeyValuePair {
47 pub fn new(key: String, value: String) -> KeyValuePair {
48 KeyValuePair { key, value }
49 }
50}
51
52#[derive(PartialEq, Eq, Clone, Debug)]
54pub enum NodeType {
55 Internal(Vec<Offset>, Vec<Key>),
57
58 Leaf(Vec<KeyValuePair>),
60
61 Unexpected,
62}
63
64impl From<u8> for NodeType {
66 fn from(orig: u8) -> NodeType {
67 match orig {
68 0x01 => NodeType::Internal(Vec::<Offset>::new(), Vec::<Key>::new()),
69 0x02 => NodeType::Leaf(Vec::<KeyValuePair>::new()),
70 _ => NodeType::Unexpected,
71 }
72 }
73}
74
75impl From<&NodeType> for u8 {
77 fn from(orig: &NodeType) -> u8 {
78 match orig {
79 NodeType::Internal(_, _) => 0x01,
80 NodeType::Leaf(_) => 0x02,
81 NodeType::Unexpected => 0x03,
82 }
83 }
84}