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
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
//! Item key encoding/decoding.

use crate::alloc_prelude::*;
use nano_leb128::ULEB128;

// Key type bits in the item control byte:
// (largely copied from b3/item_header.py)
//
// +------------+------------+
// | key type   | key type   |
// +------------+------------+
//   0     0  (0x0)  no key
//   0     1  (0x4)  UVarInt
//   1     0  (0x8)  UTF8 bytes
//   1     1  (0xC)  raw bytes

/// An item key.
#[derive(Debug, Clone, PartialEq)]
pub enum ItemKey {
    /// No key is set for this item.
    NoKey,

    /// The key is a `UVarInt` (unsigned LEB128).
    IntegerKey(u64),

    /// The key is a UTF-8 encoded string.
    ///
    /// The key is encoded as the length of the UTF-8 string in bytes as a
    /// `UVarInt` (unsigned LEB128), and then the bytes of the string.
    StringKey(String),

    /// The key is a byte array.
    ///
    /// The key is encoded as the length of the byte array as a `UVarInt`
    /// (unsigned LEB128), and then the bytes.
    BytesKey(Vec<u8>),
}

impl ItemKey {
    /// Return the bits that would be set in the item control byte for the
    /// key type.
    pub fn type_bits(&self) -> u8 {
        match self {
            Self::NoKey => 0b00000000,
            Self::IntegerKey(_) => 0b00010000,
            Self::StringKey(_) => 0b00100000,
            Self::BytesKey(_) => 0b00110000,
        }
    }

    /// Encode the key into it's byte representation.
    pub fn encode(&self) -> Result<Vec<u8>, crate::Error> {
        let mut out: Vec<u8> = Vec::new();
        let mut len_bytes = [0u8; 10];

        match self {
            // Empty vector if there's no key
            Self::NoKey => {}

            // Unsigned LEB128 for an integer key
            Self::IntegerKey(i) => {
                let count = ULEB128::from(*i).write_into(&mut len_bytes)?;
                out.extend(&len_bytes[0..count]);
            }

            // String key - unsigned LEB128 of byte length, then the bytes
            Self::StringKey(s) => {
                let b = s.as_bytes();
                let count = ULEB128::from(b.len() as u64).write_into(&mut len_bytes)?;
                out.extend(&len_bytes[0..count]);
                out.extend(b);
            }

            // Byte key - unsigned LEB128 of length, then the bytes
            Self::BytesKey(b) => {
                let count = ULEB128::from(b.len() as u64).write_into(&mut len_bytes)?;
                out.extend(&len_bytes[0..count]);
                out.extend(b);
            }
        }

        Ok(out)
    }

    pub fn decode(base: u8, ext: &[u8]) -> Result<(Self, usize), crate::Error> {
        match base & 0b00110000 {
            // No key
            0b00000000 => Ok((Self::NoKey, 0)),

            // Integer key
            0b00010000 => {
                let (val, len) = ULEB128::read_from(&ext)?;
                let val = u64::from(val);

                Ok((Self::IntegerKey(val), len))
            }

            // String key
            0b00100000 => {
                let (val, len) = ULEB128::read_from(&ext)?;
                let val = u64::from(val);
                let total = (val as usize) + len;

                let s = String::from_utf8(Vec::from(&ext[len..total]))?;
                Ok((Self::StringKey(s), total))
            }

            // Bytes key
            0b00110000 => {
                let (val, len) = ULEB128::read_from(&ext)?;
                let val = u64::from(val);
                let total = (val as usize) + len;

                let b = Vec::from(&ext[len..total]);
                Ok((Self::BytesKey(b), total))
            }

            _ => unreachable!(),
        }
    }
}

impl From<u64> for ItemKey {
    fn from(i: u64) -> ItemKey {
        ItemKey::IntegerKey(i)
    }
}

impl From<&str> for ItemKey {
    fn from(s: &str) -> ItemKey {
        ItemKey::StringKey(String::from(s))
    }
}

impl From<String> for ItemKey {
    fn from(s: String) -> ItemKey {
        ItemKey::StringKey(s)
    }
}

impl From<Vec<u8>> for ItemKey {
    fn from(v: Vec<u8>) -> ItemKey {
        ItemKey::BytesKey(v)
    }
}

impl From<&[u8]> for ItemKey {
    fn from(v: &[u8]) -> ItemKey {
        ItemKey::BytesKey(Vec::from(v))
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn encode_key_empty() {
        let key = ItemKey::NoKey;
        assert_eq!(Vec::<u8>::new(), key.encode().unwrap())
    }

    #[test]
    fn encode_key_integer() {
        let key = ItemKey::IntegerKey(624485);
        assert_eq!(vec![0xE5, 0x8E, 0x26], key.encode().unwrap())
    }

    #[test]
    fn encode_key_string() {
        let key = ItemKey::StringKey(String::from("A"));
        assert_eq!(vec![0x01, 0x41], key.encode().unwrap())
    }

    #[test]
    fn encode_key_bytes() {
        let key = ItemKey::BytesKey(vec![0xC0, 0xFF, 0xEE]);
        assert_eq!(vec![0x03, 0xC0, 0xFF, 0xEE], key.encode().unwrap())
    }

    #[test]
    fn decode_key_empty() {
        let (key, len) = ItemKey::decode(0b00000000, &[]).unwrap();
        assert_eq!(key, ItemKey::NoKey);
        assert_eq!(len, 0);
    }

    #[test]
    fn decode_key_integer() {
        let (key, len) = ItemKey::decode(0b00010000, &[0x01, 0x00]).unwrap();
        assert_eq!(key, ItemKey::IntegerKey(1));
        assert_eq!(len, 1);

        let (key, len) = ItemKey::decode(0b00010000, &[0xE5, 0x8E, 0x26, 0x00]).unwrap();
        assert_eq!(key, ItemKey::IntegerKey(624485));
        assert_eq!(len, 3);
    }

    #[test]
    fn decode_key_string() {
        let (key, len) = ItemKey::decode(0b00100000, &[0x03, 0x41, 0x41, 0x41]).unwrap();
        assert_eq!(key, ItemKey::StringKey(String::from("AAA")));
        assert_eq!(len, 4);
    }

    #[test]
    fn decode_key_bytes() {
        let (key, len) = ItemKey::decode(0b00110000, &[0x03, 0xC0, 0xFF, 0xEE]).unwrap();
        assert_eq!(key, ItemKey::BytesKey(vec![0xC0, 0xFF, 0xEE]));
        assert_eq!(len, 4);
    }
}