1use std::{error::Error, path::PathBuf, time::SystemTime};
2
3use app_dirs::{get_app_root, AppDataType, AppInfo};
4const CREATE_SECS: u64 = 1599939357;
5
6pub fn secs_since_creation() -> u32 {
7 let since_epoch = SystemTime::now()
8 .duration_since(SystemTime::UNIX_EPOCH)
9 .unwrap()
10 .as_secs();
11
12 (since_epoch - CREATE_SECS) as u32
14}
15
16pub enum QueryTerm {
17 And(String),
18 Not(String),
19}
20pub fn destructure_query_filter(q: &str) -> Vec<QueryTerm> {
21 let terms = q.split_ascii_whitespace();
22 terms
23 .filter_map(|term| match term.chars().nth(0) {
24 Some(c) if c == '!' => {
25 let q = term.split_at(1).1;
26 if q.is_empty() {
27 None
28 } else {
29 Some(QueryTerm::Not(q.to_string()))
30 }
31 }
32 Some(_) => Some(QueryTerm::And(term.to_string())),
33 None => None,
34 })
35 .collect()
36}
37
38pub fn is_valid_query_filter(q: &str) -> bool {
39 !destructure_query_filter(q).is_empty()
40}
41
42const APP_INFO: AppInfo = AppInfo {
43 name: "nf-rated",
44 author: "thlorenz",
45};
46
47#[derive(Debug)]
48pub struct DatabaseInfo {
49 pub db_exists: bool,
50 pub folder_exists: bool,
51 pub folder: PathBuf,
52 pub db_path: PathBuf,
53}
54
55pub fn get_database_info() -> Result<DatabaseInfo, Box<dyn Error>> {
56 let data_root = get_app_root(AppDataType::UserData, &APP_INFO)?;
57 let folder_exists = data_root.exists();
58 let db_path = data_root.join("nf_rated.sqlite");
59 let db_exists = db_path.exists();
60 Ok(DatabaseInfo {
61 folder_exists,
62 db_exists,
63 folder: data_root,
64 db_path,
65 })
66}