so://phie

verbal

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]

1use serde::Deserialize;

2use std::fmt::Write;

3use std::fs;

4

5use crate::error::Error;

6use crate::Result;

7use crate::ToUrl;

8use crate::APP_NAME;

9

10#[derive(Debug, Clone, Deserialize)]

11pub 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}

18

19impl 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 true

29 }

30}

31

32impl 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}

39

40#[derive(Debug, Clone, Deserialize)]

41pub struct Options {

42 pub origin: String,

43}

44

45#[derive(Debug, Clone, Deserialize)]

46pub struct Database {

47 pub hostname: String,

48 pub port: i32,

49 pub username: String,

50 pub password: String,

51 pub database: String,

52}

53

54impl 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}

65

66#[derive(Debug, Clone, Deserialize)]

67pub struct Config {

68 pub server: Server,

69 pub database: Database,

70}

71

72impl 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}