mendia-db 1.3.0

Database library to be used with the mendia crate.
Documentation
use std::fs;

use serde::{Deserialize, Serialize};

use diesel::mysql::MysqlConnection;
use diesel::prelude::*;

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct Credentials {
    pub user: String,
    pub password: String,
}

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct Endpoint {
    pub host: String,
    pub port: i16,
    pub database: String,
    pub credentials: Credentials,
}

pub trait YML {
    fn from_yml(path: &str) -> Endpoint;
}

impl YML for Endpoint {
    fn from_yml(path: &str) -> Endpoint {
        let settings_file_content: &str = &fs::read_to_string(path).unwrap();
        serde_yaml::from_str(settings_file_content).unwrap()
    }
}

pub async fn connect(endpoint: &Endpoint) -> ConnectionResult<MysqlConnection> {
    let url = format!(
        "mysql://{}:{}@{}:{}/{}",
        endpoint.credentials.user,
        endpoint.credentials.password,
        endpoint.host,
        endpoint.port,
        endpoint.database,
    );
    MysqlConnection::establish(&url)
}