Function encode

Source
pub fn encode(slice: Vec<(u8, Vec<u8>)>) -> Result<Vec<u8>, EncodeError>
Expand description

Encodes data!

§Example

let data = vec![
    (1, "one".as_bytes().to_vec()),
    (2, "two".as_bytes().to_vec()),
];

let encoded = kvds::encode(data)?;

assert_eq!(encoded, vec![1, 0, 3, 111, 110, 101, 2, 0, 3, 116, 119, 111]);

§Errors

This function returns an error if any of the values supplied are too long.

Examples found in repository?
examples/basic.rs (line 13)
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}
More examples
Hide additional examples
examples/files_old.rs (line 14)
4fn main() {
5    // Our Data:
6    let data = vec![
7        (1, "Hello".as_bytes().to_vec()),
8        (2, ", ".as_bytes().to_vec()),
9        (4, "world".as_bytes().to_vec()),
10        (1, "!".as_bytes().to_vec()),
11    ];
12
13    // Note that one should not panic!() in real life.
14    let encoded_data = match kvds::encode(data) {
15        Ok(d) => d,
16        Err(e) => panic!("Err: {:?}", e),
17    };
18
19    // Create the file...
20    // One could also open a file using File::open().
21    let mut file = match File::create("data") {
22        Ok(f) => f,
23        Err(e) => panic!("Err: {:?}", e), // Again, DO NOT PANIC!() IRL.
24    };
25
26    // We write the data, of type Vec<u8>, to the file.
27    match file.write_all(&encoded_data) {
28        Err(e) => panic!("Err: {:?}", e), // Do something other than panic!()
29        _ => (),
30    }
31
32    // Open the file.
33    let mut file = match File::open("data") {
34        Ok(f) => f,
35        Err(e) => panic!("Err: {:?}", e), // You know.
36    };
37
38    // We read the data back out, to the variable `buffer`.
39    let mut buffer = Vec::<u8>::new();
40    match file.read_to_end(&mut buffer) {
41        Err(e) => panic!("Err: {:?}", e), // ^^
42        _ => (),
43    }
44
45    // Decode the data.
46    let decoded_data = match kvds::decode(buffer) {
47        Ok(d) => d,
48        Err(e) => panic!("Err: {:?}", e),
49    };
50
51    // We print it.  It should look like a Vec<(u8, Vec<u8>)>, same as the original `data`!
52    println!("{:?}", decoded_data);
53}