use base58::{FromBase58, ToBase58};
pub fn encode(content: impl AsRef<[u8]>) -> String {
let bytes: &[u8] = content.as_ref();
bytes.to_base58()
}
pub fn decode(b58: &str) -> Result<Vec<u8>> {
b58.from_base58().map_err(|_| Error::FailToB58Decode)
}
pub fn decode_to_string(b58: &str) -> Result<String> {
decode(b58)
.ok()
.and_then(|r| String::from_utf8(r).ok())
.ok_or(Error::FailToB58Decode)
}
pub type Result<T> = core::result::Result<T, Error>;
#[derive(Debug)]
pub enum Error {
FailToB58Decode,
}
impl core::fmt::Display for Error {
fn fmt(&self, fmt: &mut core::fmt::Formatter) -> core::result::Result<(), core::fmt::Error> {
write!(fmt, "{self:?}")
}
}
impl std::error::Error for Error {}
#[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 = "3aump9mdueoaV87JMp3adSVWqNmpr9B43pnL";
#[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(())
}
}