volli-core 0.1.11

Shared types for volli
Documentation
use std::path::{Path, PathBuf};

/// Simple FNV-1a 64-bit hash for stable, short identifiers.
pub fn fnv1a64(s: &str) -> u64 {
    let mut hash: u64 = 0xcbf29ce484222325;
    for &b in s.as_bytes() {
        hash ^= b as u64;
        hash = hash.wrapping_mul(0x100000001b3);
    }
    hash
}

/// Compute a short runtime socket path under the system temporary directory.
/// Uses canonicalized `profile_dir` + role/profile to avoid UNIX sun_path limits.
pub fn runtime_socket_path_for_dir(profile_dir: &Path, role: &str, profile: &str) -> PathBuf {
    let base = std::env::temp_dir();
    let canon = std::fs::canonicalize(profile_dir).unwrap_or_else(|_| profile_dir.to_path_buf());
    let key = format!("{}:{}:{}", canon.to_string_lossy(), role, profile);
    let h = fnv1a64(&key);
    base.join(format!("vs-{}-{:x}.sock", role, h))
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn runtime_socket_path_is_short() {
        let base = tempfile::tempdir().unwrap();
        let path = runtime_socket_path_for_dir(base.path(), "worker", "some-long-profile");
        let len = path.to_string_lossy().len();
        assert!(len < 100, "socket path too long: {len}");
    }
}