ps-hash-core 0.1.0-25

Core hashing primitives for ps-hash.
Documentation
use std::fmt::{Debug, Display};

use super::super::Hash;

impl Display for Hash {
    /// Writes the canonical Crockford Base32 representation.
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        ps_crockford32::encode_into(&self.inner, f)
    }
}

impl Debug for Hash {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        Display::fmt(self, f)
    }
}

#[cfg(test)]
#[allow(clippy::expect_used)]
mod tests {
    use crate::{Hash, HASH_SIZE_CROCKFORD};

    #[test]
    fn display_correct_length() {
        let h = Hash::hash(b"test").expect("hashing should succeed");
        let s = format!("{h}");
        assert_eq!(s.len(), HASH_SIZE_CROCKFORD);
    }

    #[test]
    fn display_matches_to_crockford() {
        let h = Hash::hash(b"matches").expect("hashing should succeed");

        assert_eq!(format!("{h}"), h.to_crockford());
    }

    #[test]
    fn display_matches_to_string() {
        let h = Hash::hash(b"matches").expect("hashing should succeed");
        let displayed = format!("{h}");
        let to_string = h.to_string();
        assert_eq!(displayed, to_string);
    }

    #[test]
    fn display_is_deterministic() {
        let h = Hash::hash(b"deterministic").expect("hashing should succeed");
        let s1 = format!("{h}");
        let s2 = format!("{h}");
        assert_eq!(s1, s2);
    }

    #[test]
    fn display_different_for_different_data() {
        let h1 = Hash::hash(b"data1").expect("hashing should succeed");
        let h2 = Hash::hash(b"data2").expect("hashing should succeed");
        let s1 = format!("{h1}");
        let s2 = format!("{h2}");
        assert_ne!(s1, s2);
    }

    #[test]
    fn debug_correct_length() {
        let h = Hash::hash(b"debug").expect("hashing should succeed");
        let s = format!("{h:?}");
        assert_eq!(s.len(), HASH_SIZE_CROCKFORD);
    }

    #[test]
    fn debug_matches_display() {
        let h = Hash::hash(b"debug display").expect("hashing should succeed");
        let debug = format!("{h:?}");
        let display = format!("{h}");
        assert_eq!(debug, display);
    }

    #[test]
    fn debug_matches_to_string() {
        let h = Hash::hash(b"debug to_string").expect("hashing should succeed");
        let debug = format!("{h:?}");
        let to_string = h.to_string();
        assert_eq!(debug, to_string);
    }

    #[test]
    fn debug_is_deterministic() {
        let h = Hash::hash(b"debug deterministic").expect("hashing should succeed");
        let s1 = format!("{h:?}");
        let s2 = format!("{h:?}");
        assert_eq!(s1, s2);
    }

    #[test]
    fn display_round_trips() {
        let original = Hash::hash(b"round trip").expect("hashing should succeed");
        let displayed = format!("{original}");
        let recovered =
            Hash::validate(&displayed).expect("round trip through validate should succeed");
        assert_eq!(original, recovered);
    }
}