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
use std::path::Path;
use std::rc::Rc;

use rusqlite;
use rusqlite::Connection;

use crate::entities::errors::{DbError, DbResult};
use crate::traits::repo::IConnection;

#[derive(Clone)]
pub struct RSQLConnection {
    inner: Rc<rusqlite::Connection>,
}

impl RSQLConnection {
    pub fn new<P: AsRef<Path>>(path: P) -> DbResult<Self> {
        let cnct = rusqlite::Connection::open(path)
            .map_err(|err| DbError::CanNotConnect(err.to_string()))?;
        Ok(Self {
            inner: Rc::new(cnct),
        })
    }
}

impl IConnection for RSQLConnection {
    fn with<T, F: FnOnce(&Connection) -> DbResult<T>>(&self, fun: F) -> DbResult<T> {
        fun(&self.inner)
    }
}