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