strkey

Crates.iostrkey
lib.rsstrkey
version0.1.0
sourcesrc
created_at2021-05-09 02:53:25.184968
updated_at2021-05-09 02:53:25.184968
descriptionSerialization of values into a human-readable encoding that preserves lexicographic sort order.
homepage
repositoryhttps://github.com/chfoo/strkey/
max_upload_size
id395070
size73,993
Christopher Foo (chfoo)

documentation

README

strkey

strkey is Rust library for serialization of values into an human-readable encoding that preserves lexicographic sort order. The encoded format is useful for key-value stores/databases that maintains keys in sorted order.

Crates.io docs.rs

This crate is similar to bytekey/bytekey-fix/bytekey2 except those this crate encodes into a human-readable, UTF-8 encoded string.

Most data types in the Serde data model are supported. The encoding consists of each encoded value separated by a deliminator (a colon : by default). Integers, floats, and byte arrays are encoded in hexadecimal while strings are left as is in UTF-8. Full details are listed in the documentation in the ser module. Note that the encoding is not self-describing, that is, types are not encoded within the format.

Quick start

Remember to add strkey to your Cargo.toml first.

let serialized = strkey::to_vec(&("account", 1234u32))?;

assert_eq!(&serialized, b"account:000004d2");

let deserialized = strkey::from_slice::<(&str, u32)>(&serialized)?;

assert_eq!(deserialized.0, "account");
assert_eq!(deserialized.1, 1234);

More complicated schemas can be modelled: (Enable the derive feature in serde if needed.)

use serde::{Serialize, Deserialize};

#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
struct AccountId {
    domain: String,
    user_id: u32,
}

let serialized = strkey::to_vec(&(
    "account",
    AccountId {
        domain: "abc".to_string(),
        user_id: 1234,
    },
))?;

assert_eq!(&serialized, b"account:abc:000004d2");

let deserialized = strkey::from_slice::<(String, AccountId)>(&serialized)?;

assert_eq!(&deserialized.0, "account");
assert_eq!(
    &deserialized.1,
    &AccountId {
        domain: "abc".to_string(),
        user_id: 1234
    }
);

Contributing

If you have any issues or features, please use the GitHub Issues and Pull Request sections.

License

Copyright 2021 Christopher Foo. Licensed under MPL-2.0.

Commit count: 3

cargo fmt