5个版本
0.1.4 | 2023年4月18日 |
---|---|
0.1.3 | 2023年4月18日 |
0.1.2 | 2023年4月11日 |
0.1.1 | 2023年3月31日 |
0.1.0 | 2023年3月29日 |
#1040 in 编码
40 每月下载次数
38KB
542 行
azamcodec-rs
在Rust中为Azam Codec实现的编码和解码库,Azam Codec是一种按字典序可排序的字节数组的多部分base16编码。无外部依赖。
许可证
MIT许可证 版权 (c) 2022 Azamshul Azizy
使用方法
导入crate并开始使用。
解码
use azamcodec::{azam_decode, decode::AzamDecode};
// Decode first section of Azam-encoded string as u32, using trait [`AzamDecode`] on uint type.
// "xytxvyyf" decodes to 0xdeadbeefu32, the rest of string is ignored.
let x = u32::azam_decode("xytxvyyfh5wgg1"); // 0xdeadbeefu32
// Decode multiple sections of Azam-encoded string to a tuple using azam_decode! macro.
// "xytxvyyf" decodes to 0xdeadbeefu32.
// "h5" decodes to 0x15u8.
// "wgg1" decodes to 0xc001u16.
let (x, y, z) = azam_decode!("xytxvyyfh5wgg1", u32, u8, u16).unwrap(); // (0xdeadbeefu32, 0x15u8, c001u16)
// Decode multiple sections of Azam-encoded string into custom struct.
struct Id {
record_id: u32,
type_id: u8,
variant_id: u16,
}
impl Id {
pub fn from_str(value: &str) -> Self {
// reader can be anything that implements std::io::Read.
// e.g. network stream or file
// This example reads from a byte array, which is backed by a string.
let reader = &mut value.as_bytes();
let record_id = u32::azam_decode_read(reader).unwrap();
let type_id = u8::azam_decode_read(reader).unwrap();
let variant_id = u16::azam_decode_read(reader).unwrap();
Self {
record_id,
type_id,
variant_id,
}
}
}
编码
use azamcodec::{azam_encode, encode::AzamEncode};
// Encode u32 value as Azam-encoded string as u32, using trait [`AzamEncode`] on uint type.
// 0xdeadbeefu32 encodes to "xytxvyyf".
let x = 0xdeadbeefu32.azam_encode(); // "xytxvyyf"
// Encode multiple values as Azam-encoded string, using [`azam_decode!`] macro.
// 0xdeadbeefu32 encodes to "xytxvyyf".
// 0x15u8 encodes to "h5".
// 0xc001u16 encodes to "wgg1".
let x = azam_encode!(0xdeadbeefu32, 0x15u8, 0xc001u16); // "xytxvyyfh5wgg1"
// Encode multiple values as Azam-encoded string from custom struct.
struct Id {
record_id: u32,
type_id: u8,
variant_id: u16,
}
impl Id {
pub fn to_str(&self) -> String {
// writer can be anything that implements std::io::Write.
// This example writes to a byte array, then converted to string.
// e.g. network stream or file
let mut writer = Vec::<u8>::new();
self.record_id.azam_encode_write(&mut writer).unwrap();
self.type_id.azam_encode_write(&mut writer).unwrap();
self.variant_id.azam_encode_write(&mut writer).unwrap();
String::from_utf8(writer).unwrap()
}
}
开发
适用标准Rust开发。还包括基准测试,可以通过cargo bench
执行。