Additional cleanup and logic improvement
Browse files
+30 −27 src/bin/main.rs
| @@ -1,3 +1,4 @@ | ||
| 1 | +use std::path::Path; | |
| 1 | 2 | use std::path::PathBuf; |
| 2 | 3 | |
| 3 | 4 | static CONFIG_FILE: &str = "config.lua"; |
| @@ -16,17 +17,8 @@ fn main() { | ||
| 16 | 17 | Args::Error(e) => panic!("Could not get valid arguments:\n{}", e), |
| 17 | 18 | }; |
| 18 | 19 | |
| 19 | - let config_path = match arguments.get(2) { | |
| 20 | - Some(p) => PathBuf::from(p), | |
| 21 | - None => { | |
| 22 | - let config_directory = get_config_path(); | |
| 23 | - let config_path = config_directory.join(CONFIG_FILE); | |
| 24 | - PathBuf::from(config_path) | |
| 25 | - } | |
| 26 | - }; | |
| 27 | - | |
| 28 | - let (config, had_broken_config) = match load_config(config_path) { | |
| 29 | - Ok((c, b)) => (c, b), | |
| 20 | + let (config, had_broken_config) = match load_config(arguments.get(2)) { | |
| 21 | + Ok((c, hbc)) => (c, hbc), | |
| 30 | 22 | Err(e) => panic!("Could not load config:\n{}", e), |
| 31 | 23 | }; |
| 32 | 24 | |
| @@ -55,13 +47,23 @@ fn main() { | ||
| 55 | 47 | } |
| 56 | 48 | } |
| 57 | 49 | |
| 58 | -fn load_config(config_path: PathBuf) -> Result<(oxwm::Config, bool), Box<dyn std::error::Error>> { | |
| 59 | - check_convert(&config_path) | |
| 60 | - .map_err(|error| format!("Failed to check old config:\n{}", error))?; | |
| 61 | - let config_string = std::fs::read_to_string(&config_path) | |
| 50 | +fn load_config( | |
| 51 | + config_path: Option<&String>, | |
| 52 | +) -> Result<(oxwm::Config, bool), Box<dyn std::error::Error>> { | |
| 53 | + let path = match config_path { | |
| 54 | + None => { | |
| 55 | + let config_path = get_config_path().join(CONFIG_FILE); | |
| 56 | + check_convert(&config_path) | |
| 57 | + .map_err(|error| format!("Failed to check old config:\n{}", error))?; | |
| 58 | + config_path | |
| 59 | + } | |
| 60 | + Some(p) => PathBuf::from(p), | |
| 61 | + }; | |
| 62 | + | |
| 63 | + let config_string = std::fs::read_to_string(&path) | |
| 62 | 64 | .map_err(|error| format!("Failed to read config file:\n{}", error))?; |
| 63 | 65 | |
| 64 | - let config_directory = config_path.parent(); | |
| 66 | + let config_directory = path.parent(); | |
| 65 | 67 | |
| 66 | 68 | match oxwm::config::parse_lua_config(&config_string, config_directory) { |
| 67 | 69 | Ok(config) => Ok((config, false)), |
| @@ -77,8 +79,8 @@ fn init_config() -> Result<(), Box<dyn std::error::Error>> { | ||
| 77 | 79 | let config_directory = get_config_path(); |
| 78 | 80 | std::fs::create_dir_all(&config_directory)?; |
| 79 | 81 | |
| 80 | - let config_template = include_str!("../../templates/config.lua"); | |
| 81 | - let config_path = config_directory.join("config.lua"); | |
| 82 | + let config_template = TEMPLATE; | |
| 83 | + let config_path = config_directory.join(CONFIG_FILE); | |
| 82 | 84 | std::fs::write(&config_path, config_template)?; |
| 83 | 85 | |
| 84 | 86 | println!("✓ Config created at {:?}", config_path); |
| @@ -114,12 +116,13 @@ fn print_help() { | ||
| 114 | 116 | } |
| 115 | 117 | |
| 116 | 118 | fn process_args() -> Args { |
| 117 | - let name = match std::env::args().nth(0) { | |
| 119 | + let mut args = std::env::args(); | |
| 120 | + let name = match args.next() { | |
| 118 | 121 | Some(n) => n, |
| 119 | 122 | None => return Args::Error("Program name can't be extracted from args".to_string()), |
| 120 | 123 | }; |
| 121 | - let switch = std::env::args().nth(1); | |
| 122 | - let path = std::env::args().nth(2); | |
| 124 | + let switch = args.next(); | |
| 125 | + let path = args.next(); | |
| 123 | 126 | |
| 124 | 127 | let switch = match switch { |
| 125 | 128 | Some(s) => s, |
| @@ -135,14 +138,14 @@ fn process_args() -> Args { | ||
| 135 | 138 | print_help(); |
| 136 | 139 | Args::Exit |
| 137 | 140 | } |
| 138 | - "--init" => { | |
| 139 | - init_config().expect("Failed to create default config"); | |
| 140 | - Args::Exit | |
| 141 | - } | |
| 141 | + "--init" => match init_config() { | |
| 142 | + Ok(_) => Args::Exit, | |
| 143 | + Err(e) => Args::Error(format!("Failed to create default config:\n{e}")), | |
| 144 | + }, | |
| 142 | 145 | "--config" => { |
| 143 | 146 | if let Some(path) = path |
| 144 | 147 | && std::fs::exists(&path).is_ok() |
| 145 | - && std::fs::exists(&path).unwrap() == true | |
| 148 | + && std::fs::exists(&path).unwrap() | |
| 146 | 149 | { |
| 147 | 150 | Args::Arguments(vec![name, switch, path]) |
| 148 | 151 | } else { |
| @@ -153,7 +156,7 @@ fn process_args() -> Args { | ||
| 153 | 156 | } |
| 154 | 157 | } |
| 155 | 158 | |
| 156 | -fn check_convert(path: &PathBuf) -> Result<(), &str> { | |
| 159 | +fn check_convert(path: &Path) -> Result<(), &str> { | |
| 157 | 160 | let config_directory = get_config_path(); |
| 158 | 161 | |
| 159 | 162 | if !path.exists() { |