pretty_hash/
lib.rs

1#![forbid(unsafe_code, missing_debug_implementations, missing_docs)]
2#![cfg_attr(test, deny(warnings))]
3
4//! ## Example
5//! ```rust
6//! extern crate pretty_hash;
7//!
8//! let hash = pretty_hash::fmt(b"1234").unwrap();
9//! assert_eq!(hash, "31323334");
10//!
11//! let hash = pretty_hash::fmt(b"12345").unwrap();
12//! assert_eq!(hash, "313233..35");
13//! ```
14
15extern crate failure;
16
17use failure::Error;
18
19/// Prettify a byte slice.
20pub fn fmt(input: &[u8]) -> Result<String, Error> {
21  let string: String =
22    input.iter().map(|byte| format!("{:02x}", byte)).collect();
23
24  if string.len() > 8 {
25    let (head, tail) = string.split_at(6);
26    let cut_off = tail.len() - 2;
27    let (_, tail) = tail.split_at(cut_off);
28    Ok(format!("{}..{}", head, tail))
29  } else {
30    Ok(string)
31  }
32}