aws_iot_device_client/
config.rs

1use std::{env, fs, io, path};
2
3use once_cell::sync::OnceCell;
4use serde::Deserialize;
5
6// const CONFIG_FILE: &str = "configs/dc-testconn-config.toml";
7
8#[derive(Deserialize, Debug)]
9pub struct Config {
10    pub endpoint: String,
11    pub cert: String,
12    pub key: String,
13    pub root_ca: String,
14    pub thing_name: String,
15}
16static CONFIG: OnceCell<Config> = OnceCell::new();
17
18impl Config {
19    pub fn global() -> &'static Config {
20        CONFIG.get().expect("Config is not initialized")
21    }
22
23    fn from_file(p: &str) -> Result<Config, std::io::Error> {
24        let config = fs::read_to_string(p).expect("Something went wrong reading the file");
25        let config: Config = toml::from_str(&config).unwrap();
26        print!("{:#?}", &config);
27        Ok(config)
28    }
29}
30
31pub fn config_init(file: &str) {
32    let mut _config = Config::from_file(file).unwrap();
33
34    CONFIG.set(_config).unwrap();
35}