basic/
basic.rs

1fn main() {
2    // We define a set of data to encode.  The `.as_bytes().to_vec() part converts the str to a Vec<u8>.
3    // The type of `data` is Vec<(u8, Vec<u8>)>, which looks complicated.  In reality, it can usually be treated as a key-value list of u8 keys and String values.
4    let data = vec![
5        (1, "Hello".as_bytes().to_vec()),
6        (2, ", ".as_bytes().to_vec()),
7        (4, "world".as_bytes().to_vec()),
8        (1, "!".as_bytes().to_vec()),
9    ];
10
11    // We encode the data using kvds.  The data is now stored in a compact format, of type Vec<u8>.
12    // We unwrap the Result and panic!() on an Err.  In reality, you should not panic!().
13    let encoded_data = match kvds::encode(data) {
14        Ok(d) => d,
15        Err(e) => panic!("Err: {:?}", e),
16    };
17
18    // We print the encoded data -- it should print as a vector of u8.
19    println!("{:?}", encoded_data);
20
21    // We decode the data using kvds.  It should now be back to a Vec<(u8, Vec<u8>)>.
22    let decoded_data = match kvds::decode(encoded_data) {
23        Ok(d) => d,
24        Err(e) => panic!("Err: {:?}", e),
25    };
26
27    // We print it.  It should look like a Vec<(u8, Vec<u8>)>, same as the original `data`!
28    println!("{:?}", decoded_data);
29}