base91le 0.1.0

little-endian base91 encoding format that supports padding
Documentation
use std::io::Write as _;

use super::*;

trait Pnc {
    type Ty;
    fn or_panic(self) -> Self::Ty;
}

impl<T, E: std::fmt::Display> Pnc for Result<T, E> {
    type Ty = T;
    fn or_panic(self) -> Self::Ty {
        self.unwrap_or_else(|err| panic!("{err}"))
    }
}

#[test]
fn chunk_max() {
    let r = encode_chunk(b"\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff");
    assert_eq!(&r, b".+/Nd*DP8>OFnJg]")
}

#[test]
fn chunk_min() {
    let r = encode_chunk(b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00");
    assert_eq!(&r, b"0000000000000000")
}

#[test]
fn chunk_example() {
    let r = encode_chunk(b"example\x00\x00\x00\x00\x00\x00");
    assert_eq!(&r, b"B9bP:ie660000000")
}

#[test]
fn tail_example() {
    let r = encode_tail(b"example");
    assert_eq!(&r, b"B9bP:ie660~~~~~~")
}

#[test]
fn decode_chunk_example() {
    let c = b"B9bP:ie660000000";
    let mut buf = [0; 13];
    let n = decode_chunk(c, &mut buf).or_panic();
    let res = &buf[..n];
    assert_eq!(res, b"example\x00\x00\x00\x00\x00\x00");
}

#[test]
fn decode_tail_example() -> Result<(), DecodeError> {
    let c = b"B9bP:ie660~~~~~~";
    let mut buf = [0; 13];
    let n = decode_chunk(c, &mut buf)?;
    let res = &buf[..n];
    assert_eq!(res, b"example");
    Ok(())
}

#[test]
fn long_encode() {
    let c = b"this is a long example of base91le encoding";
    let res = encode(c);
    assert_eq!(
        res,
        "RSnMPUAvSUSV=h0a~MzmTX`IAD2T>OpAZL-S%o!le~?wN*Sa{z)RP00~~~~~~~~~"
    );
}

#[test]
fn long_decode() {
    let o = "RSnMPUAvSUSV=h0a~MzmTX`IAD2T>OpAZL-S%o!le~?wN*Sa{z)RP00~~~~~~~~~";
    let res = decode(o).or_panic();
    assert_eq!(res, b"this is a long example of base91le encoding");
}

#[test]
#[should_panic]
fn decode_bigger_chunk() {
    let c = b".+/Nd*DP8>OFnJg}";
    decode_chunk(c, &mut [0; _]).or_panic();
}

#[test]
#[should_panic]
fn decode_unexpected_char() {
    let c = b"\x7failfailfailfail";
    decode_chunk(c, &mut [0; _]).or_panic();
}

#[test]
#[should_panic]
fn long_fail() {
    let o = "RSnMPUAvSUSV=h0a~MzmTX'IAD2T>OpAZL-S%o!le~?wN*Sa{z)RP00~~~~~~~~~";
    decode(o).or_panic();
}

#[test]
fn readfile() {
    println!("{}", std::env::current_dir().or_panic().to_string_lossy());
    let mut buffer = Vec::new();
    // we need polonius
    {
        let mut wtr = write::EncodeWriter::new(&mut buffer);
        wtr.write_all(&std::fs::read("randomtext").or_panic())
            .or_panic();
    }
    let f = include_bytes!("../randomtext");
    let encoded = encode(f);
    assert_eq!(buffer, encoded.as_bytes())
}

#[test]
fn write() {
    let f = include_bytes!("../randomtext");
    let encoded = encode(f);
    let mut rdr = read::DecodeReader::new(encoded.as_bytes());
    std::io::copy(&mut rdr, &mut std::io::stdout()).or_panic();
}