Can read config files now
- Now reads config from `$XDG_CONFIG_HOME/.config/snake/snake.yaml` - Added error enum from config and try to remove all usages of unwrap - Bumped version to 0.2.3
This commit is contained in:
+16
-13
@@ -1,7 +1,5 @@
|
||||
extern crate dirs;
|
||||
extern crate serde;
|
||||
// extern crate serde_derive;
|
||||
extern crate toml;
|
||||
use serde::{Deserialize, Serialize};
|
||||
// use serde_derive::{Deserialize, Serialize};
|
||||
use crate::game::Difficulty;
|
||||
@@ -10,21 +8,26 @@ use std::io::{Read, Write};
|
||||
use std::path::PathBuf;
|
||||
use std::thread::sleep;
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct Config {
|
||||
pub difficulty: Difficulty,
|
||||
pub speed: u32,
|
||||
pub speed: f32,
|
||||
}
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
pub enum ConfigError {
|
||||
FileSystemError(std::io::Error),
|
||||
ParsingError(serde_yaml::Error),
|
||||
}
|
||||
|
||||
impl Config {
|
||||
pub fn new() -> Result<Self, std::io::Error> {
|
||||
pub fn new() -> Result<Self, ConfigError> {
|
||||
let config_path = Config::config_path().unwrap();
|
||||
if config_path.exists() {
|
||||
let mut config_file = File::open(config_path).unwrap();
|
||||
let mut config_file_str: String = String::new();
|
||||
config_file.read_to_string(&mut config_file_str).unwrap();
|
||||
// let config: Config = toml::from_str(&config_file_str).expect("Error in config file");
|
||||
let config: Config = match toml::from_str(&config_file_str) {
|
||||
let config: Config = match serde_yaml::from_str(&config_file_str) {
|
||||
Ok(config) => config,
|
||||
Err(e) => {
|
||||
eprintln!(
|
||||
@@ -33,7 +36,7 @@ impl Config {
|
||||
e,
|
||||
);
|
||||
eprintln!("Falling back to using the default values");
|
||||
sleep(std::time::Duration::from_millis(3000));
|
||||
sleep(std::time::Duration::from_millis(1000));
|
||||
Config::default()
|
||||
}
|
||||
};
|
||||
@@ -46,11 +49,11 @@ impl Config {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
difficulty: Difficulty::Flat, //Default set to ten blocks per second
|
||||
speed: 10,
|
||||
speed: 10_f32,
|
||||
}
|
||||
}
|
||||
pub fn write(&self) {
|
||||
let config_file_str: String = toml::to_string(self).unwrap();
|
||||
pub fn _write(&self) {
|
||||
let config_file_str: String = serde_yaml::to_string(self).unwrap();
|
||||
let config_path = Config::config_path().unwrap();
|
||||
let mut config_file: File =
|
||||
File::create(config_path).expect("Couldn't open config file to write");
|
||||
@@ -62,13 +65,13 @@ impl Config {
|
||||
let config_path: PathBuf = match dirs::config_dir() {
|
||||
Some(mut path) => {
|
||||
path.push("snake");
|
||||
path.push("snake.toml");
|
||||
path.push("snake.yaml");
|
||||
path
|
||||
}
|
||||
None => {
|
||||
return Err(std::io::Error::new(
|
||||
std::io::ErrorKind::NotFound,
|
||||
"snake.toml not found",
|
||||
"snake.yaml not found",
|
||||
));
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user