Skip to main content

ps_hash_core/hash/methods/
display_base64.rs

1use super::super::Hash;
2
3impl Hash {
4    /// Returns an adapter that renders the unpadded base64url representation
5    /// via [`core::fmt::Display`].
6    ///
7    /// The adapter streams directly into the formatter without allocating,
8    /// and produces exactly the same text as [`Hash::to_base64`].
9    #[inline]
10    #[must_use]
11    pub fn display_base64(&self) -> impl core::fmt::Display + '_ {
12        ps_base64::display(&self.inner)
13    }
14}
15
16#[cfg(test)]
17#[allow(clippy::expect_used)]
18mod tests {
19    use crate::{Hash, HASH_SIZE_BASE64};
20
21    #[test]
22    fn display_base64_matches_to_base64() {
23        for index in 0u32..256 {
24            let h = Hash::hash(index.to_le_bytes()).expect("hashing should succeed");
25
26            assert_eq!(h.display_base64().to_string(), h.to_base64());
27        }
28    }
29
30    #[test]
31    fn display_base64_has_the_expected_length() {
32        let h = Hash::hash(b"length").expect("hashing should succeed");
33
34        assert_eq!(h.display_base64().to_string().len(), HASH_SIZE_BASE64);
35    }
36
37    #[test]
38    fn display_base64_embeds_in_format_macros() {
39        let h = Hash::hash(b"embed").expect("hashing should succeed");
40
41        assert_eq!(
42            format!("E{}", h.display_base64()),
43            format!("E{}", h.to_base64())
44        );
45    }
46}