lichtung-plot 0.2.0

Strongly typed scientific plotting with deterministic SVG and PNG output
Documentation
//! Deterministic bootstrap streams depend on original typed keys.

use crate::data::{key_bytes, RawKey};
use crate::Result;

pub(super) fn hash_bytes(hash: &mut u64, bytes: &[u8]) -> Result<()> {
    crate::operation::work(bytes.len())?;
    for byte in bytes {
        *hash ^= u64::from(*byte);
        *hash = hash.wrapping_mul(0x0000_0100_0000_01b3);
    }
    Ok(())
}

fn hash_field(hash: &mut u64, bytes: &[u8]) -> Result<()> {
    hash_bytes(
        hash,
        &u64::try_from(bytes.len())
            .expect("byte length")
            .to_le_bytes(),
    )?;
    hash_bytes(hash, bytes)
}

pub(super) fn hash_key(hash: &mut u64, key: &RawKey) -> Result<()> {
    if let RawKey::String(value) = key {
        return hash_field(hash, value.as_bytes());
    }
    // Preserve the existing string stream. Reserve an impossible string
    // length for the typed-key branch, which carries its full framed bytes.
    hash_bytes(hash, &u64::MAX.to_le_bytes())?;
    hash_field(hash, &key_bytes(key)?)
}