micromegas_analytics/
scope.rs

1use datafusion::parquet::data_type::AsBytes;
2use std::sync::Arc;
3use xxhash_rust::xxh32::xxh32;
4
5/// A description of a scope, including its name, location, and hash.
6#[derive(Debug)]
7pub struct ScopeDesc {
8    pub name: Arc<String>,
9    pub filename: Arc<String>,
10    pub target: Arc<String>,
11    pub line: u32,
12    pub hash: u32,
13}
14
15/// A hash map of scope descriptions, keyed by their hash.
16pub type ScopeHashMap = std::collections::HashMap<u32, ScopeDesc>;
17
18impl ScopeDesc {
19    pub fn new(name: Arc<String>, filename: Arc<String>, target: Arc<String>, line: u32) -> Self {
20        let hash = compute_scope_hash(&name, &filename, &target, line);
21        Self {
22            name,
23            filename,
24            target,
25            line,
26            hash,
27        }
28    }
29}
30
31/// Computes the hash of a scope.
32pub fn compute_scope_hash(name: &str, filename: &str, target: &str, line: u32) -> u32 {
33    let hash_name = xxh32(name.as_bytes(), 0);
34    let hash_with_filename = xxh32(filename.as_bytes(), hash_name);
35    let hash_with_target = xxh32(target.as_bytes(), hash_with_filename);
36    xxh32(line.as_bytes(), hash_with_target)
37}