aboutsummaryrefslogtreecommitdiff
path: root/src/db/utils.rs
blob: f61d20aa49fc8e8a8f038298d67abd3b11af8511 (plain)
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
use crate::model::report::Report;
use tokio_rusqlite::Row;

pub fn format_key_for_search(value: &str) -> String {
    format!("replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(lower({}), 'à', 'a'), 'â', 'a'), 'ç', 'c'), 'è', 'e'), 'é', 'e'), 'ê', 'e'), 'ë', 'e'), 'î', 'i'), 'ï', 'i'), 'ô', 'o'), 'ù', 'u'), 'û', 'u'), 'ü', 'u')", value)
}

pub fn one<A, I: Iterator<Item = Result<A, rusqlite::Error>>>(
    mut iter: I,
) -> Result<A, tokio_rusqlite::Error> {
    match iter.next() {
        Some(Ok(user)) => Ok(user),
        Some(Err(err)) => Err(tokio_rusqlite::Error::Rusqlite(err)),
        None => Err(tokio_rusqlite::Error::Rusqlite(
            rusqlite::Error::QueryReturnedNoRows,
        )),
    }
}

pub fn row_to_report(row: &Row) -> Result<Report, rusqlite::Error> {
    Ok(Report {
        date: row.get(0)?,
        name: row.get(1)?,
        amount: row.get(2)?,
        action: row.get(3)?,
    })
}