use ct_codecs::{Decoder, Encoder, Hex};
use ct_codecs::Base64UrlSafe;
use ct_codecs::Base64;
use base32ct::Error as Bs32Error;
use base32ct::{Base32,Base32Unpadded};
use base32ct::Encoding;
use bs58;
use bs58::encode::Error as bs58Error;
use bs58::decode::Error as bs58DecodingError;
use ct_codecs::Error;
pub mod errors;
pub mod prelude;
use errors::SlugEncodingError;
#[derive(Clone,Copy,Debug,PartialEq,PartialOrd,Hash)]
pub enum SlugEncodings {
Hex,
Base32,
Base32unpadded,
Base58,
Base64,
Base64urlsafe,
}
pub trait SlugEncoder {
fn to_hex(&self) -> Result<String, Error>;
fn to_bs64_url(&self) -> Result<String, Error>;
fn to_bs64(&self) -> Result<String, Error>;
fn to_bs32(&self) -> String;
fn to_bs32_unpadded(&self) -> String;
fn to_base58(&self) -> String;
}
pub trait SlugDecoder {
fn from_hex(&self) -> Result<Vec<u8>,Error>;
fn from_bs64(&self) -> Result<Vec<u8>,Error>;
fn from_bs64_url(&self) -> Result<Vec<u8>,Error>;
fn from_bs32(&self) -> Result<Vec<u8>,Bs32Error>;
fn from_bs32_unpadded(&self) -> Result<Vec<u8>,Bs32Error>;
fn from_base58(&self) -> Result<Vec<u8>,bs58DecodingError>;
}
impl SlugEncoder for Vec<u8> {
fn to_hex(&self) -> Result<String, Error> {
let hex_str = Hex::encode_to_string(&self)?;
Ok(hex_str)
}
fn to_bs64(&self) -> Result<String, Error> {
let bs64_url_str = Base64::encode_to_string(&self)?;
Ok(bs64_url_str)
}
fn to_bs64_url(&self) -> Result<String, Error> {
let bs64_url_str = Base64UrlSafe::encode_to_string(&self)?;
Ok(bs64_url_str)
}
fn to_bs32(&self) -> String {
let bs32 = Base32::encode_string(&self);
return bs32
}
fn to_bs32_unpadded(&self) -> String {
let bs32 = Base32Unpadded::encode_string(&self);
bs32
}
fn to_base58(&self) -> String {
let s = bs58::encode(&self).into_string();
return s
}
}
impl SlugEncoder for &[u8] {
fn to_hex(&self) -> Result<String, Error> {
let hex_str = Hex::encode_to_string(&self)?;
Ok(hex_str)
}
fn to_bs64(&self) -> Result<String, Error> {
let bs64_url_str = Base64::encode_to_string(&self)?;
Ok(bs64_url_str)
}
fn to_bs64_url(&self) -> Result<String, Error> {
let bs64_url_str = Base64UrlSafe::encode_to_string(&self)?;
Ok(bs64_url_str)
}
fn to_bs32(&self) -> String {
let bs32 = Base32::encode_string(&self);
return bs32
}
fn to_bs32_unpadded(&self) -> String {
let bs32 = Base32Unpadded::encode_string(&self);
bs32
}
fn to_base58(&self) -> String {
let s = bs58::encode(&self).into_string();
return s
}
}
impl SlugEncoder for [u8;28] {
fn to_hex(&self) -> Result<String, Error> {
let hex_str = Hex::encode_to_string(&self)?;
Ok(hex_str)
}
fn to_bs64(&self) -> Result<String, Error> {
let bs64_url_str = Base64::encode_to_string(&self)?;
Ok(bs64_url_str)
}
fn to_bs64_url(&self) -> Result<String, Error> {
let bs64_url_str = Base64UrlSafe::encode_to_string(&self)?;
Ok(bs64_url_str)
}
fn to_bs32(&self) -> String {
let bs32 = Base32::encode_string(self);
return bs32
}
fn to_bs32_unpadded(&self) -> String {
let bs32 = Base32Unpadded::encode_string(self);
bs32
}
fn to_base58(&self) -> String {
let s = bs58::encode(&self).into_string();
return s
}
}
impl SlugEncoder for [u8;32] {
fn to_hex(&self) -> Result<String, Error> {
let hex_str = Hex::encode_to_string(&self)?;
Ok(hex_str)
}
fn to_bs64(&self) -> Result<String, Error> {
let bs64_url_str = Base64::encode_to_string(&self)?;
Ok(bs64_url_str)
}
fn to_bs64_url(&self) -> Result<String, Error> {
let bs64_url_str = Base64UrlSafe::encode_to_string(&self)?;
Ok(bs64_url_str)
}
fn to_bs32(&self) -> String {
let bs32 = Base32::encode_string(self);
return bs32
}
fn to_bs32_unpadded(&self) -> String {
let bs32 = Base32Unpadded::encode_string(self);
bs32
}
fn to_base58(&self) -> String {
let s = bs58::encode(&self).into_string();
return s
}
}
impl SlugEncoder for [u8;48] {
fn to_hex(&self) -> Result<String, Error> {
let hex_str = Hex::encode_to_string(&self)?;
Ok(hex_str)
}
fn to_bs64(&self) -> Result<String, Error> {
let bs64_url_str = Base64::encode_to_string(&self)?;
Ok(bs64_url_str)
}
fn to_bs64_url(&self) -> Result<String, Error> {
let bs64_url_str = Base64UrlSafe::encode_to_string(&self)?;
Ok(bs64_url_str)
}
fn to_bs32(&self) -> String {
let bs32 = Base32::encode_string(self);
return bs32
}
fn to_bs32_unpadded(&self) -> String {
let bs32 = Base32Unpadded::encode_string(self);
bs32
}
fn to_base58(&self) -> String {
let s = bs58::encode(&self).into_string();
return s
}
}
impl SlugEncoder for [u8;64] {
fn to_hex(&self) -> Result<String, Error> {
let hex_str = Hex::encode_to_string(&self)?;
Ok(hex_str)
}
fn to_bs64(&self) -> Result<String, Error> {
let bs64_url_str = Base64::encode_to_string(&self)?;
Ok(bs64_url_str)
}
fn to_bs64_url(&self) -> Result<String, Error> {
let bs64_url_str = Base64UrlSafe::encode_to_string(&self)?;
Ok(bs64_url_str)
}
fn to_bs32(&self) -> String {
let bs32 = Base32::encode_string(self);
return bs32
}
fn to_bs32_unpadded(&self) -> String {
let bs32 = Base32Unpadded::encode_string(self);
bs32
}
fn to_base58(&self) -> String {
let s = bs58::encode(&self).into_string();
return s
}
}
#[derive(Clone,Copy,Debug,PartialEq,PartialOrd,Hash)]
pub struct SlugEncodingUsage {
encoding: SlugEncodings,
}
impl SlugEncodingUsage {
pub fn new(encoding: SlugEncodings) -> Self {
return Self {
encoding: encoding,
}
}
pub fn get_encoding(&self) -> SlugEncodings {
return self.encoding
}
pub fn decode<T: AsRef<str>>(&self, encoded_str: T) -> Result<Vec<u8>,SlugEncodingError> {
match self.encoding {
SlugEncodings::Hex => {
let decoding = encoded_str.as_ref().from_hex();
match decoding {
Ok(v) => return Ok(v),
Err(_) => return Err(SlugEncodingError::DecodingError)
}
}
SlugEncodings::Base32 => {
let decoding = encoded_str.as_ref().from_bs32();
match decoding {
Ok(v) => return Ok(v),
Err(_) => return Err(SlugEncodingError::DecodingError)
}
}
SlugEncodings::Base32unpadded => {
let decoding = encoded_str.as_ref().from_bs32_unpadded();
match decoding {
Ok(v) => return Ok(v),
Err(_) => return Err(SlugEncodingError::DecodingError)
}
}
SlugEncodings::Base58 => {
let decoding = encoded_str.as_ref().from_base58();
match decoding {
Ok(v) => return Ok(v),
Err(_) => return Err(SlugEncodingError::DecodingError)
}
}
SlugEncodings::Base64 => {
let decoding = encoded_str.as_ref().from_bs64();
match decoding {
Ok(v) => return Ok(v),
Err(_) => return Err(SlugEncodingError::DecodingError)
}
}
SlugEncodings::Base64urlsafe => {
let decoding = encoded_str.as_ref().from_bs64_url();
match decoding {
Ok(v) => return Ok(v),
Err(_) => return Err(SlugEncodingError::DecodingError)
}
}
}
}
pub fn encode<T: AsRef<[u8]>>(&self, bytes: T) -> Result<String,SlugEncodingError> {
match self.encoding {
SlugEncodings::Hex => {
let encoding = bytes.as_ref().to_hex();
match encoding {
Ok(v) => return Ok(v),
Err(_) => return Err(SlugEncodingError::Failed)
}
}
SlugEncodings::Base32 => {
Ok(bytes.as_ref().to_bs32())
}
SlugEncodings::Base32unpadded => {
Ok(bytes.as_ref().to_bs32_unpadded())
}
SlugEncodings::Base58 => {
Ok(bytes.as_ref().to_base58())
}
SlugEncodings::Base64 => {
let encoding = bytes.as_ref().to_bs64();
match encoding {
Ok(v) => return Ok(v),
Err(_) => return Err(SlugEncodingError::Failed)
}
}
SlugEncodings::Base64urlsafe => {
let encoding = bytes.as_ref().to_bs64_url();
match encoding {
Ok(v) => return Ok(v),
Err(_) => return Err(SlugEncodingError::Failed)
}
}
}
}
}
impl SlugDecoder for String {
fn from_hex(&self) -> Result<Vec<u8>,Error> {
let output = Hex::decode_to_vec(&self, None)?;
Ok(output)
}
fn from_bs64(&self) -> Result<Vec<u8>,Error> {
let output = Base64::decode_to_vec(&self, None)?;
Ok(output)
}
fn from_bs64_url(&self) -> Result<Vec<u8>,Error> {
let output = Base64UrlSafe::decode_to_vec(&self, None)?;
Ok(output)
}
fn from_bs32(&self) -> Result<Vec<u8>,Bs32Error> {
let output = Base32::decode_vec(&self)?;
Ok(output)
}
fn from_bs32_unpadded(&self) -> Result<Vec<u8>,Bs32Error> {
let output = Base32Unpadded::decode_vec(&self)?;
Ok(output)
}
fn from_base58(&self) -> Result<Vec<u8>,bs58DecodingError> {
let output = bs58::decode(&self).into_vec()?;
Ok(output)
}
}
impl SlugDecoder for &str {
fn from_hex(&self) -> Result<Vec<u8>,Error> {
let output = Hex::decode_to_vec(&self, None)?;
Ok(output)
}
fn from_bs64(&self) -> Result<Vec<u8>,Error> {
let output = Base64::decode_to_vec(&self, None)?;
Ok(output)
}
fn from_bs64_url(&self) -> Result<Vec<u8>,Error> {
let output = Base64UrlSafe::decode_to_vec(&self, None)?;
Ok(output)
}
fn from_bs32(&self) -> Result<Vec<u8>,Bs32Error> {
let output = Base32::decode_vec(&self)?;
Ok(output)
}
fn from_bs32_unpadded(&self) -> Result<Vec<u8>,Bs32Error> {
let output = Base32Unpadded::decode_vec(&self)?;
Ok(output)
}
fn from_base58(&self) -> Result<Vec<u8>,bs58DecodingError> {
let output = bs58::decode(&self).into_vec()?;
Ok(output)
}
}
#[test]
fn run() {
use self::SlugEncoder;
let bytes: [u8;30] = [33u8;30];
let byte_slice = bytes.as_slice();
let output = byte_slice.to_base58();
println!("Output: {}", output);
}
#[test]
fn slugencoder() {
use self::SlugEncoder;
println!("Running SlugEncoder Tests:");
println!("");
let message_concat: &str = "4144675a6e958d60";
let message_224: &str = "4144675a6e958d600a2d6a859f5b16ab321ec93e47580ec42025be0b";
let message_512: &str = "62928ef1f4effcfcba350e9e033ed8c07a94066654e1a4be79dd72027f3828480a7c517266a04fd747a8702d7087a298484c525bdd1b54997bb5eca1a6219b58";
let bytes = message_concat.as_bytes();
let byte_vec: Vec<u8> = bytes.to_vec();
println!("HEX: {}",bytes.to_hex().unwrap());
println!("Base32: {}", bytes.to_bs32());
println!("Base32_unpadded: {}", bytes.to_bs32_unpadded());
println!("Base58: {}", bytes.to_base58());
println!("Base64: {}",bytes.to_bs64().unwrap());
println!("Base64_URL_SAFE: {}",bytes.to_bs64_url().unwrap());
}
#[test]
fn slugapi() {
let x = SlugEncodingUsage::new(SlugEncodings::Base64);
let output = x.encode(b"Hnma").unwrap();
println!("Output: {}", output)
}
#[test]
fn slugdecoder() {
use self::SlugEncoder;
println!("Running SlugEncoder Tests:");
println!("");
let message_concat: &str = "4144675a6e958d60";
let message_224: &str = "4144675a6e958d600a2d6a859f5b16ab321ec93e47580ec42025be0b";
let message_512: &str = "62928ef1f4effcfcba350e9e033ed8c07a94066654e1a4be79dd72027f3828480a7c517266a04fd747a8702d7087a298484c525bdd1b54997bb5eca1a6219b58";
let bytes = message_concat.as_bytes();
let byte_vec: Vec<u8> = bytes.to_vec();
println!("HEX: {}",bytes.to_hex().unwrap());
println!("Base32: {}", bytes.to_bs32());
println!("Base32_unpadded: {}", bytes.to_bs32_unpadded());
println!("Base58: {}", bytes.to_base58());
println!("Base64: {}",bytes.to_bs64().unwrap());
println!("Base64_URL_SAFE: {}",bytes.to_bs64_url().unwrap());
let hex = bytes.to_hex().unwrap();
let output = hex.from_hex().unwrap();
println!("Decoded Output: {:?}", output)
}
#[test]
fn slugapi_usage() {
let message_512: &str = "62928ef1f4effcfcba350e9e033ed8c07a94066654e1a4be79dd72027f3828480a7c517266a04fd747a8702d7087a298484c525bdd1b54997bb5eca1a6219b58";
let x = SlugEncodingUsage::new(SlugEncodings::Hex);
let output = x.decode(message_512).unwrap();
println!("Output: {:?}", output)
}