Skip to main content

katana_markdown_engine/source/
impls.rs

1use super::types::{RawSnippet, TextFingerprint};
2use sha2::{Digest, Sha256};
3
4impl RawSnippet {
5    pub fn new(text: impl Into<String>) -> Self {
6        Self { text: text.into() }
7    }
8
9    pub fn fingerprint(&self) -> TextFingerprint {
10        TextFingerprint::for_text(&self.text)
11    }
12}
13
14impl TextFingerprint {
15    pub fn for_text(text: &str) -> Self {
16        let digest = Sha256::digest(text.as_bytes());
17        Self {
18            algorithm: "sha256".to_string(),
19            value: to_hex(&digest),
20        }
21    }
22}
23
24fn to_hex(bytes: &[u8]) -> String {
25    const HEX: &[u8; 16] = b"0123456789abcdef";
26    let mut output = String::with_capacity(bytes.len() * 2);
27    for byte in bytes {
28        output.push(HEX[(byte >> 4) as usize] as char);
29        output.push(HEX[(byte & 0x0f) as usize] as char);
30    }
31    output
32}