use crate::base_x::{self, Result};
const ENCODING: data_encoding::Encoding = data_encoding::BASE32HEX_NOPAD;
pub fn encode(content: impl AsRef<[u8]>) -> String {
base_x::encode(content, ENCODING)
}
pub fn decode(b32: &str) -> Result<Vec<u8>> {
base_x::decode(b32, ENCODING)
}
pub fn decode_to_string(b32: &str) -> Result<String> {
base_x::decode_to_string(b32, ENCODING)
}
#[cfg(test)]
mod tests {
type Result<T> = core::result::Result<T, Box<dyn std::error::Error>>;
use super::*;
const TEXT: &str = "This is not just a string!";
const RESULT: &str = "AHK6ISP0D5PI0RJFEGG6KTBJEGG6283JEHP6IRJ744";
#[test]
fn test_decode() -> Result<()> {
let decoded = decode(RESULT)?;
assert_eq!(decoded, TEXT.as_bytes());
Ok(())
}
#[test]
fn test_decode_to_string() -> Result<()> {
let decoded = decode_to_string(RESULT)?;
assert_eq!(decoded, TEXT);
Ok(())
}
#[test]
fn test_encode() -> Result<()> {
let encoded = encode(&TEXT);
assert_eq!(encoded, RESULT);
Ok(())
}
}