1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
use std::{error::Error, path::PathBuf, time::SystemTime};

use app_dirs::{get_app_root, AppDataType, AppInfo};
const CREATE_SECS: u64 = 1599939357;

pub fn secs_since_creation() -> u32 {
    let since_epoch = SystemTime::now()
        .duration_since(SystemTime::UNIX_EPOCH)
        .unwrap()
        .as_secs();

    // This hack will work for about 136 years
    (since_epoch - CREATE_SECS) as u32
}

pub enum QueryTerm {
    And(String),
    Not(String),
}
pub fn destructure_query_filter(q: &str) -> Vec<QueryTerm> {
    let terms = q.split_ascii_whitespace();
    terms
        .filter_map(|term| match term.chars().nth(0) {
            Some(c) if c == '!' => {
                let q = term.split_at(1).1;
                if q.is_empty() {
                    None
                } else {
                    Some(QueryTerm::Not(q.to_string()))
                }
            }
            Some(_) => Some(QueryTerm::And(term.to_string())),
            None => None,
        })
        .collect()
}

pub fn is_valid_query_filter(q: &str) -> bool {
    !destructure_query_filter(q).is_empty()
}

const APP_INFO: AppInfo = AppInfo {
    name: "nf-rated",
    author: "thlorenz",
};

#[derive(Debug)]
pub struct DatabaseInfo {
    pub db_exists: bool,
    pub folder_exists: bool,
    pub folder: PathBuf,
    pub db_path: PathBuf,
}

pub fn get_database_info() -> Result<DatabaseInfo, Box<dyn Error>> {
    let data_root = get_app_root(AppDataType::UserData, &APP_INFO)?;
    let folder_exists = data_root.exists();
    let db_path = data_root.join("nf_rated.sqlite");
    let db_exists = db_path.exists();
    Ok(DatabaseInfo {
        folder_exists,
        db_exists,
        folder: data_root,
        db_path,
    })
}