nameme_core 0.2.3

Library to find the actual type of files based on their magic number.
use lazy_static::lazy_static;
use std::sync::Mutex;
use std::time::Duration;

lazy_static! {
    static ref FILE_OPEN_TIMES: Mutex<Vec<Duration>> = Mutex::new(vec![]);
    static ref FILE_READ_TIMES: Mutex<Vec<Duration>> = Mutex::new(vec![]);
    static ref FIND_HEADER_TIMES: Mutex<Vec<Duration>> = Mutex::new(vec![]);
    static ref MATCH_HEADER_TIMES: Mutex<Vec<Duration>> = Mutex::new(vec![]);
}

pub fn add_open_time(d: Duration) {
    FILE_OPEN_TIMES.lock().unwrap().push(d);
}

pub fn add_read_time(d: Duration) {
    FILE_READ_TIMES.lock().unwrap().push(d);
}

pub fn add_match_header_time(d: Duration) {
    MATCH_HEADER_TIMES.lock().unwrap().push(d);
}

pub fn add_find_header_time(d: Duration) {
    FIND_HEADER_TIMES.lock().unwrap().push(d);
}

pub fn get_open_times() -> anyhow::Result<Duration> {
    Ok(Duration::from_nanos(
        FILE_OPEN_TIMES
            .lock()
            .unwrap()
            .iter()
            .fold(0 as u128, |p, x| p + x.as_nanos())
            .try_into()?,
    ))
}

pub fn get_read_times() -> anyhow::Result<Duration> {
    Ok(Duration::from_nanos(
        FILE_READ_TIMES
            .lock()
            .unwrap()
            .iter()
            .fold(0 as u128, |p, x| p + x.as_nanos())
            .try_into()?,
    ))
}

pub fn get_match_header_times() -> anyhow::Result<Duration> {
    Ok(Duration::from_nanos(
        MATCH_HEADER_TIMES
            .lock()
            .unwrap()
            .iter()
            .fold(0 as u128, |p, x| p + x.as_nanos())
            .try_into()?,
    ))
}

pub fn get_find_header_times() -> anyhow::Result<Duration> {
    Ok(Duration::from_nanos(
        FIND_HEADER_TIMES
            .lock()
            .unwrap()
            .iter()
            .fold(0 as u128, |p, x| p + x.as_nanos())
            .try_into()?,
    ))
}