现在config.json会自动生成
This commit is contained in:
2
.gitignore
vendored
2
.gitignore
vendored
@@ -1,3 +1,5 @@
|
|||||||
/target
|
/target
|
||||||
/.idea
|
/.idea
|
||||||
/我是崩服小能手
|
/我是崩服小能手
|
||||||
|
config.json
|
||||||
|
*.txt
|
||||||
@@ -1,6 +1,8 @@
|
|||||||
|
use std::fs;
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use std::io::BufReader;
|
use std::io::{BufReader, Write};
|
||||||
|
use tokio::io::AsyncWriteExt;
|
||||||
|
|
||||||
#[derive(Debug, Deserialize, Serialize)]
|
#[derive(Debug, Deserialize, Serialize)]
|
||||||
pub struct Configuration {
|
pub struct Configuration {
|
||||||
@@ -18,6 +20,7 @@ pub struct Configuration {
|
|||||||
pub use_thread_pool: bool
|
pub use_thread_pool: bool
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const DEFAULT_CONFIG: &str = include_str!("default/config.json");
|
||||||
impl Configuration {
|
impl Configuration {
|
||||||
pub fn new(filename: &str) -> Self {
|
pub fn new(filename: &str) -> Self {
|
||||||
let file = File::open(filename);
|
let file = File::open(filename);
|
||||||
@@ -40,7 +43,20 @@ impl Configuration {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err(error) => {
|
Err(error) => {
|
||||||
eprintln!("[MAIN/CONF] Unable to read config file, will use default values: {}", error);
|
eprintln!("[MAIN/CONF] can't read configuration: {}", error);
|
||||||
|
println!("[MAIN/CONF] creating new config.json");
|
||||||
|
|
||||||
|
match File::create("config.json") {
|
||||||
|
Ok(mut file) => {
|
||||||
|
if let Err(e) = file.write_all(DEFAULT_CONFIG.as_bytes()) {
|
||||||
|
eprintln!("[MAIN/CONF] can't write config.json: {}", e);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
Err(e) => {
|
||||||
|
eprintln!("[MAIN/CONF] can't create config.json: {}", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Self::default()
|
Self::default()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user