lib-porcelain 0.2.1

dollcode encoder and decoder
Documentation
mod deprecated;

pub use deprecated::{dollcode_decode, dollcode_encode};

pub trait Dollcode {
    /// Decodes a `&str` containing a dollcode into a `Self`.
    /// Returns `None` if `dollcode` contains invalid characters
    /// (everything except `'▖'`, `'▘'`, and `'▌'`).
    fn from_dollcode(dollcode: &str) -> Option<Self>
    where
        Self: Sized;

    /// Takes a `u64` and encodes in to a `String` containing a dollcode made up
    /// of `'▖'`, `'▘'`, and `'▌'`.
    fn to_dollcode(self) -> String;
}

macro_rules! impl_std_Dollcode {
    (for $($t:ty),+) => {
        $(impl Dollcode for $t {
            fn from_dollcode(input: &str) -> Option<Self> {
                let mut acc = 0;

                for char in input.chars() {
                    acc = acc * 3
                        + match char {
                            '▖' => 1,
                            '▘' => 2,
                            '▌' => 3,
                            _ => return None,
                        }
                }

                Some(acc)
            }

            fn to_dollcode(mut self) -> String {
                let mut res = String::new();

                while self > 0  {
                    self -= 1;
                    res.push(['▖', '▘', '▌'][(self % 3) as usize]);
                    self /= 3;
                }

                res.chars().rev().collect::<String>()
            }
        })*
    }
}

impl_std_Dollcode!(for u8);
impl_std_Dollcode!(for u16);
impl_std_Dollcode!(for u32);
impl_std_Dollcode!(for u64);
impl_std_Dollcode!(for u128);
impl_std_Dollcode!(for usize);

#[cfg(feature = "num")]
use num::{BigUint, ToPrimitive};

#[cfg(feature = "num")]
impl Dollcode for BigUint {
    fn from_dollcode(input: &str) -> Option<Self> {
        let mut acc = BigUint::ZERO;

        for char in input.chars() {
            acc = acc * 3_u32
                + match char {
                    '▖' => 1_u32,
                    '▘' => 2_u32,
                    '▌' => 3_u32,
                    _ => return None,
                }
        }

        Some(acc)
    }

    fn to_dollcode(mut self) -> String {
        let mut res = String::new();

        while self > BigUint::ZERO {
            self -= 1_u32;
            res.push(
                ['▖', '▘', '▌'][(self.clone() % 3_u32)
                    .to_usize()
                    .expect("failed to convert value to usize")],
            );
            self /= 3_u32;
        }

        res.chars().rev().collect::<String>()
    }
}

#[cfg(test)]
mod tests {
    use crate::Dollcode;

    #[test]
    fn std_encode() {
        assert_eq!(0_u32.to_dollcode(), "");
        assert_eq!(1_u32.to_dollcode(), "▖");
        assert_eq!(2_u32.to_dollcode(), "▘");
        assert_eq!(3_u32.to_dollcode(), "▌");
        assert_eq!(4_u32.to_dollcode(), "▖▖");
        assert_eq!(13_u32.to_dollcode(), "▖▖▖");
        assert_eq!(440729_u32.to_dollcode(), "▖▌▌▌▌▖▖▖▘▌▘▘");
        assert_eq!(893271_u32.to_dollcode(), "▖▖▘▘▌▘▌▌▘▘▘▌▌");
        assert_eq!(
            u64::MAX.to_dollcode(),
            "▖▖▖▖▘▘▖▘▌▘▘▖▘▘▖▖▘▌▌▖▖▌▌▌▖▌▖▖▌▖▌▌▖▌▌▘▖▖▘▖▌"
        );
    }

    #[test]
    fn std_decode() {
        assert_eq!(u32::from_dollcode("invalid"), None);
        assert_eq!(u32::from_dollcode("▖▌▌▌▌▖▖▖▘invalid"), None);
        assert_eq!(u32::from_dollcode(""), Some(0));
        assert_eq!(u32::from_dollcode("▖"), Some(1));
        assert_eq!(u32::from_dollcode("▘"), Some(2));
        assert_eq!(u32::from_dollcode("▌"), Some(3));
        assert_eq!(u32::from_dollcode("▖▖"), Some(4));
        assert_eq!(u32::from_dollcode("▖▖▖"), Some(13));
        assert_eq!(u32::from_dollcode("▖▌▌▌▌▖▖▖▘▌▘▘"), Some(440729));
        assert_eq!(u32::from_dollcode("▖▖▘▘▌▘▌▌▘▘▘▌▌"), Some(893271));
        assert_eq!(
            u64::from_dollcode("▖▖▖▖▘▘▖▘▌▘▘▖▘▘▖▖▘▌▌▖▖▌▌▌▖▌▖▖▌▖▌▌▖▌▌▘▖▖▘▖▌"),
            Some(u64::MAX)
        );
    }

    #[test]
    #[cfg(feature = "num")]
    fn big_int_encode() {
        use num::BigUint;

        assert_eq!(BigUint::ZERO.to_dollcode(), "");
        assert_eq!(
            BigUint::from_slice(&[893271]).to_dollcode(),
            "▖▖▘▘▌▘▌▌▘▘▘▌▌"
        );
        assert_eq!(
            BigUint::from_slice(&[u32::MAX, u32::MAX, u32::MAX]).to_dollcode(),
            "▖▘▖▖▌▌▘▌▌▌▘▌▘▖▌▘▌▘▌▌▖▌▖▘▘▖▘▖▘▖▘▌▌▖▌▖▖▌▖▖▖▘▖▌▖▖▘▌▌▘▌▘▘▘▘▖▘▌▌▘▌"
        );
    }

    #[test]
    #[cfg(feature = "num")]
    fn big_int_decode() {
        use num::BigUint;

        assert_eq!(BigUint::from_dollcode("invalid"), None);
        assert_eq!(BigUint::from_dollcode(""), Some(BigUint::ZERO));
        assert_eq!(
            BigUint::from_dollcode("▖▖▘▘▌▘▌▌▘▘▘▌▌"),
            Some(BigUint::from_slice(&[893271]))
        );
        assert_eq!(
            BigUint::from_dollcode("▖▘▖▖▌▌▘▌▌▌▘▌▘▖▌▘▌▘▌▌▖▌▖▘▘▖▘▖▘▖▘▌▌▖▌▖▖▌▖▖▖▘▖▌▖▖▘▌▌▘▌▘▘▘▘▖▘▌▌▘▌"),
            Some(BigUint::from_slice(&[u32::MAX, u32::MAX, u32::MAX]))
        );
    }
}