use std::collections::HashMap;
use std::fs;
use std::path::{PathBuf};
use json::{JsonValue, object};
#[derive(Clone, Debug)]
pub struct Config {
pub default: String,
pub connections: HashMap<String, Connection>,
}
impl Config {
pub fn default() -> Config {
let mut connections = HashMap::new();
connections.insert("mydbname".to_string(), Connection::default());
Self {
default: "mydbname".to_string(),
connections,
}
}
pub fn json(&mut self) -> JsonValue {
let mut data = object! {};
data["default"] = self.default.clone().into();
let mut connections = object! {};
for (name, connection) in self.connections.iter_mut() {
connections[name.clone()] = connection.json().clone();
}
data["connections"] = connections;
data
}
pub fn from(data: JsonValue) -> Config {
let default = data["default"].to_string();
let mut connections = HashMap::new();
for (key, value) in data["connections"].entries() {
let connection = Connection::default().from(value.clone()).clone();
connections.insert(key.to_string(), connection.clone());
}
Self {
default,
connections,
}
}
}
#[derive(Clone, Debug)]
pub enum Mode {
Mysql,
Mssql,
Sqlite,
}
impl Mode {
pub fn str(&mut self) -> &'static str {
match self {
Mode::Mysql => "mysql",
Mode::Sqlite => "sqlite",
Mode::Mssql => "mssql"
}
}
pub fn from(name: &str) -> Self {
match name {
"mysql" => Mode::Mysql,
"sqlite" => Mode::Sqlite,
"mssql" => Mode::Mssql,
_ => Mode::Sqlite
}
}
}
#[derive(Clone, Debug)]
pub struct Connection {
pub mode: Mode,
pub hostname: String,
pub hostport: String,
pub database: String,
pub username: String,
pub userpass: String,
pub params: Vec<String>,
pub charset: String,
pub prefix: String,
pub debug: bool,
}
impl Connection {
pub fn default() -> Connection {
Self {
mode: Mode::Sqlite,
hostname: "".to_string(),
hostport: "".to_string(),
database: "db/sqlite.db".to_string(),
username: "".to_string(),
userpass: "".to_string(),
params: vec![],
charset: "".to_string(),
prefix: "".to_string(),
debug: false,
}
}
pub fn json(&mut self) -> JsonValue {
let res = format!("{:?}", self);
let res = res.replace("Connection {", "{");
let res = res.replace("Sqlite", format!("\"{}\"", self.mode.str()).as_str());
let res = res.replace("Mysql", format!("\"{}\"", self.mode.str()).as_str());
let res = res.replace("Mssql", format!("\"{}\"", self.mode.str()).as_str());
let res = res.replace(",},", "}");
let res = res.replace(": ", "\": ");
let res = res.replace("{ ", "{ \"");
let res = res.replace(", ", ", \"");
json::parse(&*res).unwrap()
}
pub fn from(&mut self, data: JsonValue) -> &mut Connection {
self.mode = Mode::from(data["mode"].as_str().unwrap());
self.hostname = data["hostname"].to_string();
self.hostport = data["hostport"].to_string();
self.database = data["database"].to_string();
self.username = data["username"].to_string();
self.userpass = data["userpass"].to_string();
self.params = data["params"].members().map(|x| x.to_string()).collect();
self.charset = data["charset"].as_str().unwrap_or("utf8mb4").to_string();
self.prefix = data["prefix"].as_str().unwrap_or("").to_string();
self.debug = data["debug"].to_string().parse::<bool>().unwrap_or(false).into();
self
}
pub fn get_dsn(self) -> String {
match self.mode {
Mode::Mysql => {
format!("mysql://{}:{}@{}:{}/{}", self.username, self.userpass, self.hostname, self.hostport, self.database)
}
Mode::Sqlite => {
let db_path = self.database.as_str();
let path_buf = PathBuf::from(db_path);
if !path_buf.is_file() {
fs::create_dir_all(db_path.trim_end_matches(path_buf.file_name().unwrap().to_str().unwrap())).unwrap();
}
format!("{}", path_buf.to_str().unwrap())
}
Mode::Mssql => format!("sqlsrv://{}:{}@{}:{}/{}", self.username, self.userpass, self.hostname, self.hostport, self.database),
}
}
}