1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
use std::path::PathBuf; #[derive(Serialize, Deserialize, Debug)] pub struct Settings { pixelsize: u8, pub fullscreen: bool, pub draw_collision_bounds: bool, } impl Default for Settings { fn default() -> Self { Settings { pixelsize: 1, fullscreen: false, draw_collision_bounds: false, } } } impl Settings { fn savepath() -> PathBuf { crate::config_dir().join("settings.toml") } pub fn load_or_create() -> Settings { let path = Self::savepath(); match std::fs::read_to_string(&path) { Ok(s) => match toml::de::from_str(&s) { Ok(settings) => settings, Err(e) => { eprintln!( "Error reading from settings file {:?}: {:?}", path.to_string_lossy(), e ); eprintln!("Using default settings"); Settings::default() } }, Err(e) => { eprintln!( "Error opening settings file {:?}: {:?}", path.to_string_lossy(), e ); eprintln!("Using default settings"); Settings::default() } } } pub fn save(&self) { let config_dir = crate::config_dir(); match std::fs::create_dir_all(&config_dir) { Ok(()) => {} Err(e) => { eprintln!( "Error creating config directory {:?}: {:?}", config_dir.to_string_lossy(), e ); return; } } let savepath = Self::savepath(); match std::fs::write( &savepath, toml::ser::to_string(self).unwrap().as_bytes(), ) { Ok(()) => {} Err(e) => { eprintln!( "Error saving config file {:?}: {:?}", savepath.to_string_lossy(), e ); } } } }