proxyster_lib/
util.rs

1use dirs::config_dir;
2use std::fs::read_to_string;
3
4use crate::config::Config;
5
6/**
7 Reads the config file and returns the providers
8
9 Runs the following tests:
10    - generic config directory path exists
11    - generic config directory path is a directory
12    - proxyster config directory path exists
13    - proxyster config directory path is a directory
14    - proxyster providers file path exists
15    - proxyster providers file path is a file
16
17 Then it reads the file and returns the providers
18*/
19pub fn read_config() -> Config {
20    let conf_dir_resolve = config_dir().expect("should find user config directory");
21    let conf_dir = conf_dir_resolve.as_path();
22    assert!(conf_dir.exists(), "user config directory should exist");
23    assert!(
24        conf_dir.is_dir(),
25        "user config directory path should be a directory and not a file"
26    );
27    let dir = conf_dir.join("proxyster");
28    assert!(dir.exists(), "proxyster config directory should exist");
29    assert!(
30        dir.is_dir(),
31        "proxyster config directory path should be a directory and not a file"
32    );
33    let providers_file = dir.join("providers.toml");
34    assert!(
35        providers_file.exists(),
36        "providers config file should exist"
37    );
38    assert!(
39        providers_file.is_file(),
40        "providers config file path should be a file and not a directory"
41    );
42    toml::from_str(&read_to_string(providers_file).unwrap()[..]).unwrap()
43}
44