Skip to content
/ strkey Public

Rust library for serialization of values into an human-readable encoding that preserves lexicographic sort order

License

Notifications You must be signed in to change notification settings

chfoo/strkey

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

About

Rust library for serialization of values into an human-readable encoding that preserves lexicographic sort order

Resources

License

Stars

Watchers

Forks

Sponsor this project

Packages

No packages published

Languages