1use std::collections::BTreeMap;
2use std::fs;
3use std::path::PathBuf;
4use json::{object, JsonValue};
5use serde::{Deserialize, Serialize};
6
7#[derive(Clone, Debug, Deserialize, Serialize)]
8pub struct Config {
9 pub default: String,
10 pub connections: BTreeMap<String, Connection>,
11}
12
13impl Default for Config {
14 fn default() -> Self {
15 Self::new()
16 }
17}
18
19impl Config {
20 pub fn create(config_file: PathBuf, pkg_name: bool) -> Config {
24 #[derive(Clone, Debug, Deserialize, Serialize)]
25 pub struct ConfigNew {
26 pub br_cache: Config,
27 }
28 impl ConfigNew {
29 pub fn new() -> ConfigNew {
30 let mut connections = BTreeMap::new();
31 connections.insert("my_name".to_string(), Connection::default());
32 Self {
33 br_cache: Config {
34 default: "my_name".to_string(),
35 connections,
36 },
37 }
38 }
39 }
40 match fs::read_to_string(config_file.clone()) {
41 Ok(e) => {
42 if pkg_name {
43 let data = ConfigNew::new();
44 toml::from_str::<ConfigNew>(&e).unwrap_or_else(|_| {
45 let toml = toml::to_string(&data).unwrap();
46 let toml = format!("{e}\r\n{toml}");
47 let _ = fs::write(config_file.to_str().unwrap(), toml);
48 data
49 }).br_cache
50 } else {
51 Config::new()
52 }
53 }
54 Err(_) => {
55 if pkg_name {
56 let data = ConfigNew::new();
57 fs::create_dir_all(config_file.parent().unwrap()).unwrap();
58 let toml = toml::to_string(&data).unwrap();
59 let _ = fs::write(config_file.to_str().unwrap(), toml);
60 data.br_cache
61 } else {
62 let data = Config::new();
63 fs::create_dir_all(config_file.parent().unwrap()).unwrap();
64 let toml = toml::to_string(&data).unwrap();
65 let _ = fs::write(config_file.to_str().unwrap(), toml);
66 data
67 }
68 }
69 }
70 }
71 pub fn new() -> Config {
72 let mut connections = BTreeMap::new();
73 connections.insert("my_name".to_string(), Connection::default());
74 Self {
75 default: "my_name".to_string(),
76 connections,
77 }
78 }
79}
80
81#[derive(Clone, Debug, Serialize, Deserialize)]
82pub struct Connection {
83 pub mode: CacheMode,
84 pub hostname: String,
85 pub hostport: String,
86 pub userpass: String,
87}
88impl Default for Connection {
89 fn default() -> Self {
90 Self {
91 mode: CacheMode::Redis,
92 hostname: "127.0.0.1".to_string(),
93 hostport: "6379".to_string(),
94 userpass: "".to_string(),
95 }
96 }
97}
98impl Connection {
99 pub fn from(data: JsonValue) -> Connection {
100 Self {
101 mode: CacheMode::from(data["mode"].as_str().unwrap_or("")),
102 hostname: data["hostname"].to_string(),
103 hostport: data["hostport"].to_string(),
104 userpass: data["userpass"].to_string(),
105 }
106 }
107 pub fn json(&mut self) -> JsonValue {
108 let mut data = object! {};
109 data["mode"] = self.mode.str().into();
110 data["hostname"] = self.hostname.clone().into();
111 data["hostport"] = self.hostport.clone().into();
112 data["userpass"] = self.userpass.clone().into();
113 data
114 }
115}
116
117#[derive(Clone, Debug, Serialize, Deserialize)]
118pub enum CacheMode {
119 Redis,
120 None,
121}
122
123impl CacheMode {
124 pub fn str(&mut self) -> &'static str {
125 match self {
126 CacheMode::Redis => "redis",
127 CacheMode::None => ""
128 }
129 }
130 pub fn from(name: &str) -> Self {
131 match name {
132 "redis" => CacheMode::Redis,
133 _ => CacheMode::Redis,
134 }
135 }
136}