binarytext 0.1.2

Binary-to-text encoders / decoders
Documentation
//! This crate provides different encoders and some of their variants, like Base64 or Base32.
//! All of them implement the trait BinaryText in the module binarytext and share a common
//! interface.
//!
//! Furthermore error types can be found in the mod error.
//! Functions for handling encoding / decoding of byte streams from a source to a destination, e.g.
//! input string decoded to stdout, are provided in the module stream.

pub mod base16;
pub mod base32;
pub mod base45;
pub mod base64;
pub mod base85;
pub mod binarytext;
pub mod error;
pub mod int;
pub mod stream;

#[cfg(test)]
mod tests {
    use crate::base16::Base16;
    use crate::base32::Base32;
    use crate::base45::Base45;
    use crate::base64::Base64;
    use crate::base85::Base85;
    use crate::binarytext::BinaryText;
    use crate::int::IntEncoder;

    fn compare<B: BinaryText>(b: &B, dec: &str, enc: &str) {
        assert_eq!(b.decode_from_str(enc).unwrap(), dec.to_string());
        assert_eq!(b.encode_from_str(dec).unwrap(), enc.to_string());
    }

    fn compare_int<B: BinaryText>(b: &B, int: u64, s: &str) {
        assert_eq!(b.decode_u64(s.as_bytes()).unwrap(), int);
        assert_eq!(b.encode_u64(int).unwrap(), s.to_string());
        let int128 = int as u128;
        assert_eq!(b.decode_u128(s.as_bytes()).unwrap(), int128);
        assert_eq!(b.encode_u128(int128).unwrap(), s.to_string());
    }

    #[test]
    fn base16() {
        let b = Base16::new();
        compare(&b, "", "");
        compare(&b, "f", "66");
        compare(&b, "fo", "666F");
        compare(&b, "foo", "666F6F");
        compare(&b, "foob", "666F6F62");
        compare(&b, "fooba", "666F6F6261");
        compare(&b, "foobar", "666F6F626172");
    }

    #[test]
    fn base32() {
        let b = Base32::new();
        compare(&b, "", "");
        compare(&b, "f", "MY======");
        compare(&b, "fo", "MZXQ====");
        compare(&b, "foo", "MZXW6===");
        compare(&b, "foob", "MZXW6YQ=");
        compare(&b, "fooba", "MZXW6YTB");
        compare(&b, "foobar", "MZXW6YTBOI======");
    }

    #[test]
    fn base32hex() {
        let b = Base32::base32hex();
        compare(&b, "", "");
        compare(&b, "f", "CO======");
        compare(&b, "fo", "CPNG====");
        compare(&b, "foo", "CPNMU===");
        compare(&b, "foob", "CPNMUOG=");
        compare(&b, "fooba", "CPNMUOJ1");
        compare(&b, "foobar", "CPNMUOJ1E8======");
    }

    #[test]
    fn base36() {
        let b = IntEncoder::base36();
        compare_int(&b, 0u64, "");
        compare_int(&b, 123u64, "3F");
        compare_int(&b, 926192u64, "JUNK");
        compare_int(&b, 9223372036854775807u64, "1Y2P0IJ32E8E7");
        compare_int(&b, 18537u64, "EAX");
        compare(&b, "", "");
        compare(&b, "Hi", "EAX");
    }

    #[test]
    fn base45() {
        let b = Base45::new();
        //Examples taken from RFC 9285
        compare(&b, "", "");
        compare(&b, "AB", "BB8");
        compare(&b, "base-45", "UJCLQE7W581");
        compare(&b, "Hello!!", "%69 VD92EX0");
        compare(&b, "ietf!", "QED8WEX0");
    }

    #[test]
    fn base62() {
        let b = IntEncoder::base62();
        compare_int(&b, 0u64, "");
        compare_int(&b, 91574294826053u64, "Q0DRQksv");
        compare(&b, "SIMPLE", "Q0DRQksv");
    }

    #[test]
    fn base64() {
        let b = Base64::new();
        compare(&b, "", "");
        compare(&b, "f", "Zg==");
        compare(&b, "fo", "Zm8=");
        compare(&b, "foo", "Zm9v");
        compare(&b, "foob", "Zm9vYg==");
        compare(&b, "fooba", "Zm9vYmE=");
        compare(&b, "foobar", "Zm9vYmFy");
    }

    #[test]
    fn base85() {
        let b = Base85::new();
        // Examples taken from Wikipedia
        compare(&b, "", "");
        compare(&b, "Man ", "9jqo^");
        compare(&b, "sure", "F*2M7");
        compare(&b, ".", "/c");
        compare(
            &b,
            "Man is distinguished, not only by his reason, but by this singular passion from other animals, which is a lust of the mind, that by a perseverance of delight in the continued and indefatigable generation of knowledge, exceeds the short vehemence of any carnal pleasure.",
            "9jqo^BlbD-BleB1DJ+*+F(f,q/0JhKF<GL>Cj@.4Gp$d7F!,L7@<6@)/0JDEF<G%<+EV:2F!,O<DJ+*.@<*K0@<6L(Df-\\0Ec5e;DffZ(EZee.Bl.9pF\"AGXBPCsi+DGm>@3BB/F*&OCAfu2/AKYi(DIb:@FD,*)+C]U=@3BN#EcYf8ATD3s@q?d$AftVqCh[NqF<G:8+EV:.+Cf>-FD5W8ARlolDIal(DId<j@<?3r@:F%a+D58'ATD4$Bl@l3De:,-DJs`8ARoFb/0JMK@qB4^F!,R<AKZ&-DfTqBG%G>uD.RTpAKYo'+CT/5+Cei#DII?(E,9)oF*2M7/c",
        );
    }
}