use crate::error::{ErrorKind, TidemarkError};
use std::path::Path;
pub fn hash_bytes(bytes: &[u8]) -> String {
format!("blake3:{}", blake3::hash(bytes).to_hex())
}
pub fn read_link(path: &Path) -> Result<String, TidemarkError> {
let target = std::fs::read_link(path)?;
target
.to_str()
.map(|s| s.replace('\\', "/"))
.ok_or_else(|| {
TidemarkError::new(
ErrorKind::Unsupported,
"non-UTF-8 symlink target".to_string(),
)
})
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn same_content_same_hash() {
assert_eq!(hash_bytes(b"hello"), hash_bytes(b"hello"));
}
#[test]
fn different_content_different_hash() {
assert_ne!(hash_bytes(b"hello"), hash_bytes(b"world"));
}
#[test]
fn hash_has_prefix() {
assert!(hash_bytes(b"x").starts_with("blake3:"));
}
}