Skip to main content

entropy_game/
config.rs

1use std::path::PathBuf;
2
3use serde::{Deserialize, Serialize};
4use tokio::{fs::File, io::AsyncReadExt};
5
6use crate::err::RuntimeError;
7
8#[derive(Debug, Deserialize, Default)]
9pub struct Root {
10    pub db: Db,
11    pub http: Http,
12    // pub socket: Socket,
13}
14
15#[derive(Debug, Deserialize)]
16pub struct Db {
17    pub embed: EmbedDb,
18    pub remote: RemoteDb,
19}
20impl Default for Db {
21    fn default() -> Self {
22        Db {
23            embed: EmbedDb {
24                enable: true,
25                dir: PathBuf::from(".data"),
26                port: 5432,
27                user: "entropy".to_string(),
28                password: "entropy".to_string(),
29                persistent: true,
30                timeout: 15,
31            },
32            remote: RemoteDb {
33                url: "".to_string(),
34            },
35        }
36    }
37}
38
39#[derive(Debug, Deserialize)]
40pub struct EmbedDb {
41    pub enable: bool,
42    pub dir: PathBuf,
43    pub port: u16,
44    pub user: String,
45    pub password: String,
46    pub persistent: bool,
47    pub timeout: u64,
48}
49
50#[derive(Debug, Deserialize)]
51pub struct RemoteDb {
52    pub url: String,
53}
54
55#[derive(Debug, Serialize, Deserialize)]
56pub struct Http {
57    pub enable: bool,
58    pub listen: String,
59    pub port: u16,
60}
61impl Default for Http {
62    fn default() -> Self {
63        Http {
64            enable: true,
65            listen: "0.0.0.0".to_string(),
66            port: 3333,
67        }
68    }
69}
70
71#[derive(Debug, Serialize, Deserialize)]
72pub struct Socket {
73    pub enable: bool,
74    pub listen: String,
75    pub port: u16,
76}
77
78pub async fn read_from_file(path: PathBuf) -> Result<Root, RuntimeError> {
79    let mut file = File::open(path).await?;
80    let mut contents = String::new();
81    file.read_to_string(&mut contents).await?;
82
83    let config: Root = toml::from_str(&contents)?;
84    Ok(config)
85}