so://phie

verbal

A mobile app for listening to radio stations from around the globe
git clone git://soophie.de/verbal

blob: 3115f29770d6a18974adec45c97c21e3e7dd0962 (1520 bytes)[doc][blame][raw]

1use serde::Deserialize;

2use std::fmt;

3use std::fs;

4

5use crate::error::Error;

6use crate::Result;

7use crate::APP_NAME;

8

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

10pub struct Server {

11 pub hostname: String,

12 pub port: i32,

13 pub options: Options,

14}

15

16impl fmt::Display for Server {

17 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {

18 write!(f, "{}:{}", self.hostname, self.port,)

19 }

20}

21

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

23pub struct Options {

24 pub origin: String,

25}

26

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

28pub struct Database {

29 pub hostname: String,

30 pub port: i32,

31 pub username: String,

32 pub password: String,

33 pub database: String,

34}

35

36impl fmt::Display for Database {

37 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {

38 write!(

39 f,

40 "postgres://{}:{}@{}:{}/{}?sslmode=disable",

41 self.username, self.password, self.hostname, self.port, self.database,

42 )

43 }

44}

45

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

47pub struct Config {

48 pub server: Server,

49 pub database: Database,

50}

51

52impl Config {

53 pub fn acquire() -> Result<Self> {

54 let mut config_path = match dirs::config_dir() {

55 Some(config_path) => config_path,

56 None => return Err(Error::new("unable to determine config path!")),

57 };

58 config_path.push(APP_NAME);

59 config_path.push("config.toml");

60 let raw = fs::read_to_string(&config_path)?;

61 let config: Self = toml::from_str(&raw)?;

62 Ok(config)

63 }

64}