feat(serialize):基本完成Hex和Base64的编解码。

This commit is contained in:
徐涛
2023-06-29 15:43:59 +08:00
parent fb5937918a
commit a0f189d1f4
4 changed files with 98 additions and 2 deletions

39
tests/serialize.rs Normal file
View File

@@ -0,0 +1,39 @@
#[cfg(test)]
mod hex {
#[test]
fn encode_hex() {
let input = vec![0x01, 0x02, 0x03, 0x04, 0x0f, 0xff];
let output = rs_toolbox::serialize::to_hex(input);
assert_eq!(output, "010203040fff");
}
#[test]
fn decode_hex() {
let input = "010203040fff";
let output = match rs_toolbox::serialize::from_hex(input) {
Ok(v) => v,
Err(e) => panic!("Error: {:?}", e),
};
assert_eq!(output, vec![0x01, 0x02, 0x03, 0x04, 0x0f, 0xff]);
}
}
#[cfg(test)]
mod base64 {
#[test]
fn encode_base64_str() {
let input = "hello";
let output = rs_toolbox::serialize::to_base64_str(input);
assert_eq!(output, "aGVsbG8=");
}
#[test]
fn decode_base64_str() {
let input = "aGVsbG8=";
let output = match rs_toolbox::serialize::from_base64_str(input) {
Ok(v) => String::from_utf8(v).unwrap(),
Err(e) => panic!("Error: {:?}", e),
};
assert_eq!(output, "hello");
}
}