新增了config.json实现
This commit is contained in:
68
src/configuration.rs
Normal file
68
src/configuration.rs
Normal file
@@ -0,0 +1,68 @@
|
||||
use std::fs::File;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::io::BufReader;
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize)]
|
||||
pub struct Configuration {
|
||||
server_ip: String,
|
||||
server_port: String,
|
||||
username_prefix: String,
|
||||
count: i32,
|
||||
proxy_url: String,
|
||||
}
|
||||
|
||||
impl Configuration {
|
||||
pub fn new(filename: &str) -> Self {
|
||||
let file = File::open(filename);
|
||||
match file {
|
||||
Ok(file) => {
|
||||
let reader = BufReader::new(file);
|
||||
match serde_json::from_reader::<_,Configuration>(reader) { // serde可以直接解析出实现了Deserialize trait的类型 就很不错
|
||||
Ok(config) => {
|
||||
println!("[CONFIG] Server IP: {}", config.server_ip);
|
||||
println!("[CONFIG] Server Port: {}", config.server_port);
|
||||
println!("[CONFIG] Username Prefix: {}", config.username_prefix);
|
||||
println!("[CONFIG] Bot Count: {}", config.count);
|
||||
println!("[CONFIG] Proxy URL: {}", config.proxy_url);
|
||||
config
|
||||
}
|
||||
Err(error) => {
|
||||
eprintln!("[MAIN/CONF] Error parsing config file: {}", error);
|
||||
Self::default()
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(error) => {
|
||||
eprintln!("[MAIN/CONF] Unable to read config file, will use default values: {}", error);
|
||||
Self::default()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn default() -> Self {
|
||||
Configuration {
|
||||
server_ip: "49.70.3.146".to_string(),
|
||||
server_port: "33009".to_string(),
|
||||
username_prefix: "".to_string(),
|
||||
count: 300,
|
||||
proxy_url: "http://uu-proxy.com/api/get_proxies?id=RZ7GS5ZYTA&size=1&schemes=socks5&support_https=false&format=json".to_string(),
|
||||
}
|
||||
}
|
||||
|
||||
// 下面是getter一堆
|
||||
pub fn server_ip(&self) -> &str {
|
||||
self.server_ip.as_str()
|
||||
}
|
||||
pub fn server_port(&self) -> &str {
|
||||
self.server_port.as_str()
|
||||
}
|
||||
pub fn username_prefix(&self) -> &str {
|
||||
self.username_prefix.as_str()
|
||||
}
|
||||
pub fn count(&self) -> i32 {
|
||||
self.count
|
||||
}
|
||||
pub fn proxy_url(&self) -> &str {
|
||||
self.proxy_url.as_str()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user