blob: 5a8808322f644cc883dfb55192319f96ce11c130 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
use std::{fs::create_dir_all, path::Path};
use anyhow::Context;
use diesel::{Connection, SqliteConnection};
const SCHIST_DATA_DIR: &'static str = "schist";
const SCHIST_DATABASE_FILENAME: &'static str = "schist.sqlite";
pub fn establish_connection() -> anyhow::Result<SqliteConnection> {
let data_dir = dirs::data_dir().context("Failed to get local app data directory")?;
let schist_local_dir = data_dir.join(Path::new(SCHIST_DATA_DIR));
create_dir_all(&schist_local_dir)
.context(format!("Failed to create local files directory at {}", schist_local_dir.display()))?;
let db_path = schist_local_dir.join(Path::new(SCHIST_DATABASE_FILENAME));
let db_path = db_path
.to_str()
.context("Failed to construct database path")?;
Ok(SqliteConnection::establish(db_path)
.with_context(|| format!("Failed to connect to database at {}", db_path))?)
}
|