-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8abd3f9
commit ee351c8
Showing
7 changed files
with
197 additions
and
9 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
use std::env; | ||
use std::fmt::{Display, Formatter, Write}; | ||
use std::fs::{read_to_string, File}; | ||
|
||
use knuffel::Decode; | ||
use knuffel::Error; | ||
|
||
#[derive(Decode, Copy, Clone)] | ||
pub enum Config { | ||
Performance(Performance), | ||
} | ||
|
||
#[derive(Decode, Copy, Clone)] | ||
pub struct Performance { | ||
#[knuffel(property)] | ||
pub show_fps: bool, | ||
|
||
#[knuffel(property)] | ||
pub fps_cap: u8, | ||
} | ||
|
||
impl Display for Performance { | ||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { | ||
f.write_str(&*format!( | ||
"Performance(show_fps={}, fps_cap={})", | ||
self.show_fps, self.fps_cap | ||
)) | ||
} | ||
} | ||
|
||
pub fn load_config() -> Config { | ||
let mut current_path = | ||
env::current_exe().expect("could not load current directory for configuration loading"); | ||
current_path.pop(); // removes the binary | ||
current_path.push("config.kdl"); | ||
|
||
if !current_path.as_path().exists() { | ||
File::create(¤t_path).unwrap(); | ||
} | ||
|
||
let config = parse_config( | ||
¤t_path | ||
.to_str() | ||
.expect("could not parse config") | ||
.to_string(), | ||
) | ||
.unwrap(); | ||
|
||
*config | ||
.iter() | ||
.nth(0) | ||
.unwrap_or(&Config::Performance(Performance { | ||
show_fps: true, | ||
fps_cap: 144, | ||
})) | ||
} | ||
|
||
fn parse_config(path: &String) -> Result<Vec<Config>, Error> { | ||
let text = read_to_string(path).unwrap(); | ||
|
||
knuffel::parse::<Vec<Config>>(&*path, &text) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters