diff --git a/.gitignore b/.gitignore index 2923626..cfb26c2 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ /target /.idea -/我是崩服小能手 \ No newline at end of file +/我是崩服小能手 +config.json +*.txt \ No newline at end of file diff --git a/src/configuration.rs b/src/configuration.rs index f2dc23a..7f3f576 100644 --- a/src/configuration.rs +++ b/src/configuration.rs @@ -1,6 +1,8 @@ +use std::fs; use std::fs::File; use serde::{Deserialize, Serialize}; -use std::io::BufReader; +use std::io::{BufReader, Write}; +use tokio::io::AsyncWriteExt; #[derive(Debug, Deserialize, Serialize)] pub struct Configuration { @@ -18,6 +20,7 @@ pub struct Configuration { pub use_thread_pool: bool } +const DEFAULT_CONFIG: &str = include_str!("default/config.json"); impl Configuration { pub fn new(filename: &str) -> Self { let file = File::open(filename); @@ -40,7 +43,20 @@ impl Configuration { } } 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() } } diff --git a/config.json b/src/default/config.json similarity index 100% rename from config.json rename to src/default/config.json diff --git a/test.txt b/test.txt deleted file mode 100644 index 9e7b180..0000000 --- a/test.txt +++ /dev/null @@ -1,5 +0,0 @@ -我奶猪好燃 -猪好燃的猪 -猪好燃的好 -猪好燃的燃 -没错,我叫猪好燃 \ No newline at end of file