pillow_config/
lib.rs

1use std::fs;
2
3use serde::{Deserialize, Serialize};
4
5pub fn get_config() -> Configuration {
6    let filename = "pillow.toml";
7
8    let contents = match fs::read_to_string(filename) {
9        Ok(c) => c,
10
11        Err(err) => {
12            panic!("Could not read file {} {}", filename, err);
13        }
14    };
15    let configuration: Configuration = match toml::from_str(&contents) {
16        Ok(c) => c,
17
18        Err(err) => panic!("Unable to load data from {} {}", filename, err),
19    };
20
21    configuration
22}
23
24#[derive(Debug, Deserialize, Serialize)]
25pub struct Configuration {
26    app: Option<App>,
27
28    router: Option<Router>,
29
30    server: Option<Server>,
31
32    database: Option<Database>,
33}
34
35impl Configuration {
36    pub fn app(self) -> App {
37        match self.app {
38            Some(app) => app,
39
40            None => App {
41                name: String::from("Pillow"),
42                debug: true,
43            },
44        }
45    }
46
47    pub fn server(self) -> Server {
48        match self.server {
49            Some(s) => s,
50
51            None => Server {
52                port: 3000,
53                url: String::from("http://localhost"),
54                address: [127, 0, 0, 1],
55                ssl: None,
56            },
57        }
58    }
59
60    pub fn router() {}
61
62    pub fn database(self) -> Database {
63        self.database.unwrap()
64    }
65}
66
67#[derive(Debug, Deserialize, Serialize)]
68pub struct App {
69    pub name: String,
70    pub debug: bool,
71}
72
73#[derive(Debug, Deserialize, Serialize)]
74pub struct Router {
75    directory: Directory,
76}
77
78#[derive(Debug, Deserialize, Serialize)]
79pub struct Directory {
80    resourcess: String,
81    views: String,
82    js: String,
83    css: String,
84}
85
86#[derive(Debug, Deserialize, Serialize)]
87pub struct Server {
88    pub port: u16,
89    pub url: String,
90    pub address: [u8; 4],
91    ssl: Option<Ssl>,
92}
93
94impl Server {
95    pub fn ssl(self) -> Option<Ssl> {
96        self.ssl
97    }
98}
99
100#[derive(Debug, Deserialize, Serialize)]
101pub struct Ssl {
102    pub cert: String,
103    pub key: String,
104}
105
106#[derive(Debug, Deserialize, Serialize)]
107pub struct Database {
108    pub connection: String,
109    pub port: u16,
110    pub name: String,
111    pub user: String,
112    pub password: String,
113}