A mobile app for listening to radio stations from around the globe
git clone git://soophie.de/verbal
blob: ca3abc6152064c1f4ed0812c4b3d35598673d737 (1983 bytes)[doc][blame][raw]
1
use serde::Deserialize;2
use std::fmt::Write;3
use std::fs;5
use crate::error::Error;6
use crate::Result;7
use crate::ToUrl;8
use crate::APP_NAME;10
#[derive(Debug, Clone, Deserialize)]11
pub struct Server {12
pub hostname: String,13
pub port: i32,14
pub cert_path: Option<String>,15
pub key_path: Option<String>,16
pub options: Options,17
}19
impl Server {20
pub fn is_tls(&self) -> bool {21
if !443.eq(&self.port) {22
return false;23
}24
if self.cert_path.is_none() || self.key_path.is_none() {25
warn!("the port 443 is usually used for TLS only!");26
return false;27
}28
true29
}30
}32
impl ToUrl for Server {33
fn to_url(&self) -> Result<String> {34
let mut url = String::new();35
write!(url, "{}:{}", self.hostname, self.port)?;36
Ok(url)37
}38
}40
#[derive(Debug, Clone, Deserialize)]41
pub struct Options {42
pub origin: String,43
}45
#[derive(Debug, Clone, Deserialize)]46
pub struct Database {47
pub hostname: String,48
pub port: i32,49
pub username: String,50
pub password: String,51
pub database: String,52
}54
impl ToUrl for Database {55
fn to_url(&self) -> Result<String> {56
let mut url = String::new();57
write!(58
url,59
"postgres://{}:{}@{}:{}/{}?sslmode=disable",60
self.username, self.password, self.hostname, self.port, self.database,61
)?;62
Ok(url)63
}64
}66
#[derive(Debug, Clone, Deserialize)]67
pub struct Config {68
pub server: Server,69
pub database: Database,70
}72
impl Config {73
pub fn acquire() -> Result<Self> {74
let mut config_path = match dirs::config_dir() {75
Some(config_path) => config_path,76
None => return Err(Error::new("unable to determine config path!")),77
};78
config_path.push(APP_NAME);79
config_path.push("config.toml");80
let raw = fs::read_to_string(&config_path)?;81
let config: Self = toml::from_str(&raw)?;82
Ok(config)83
}84
}