Fixed a bug where I was calling std::fs twice and cleaned up error handling a little bit.
Browse files
+34 −22 src/bin/main.rs
| @@ -14,17 +14,17 @@ fn main() { | |||
| 14 | let arguments = match process_args() { | 14 | let arguments = match process_args() { |
| 15 | Args::Exit => return, | 15 | Args::Exit => return, |
| 16 | Args::Arguments(v) => v, | 16 | Args::Arguments(v) => v, |
| 17 | - Args::Error(e) => panic!("Could not get valid arguments:\n{}", e), | 17 | + Args::Error(e) => panic!("Error: Could not get valid arguments:\n{}", e), |
| 18 | }; | 18 | }; |
| 19 | 19 | ||
| 20 | let (config, had_broken_config) = match load_config(arguments.get(2)) { | 20 | let (config, had_broken_config) = match load_config(arguments.get(2)) { |
| 21 | Ok((c, hbc)) => (c, hbc), | 21 | Ok((c, hbc)) => (c, hbc), |
| 22 | - Err(e) => panic!("Could not load config:\n{}", e), | 22 | + Err(e) => panic!("Error: Could not load config:\n{}", e), |
| 23 | }; | 23 | }; |
| 24 | 24 | ||
| 25 | let mut window_manager = match oxwm::window_manager::WindowManager::new(config) { | 25 | let mut window_manager = match oxwm::window_manager::WindowManager::new(config) { |
| 26 | Ok(wm) => wm, | 26 | Ok(wm) => wm, |
| 27 | - Err(e) => panic!("Could not start window manager:\n{}", e), | 27 | + Err(e) => panic!("Error: Could not start window manager:\n{}", e), |
| 28 | }; | 28 | }; |
| 29 | 29 | ||
| 30 | if had_broken_config { | 30 | if had_broken_config { |
| @@ -33,7 +33,7 @@ fn main() { | |||
| 33 | 33 | ||
| 34 | let should_restart = match window_manager.run() { | 34 | let should_restart = match window_manager.run() { |
| 35 | Ok(sr) => sr, | 35 | Ok(sr) => sr, |
| 36 | - Err(e) => panic!("{}", e), | 36 | + Err(e) => panic!("Error: Could not determine restart\n{}", e), |
| 37 | }; | 37 | }; |
| 38 | 38 | ||
| 39 | drop(window_manager); | 39 | drop(window_manager); |
| @@ -43,7 +43,7 @@ fn main() { | |||
| 43 | let error = std::process::Command::new(&arguments[0]) | 43 | let error = std::process::Command::new(&arguments[0]) |
| 44 | .args(&arguments[1..]) | 44 | .args(&arguments[1..]) |
| 45 | .exec(); | 45 | .exec(); |
| 46 | - eprintln!("Failed to restart: {}", error); | 46 | + eprintln!("Error: Failed to restart: {}", error); |
| 47 | } | 47 | } |
| 48 | } | 48 | } |
| 49 | 49 | ||
| @@ -54,22 +54,23 @@ fn load_config( | |||
| 54 | None => { | 54 | None => { |
| 55 | let config_path = get_config_path().join(CONFIG_FILE); | 55 | let config_path = get_config_path().join(CONFIG_FILE); |
| 56 | check_convert(&config_path) | 56 | check_convert(&config_path) |
| 57 | - .map_err(|error| format!("Failed to check old config:\n{}", error))?; | 57 | + .map_err(|error| format!("Error: Failed to check old config:\n{}", error))?; |
| 58 | config_path | 58 | config_path |
| 59 | } | 59 | } |
| 60 | Some(p) => PathBuf::from(p), | 60 | Some(p) => PathBuf::from(p), |
| 61 | }; | 61 | }; |
| 62 | 62 | ||
| 63 | let config_string = std::fs::read_to_string(&path) | 63 | let config_string = std::fs::read_to_string(&path) |
| 64 | - .map_err(|error| format!("Failed to read config file:\n{}", error))?; | 64 | + .map_err(|error| format!("Error: Failed to read config file:\n{}", error))?; |
| 65 | 65 | ||
| 66 | let config_directory = path.parent(); | 66 | let config_directory = path.parent(); |
| 67 | 67 | ||
| 68 | match oxwm::config::parse_lua_config(&config_string, config_directory) { | 68 | match oxwm::config::parse_lua_config(&config_string, config_directory) { |
| 69 | Ok(config) => Ok((config, false)), | 69 | Ok(config) => Ok((config, false)), |
| 70 | Err(_error) => { | 70 | Err(_error) => { |
| 71 | - let config = oxwm::config::parse_lua_config(TEMPLATE, None) | 71 | + let config = oxwm::config::parse_lua_config(TEMPLATE, None).map_err(|error| { |
| 72 | - .map_err(|error| format!("Failed to parse default template config:\n{}", error))?; | 72 | + format!("Error: Failed to parse default template config:\n{}", error) |
| 73 | + })?; | ||
| 73 | Ok((config, true)) | 74 | Ok((config, true)) |
| 74 | } | 75 | } |
| 75 | } | 76 | } |
| @@ -119,7 +120,7 @@ fn process_args() -> Args { | |||
| 119 | let mut args = std::env::args(); | 120 | let mut args = std::env::args(); |
| 120 | let name = match args.next() { | 121 | let name = match args.next() { |
| 121 | Some(n) => n, | 122 | Some(n) => n, |
| 122 | - None => return Args::Error("Program name can't be extracted from args".to_string()), | 123 | + None => return Args::Error("Error: Program name can't be extracted from args".to_string()), |
| 123 | }; | 124 | }; |
| 124 | let switch = args.next(); | 125 | let switch = args.next(); |
| 125 | let path = args.next(); | 126 | let path = args.next(); |
| @@ -140,22 +141,33 @@ fn process_args() -> Args { | |||
| 140 | } | 141 | } |
| 141 | "--init" => match init_config() { | 142 | "--init" => match init_config() { |
| 142 | Ok(_) => Args::Exit, | 143 | Ok(_) => Args::Exit, |
| 143 | - Err(e) => Args::Error(format!("Failed to create default config:\n{e}")), | 144 | + Err(e) => Args::Error(format!("Error: Failed to create default config:\n{e}")), |
| 145 | + }, | ||
| 146 | + "--config" => match check_custom_config(path) { | ||
| 147 | + Ok(p) => Args::Arguments(vec![name, switch, p]), | ||
| 148 | + Err(e) => Args::Error(e), | ||
| 144 | }, | 149 | }, |
| 145 | - "--config" => { | ||
| 146 | - if let Some(path) = path | ||
| 147 | - && std::fs::exists(&path).is_ok() | ||
| 148 | - && std::fs::exists(&path).unwrap() | ||
| 149 | - { | ||
| 150 | - Args::Arguments(vec![name, switch, path]) | ||
| 151 | - } else { | ||
| 152 | - Args::Error("Error: --config requires a valid path argument".to_string()) | ||
| 153 | - } | ||
| 154 | - } | ||
| 155 | _ => Args::Error(format!("Error: {switch} is an unknown argument")), | 150 | _ => Args::Error(format!("Error: {switch} is an unknown argument")), |
| 156 | } | 151 | } |
| 157 | } | 152 | } |
| 158 | 153 | ||
| 154 | +fn check_custom_config(path: Option<String>) -> Result<String, String> { | ||
| 155 | + let path = match path { | ||
| 156 | + Some(p) => p, | ||
| 157 | + None => { | ||
| 158 | + return Err("Error: --config requires a valid path argument".to_string()); | ||
| 159 | + } | ||
| 160 | + }; | ||
| 161 | + | ||
| 162 | + match std::fs::exists(&path) { | ||
| 163 | + Ok(b) => match b { | ||
| 164 | + true => Ok(path), | ||
| 165 | + false => Err(format!("Error: {path} does not exist")), | ||
| 166 | + }, | ||
| 167 | + Err(e) => Err(format!("Error: Failed to check config exists:\n{e}")), | ||
| 168 | + } | ||
| 169 | +} | ||
| 170 | + | ||
| 159 | fn check_convert(path: &Path) -> Result<(), &str> { | 171 | fn check_convert(path: &Path) -> Result<(), &str> { |
| 160 | let config_directory = get_config_path(); | 172 | let config_directory = get_config_path(); |
| 161 | 173 | ||
| @@ -166,7 +178,7 @@ fn check_convert(path: &Path) -> Result<(), &str> { | |||
| 166 | println!("No config found at {:?}", config_directory); | 178 | println!("No config found at {:?}", config_directory); |
| 167 | println!("Creating default Lua config..."); | 179 | println!("Creating default Lua config..."); |
| 168 | if init_config().is_err() { | 180 | if init_config().is_err() { |
| 169 | - return Err("Failed to create default lua"); | 181 | + return Err("Error: Failed to create default lua"); |
| 170 | } | 182 | } |
| 171 | 183 | ||
| 172 | if had_ron_config { | 184 | if had_ron_config { |