tcup 0.1.2

The one and only teacup client!
Documentation
use std::{fs, path::PathBuf};

use dirs::home_dir;
use rusqlite::Connection;

use crate::errors::TeacupError;

pub struct Config {
    pub connection: Connection,
}

pub fn get_config() -> Result<Config, TeacupError> {
    // Check we can actually find our home directory, and therefore ~/.teacup
    //
    let teacup_home = home_dir()
        .map(|home| home.join(".teacup"))
        .ok_or(TeacupError::UnableToFindHomeDirectory)?;

    // Create it if it doesn't exist
    //
    fs::create_dir_all(&teacup_home).map_err(|e| TeacupError::UnableToCreateConfigHome)?;

    // ~/.teacup/db
    //
    let teacup_db = teacup_home.join("db");

    // Open or create the db
    //
    let connection = Connection::open(teacup_db.clone()).map_err(|e| {
        eprintln!("Error: {}", e);
        TeacupError::UnableToOpenDatabase(
            teacup_db
                .to_str()
                .map(|s| s.to_string())
                .unwrap_or("unknown".to_string()),
        )
    })?;

    Ok(Config { connection })
}