| @@ -1,3 +1,4 @@ |
|
|
1 |
+use oxwm::errors::MainError; |
| 1 |
use std::path::Path; |
2 |
use std::path::Path; |
| 2 |
use std::path::PathBuf; |
3 |
use std::path::PathBuf; |
| 3 |
|
4 |
|
| @@ -7,24 +8,21 @@ static TEMPLATE: &str = include_str!("../../templates/config.lua"); |
| 7 |
enum Args { |
8 |
enum Args { |
| 8 |
Exit, |
9 |
Exit, |
| 9 |
Arguments(Vec<String>), |
10 |
Arguments(Vec<String>), |
| 10 |
- Error(String), |
11 |
+ Error(MainError), |
| 11 |
} |
12 |
} |
| 12 |
|
13 |
|
| 13 |
-fn main() { |
14 |
+fn main() -> Result<(), MainError> { |
| 14 |
let arguments = match process_args() { |
15 |
let arguments = match process_args() { |
| 15 |
- Args::Exit => return, |
16 |
+ Args::Exit => return Ok(()), |
| 16 |
Args::Arguments(v) => v, |
17 |
Args::Arguments(v) => v, |
| 17 |
- Args::Error(e) => panic!("Error: Could not get valid arguments:\n{}", e), |
18 |
+ Args::Error(e) => return Err(e), |
| 18 |
}; |
19 |
}; |
| 19 |
|
20 |
|
| 20 |
- let (config, had_broken_config) = match load_config(arguments.get(2)) { |
21 |
+ let (config, had_broken_config) = load_config(arguments.get(2))?; |
| 21 |
- Ok((c, hbc)) => (c, hbc), |
|
|
| 22 |
- Err(e) => panic!("Error: Could not load config:\n{}", e), |
|
|
| 23 |
- }; |
|
|
| 24 |
|
22 |
|
| 25 |
let mut window_manager = match oxwm::window_manager::WindowManager::new(config) { |
23 |
let mut window_manager = match oxwm::window_manager::WindowManager::new(config) { |
| 26 |
Ok(wm) => wm, |
24 |
Ok(wm) => wm, |
| 27 |
- Err(e) => panic!("Error: Could not start window manager:\n{}", e), |
25 |
+ Err(e) => return Err(MainError::CouldNotStartWm(e)), |
| 28 |
}; |
26 |
}; |
| 29 |
|
27 |
|
| 30 |
if had_broken_config { |
28 |
if had_broken_config { |
| @@ -33,7 +31,7 @@ fn main() { |
| 33 |
|
31 |
|
| 34 |
let should_restart = match window_manager.run() { |
32 |
let should_restart = match window_manager.run() { |
| 35 |
Ok(sr) => sr, |
33 |
Ok(sr) => sr, |
| 36 |
- Err(e) => panic!("Error: Could not determine restart\n{}", e), |
34 |
+ Err(e) => return Err(MainError::BadRestartStatus(e)), |
| 37 |
}; |
35 |
}; |
| 38 |
|
36 |
|
| 39 |
drop(window_manager); |
37 |
drop(window_manager); |
| @@ -45,23 +43,24 @@ fn main() { |
| 45 |
.exec(); |
43 |
.exec(); |
| 46 |
eprintln!("Error: Failed to restart: {}", error); |
44 |
eprintln!("Error: Failed to restart: {}", error); |
| 47 |
} |
45 |
} |
|
|
46 |
+ Ok(()) |
| 48 |
} |
47 |
} |
| 49 |
|
48 |
|
| 50 |
-fn load_config( |
49 |
+fn load_config(config_path: Option<&String>) -> Result<(oxwm::Config, bool), MainError> { |
| 51 |
- config_path: Option<&String>, |
|
|
| 52 |
-) -> Result<(oxwm::Config, bool), Box<dyn std::error::Error>> { |
|
|
| 53 |
let path = match config_path { |
50 |
let path = match config_path { |
| 54 |
None => { |
51 |
None => { |
| 55 |
- let config_path = get_config_path().join(CONFIG_FILE); |
52 |
+ let config_dir = get_config_path()?; |
| 56 |
- check_convert(&config_path) |
53 |
+ let config_path = config_dir.join(CONFIG_FILE); |
| 57 |
- .map_err(|error| format!("Error: Failed to check old config:\n{}", error))?; |
54 |
+ check_convert(&config_path)?; |
| 58 |
config_path |
55 |
config_path |
| 59 |
} |
56 |
} |
| 60 |
Some(p) => PathBuf::from(p), |
57 |
Some(p) => PathBuf::from(p), |
| 61 |
}; |
58 |
}; |
| 62 |
|
59 |
|
| 63 |
- let config_string = std::fs::read_to_string(&path) |
60 |
+ let config_string = match std::fs::read_to_string(&path) { |
| 64 |
- .map_err(|error| format!("Error: Failed to read config file:\n{}", error))?; |
61 |
+ Ok(c) => c, |
|
|
62 |
+ Err(e) => return Err(MainError::FailedReadConfig(e)), |
|
|
63 |
+ }; |
| 65 |
|
64 |
|
| 66 |
let config_directory = path.parent(); |
65 |
let config_directory = path.parent(); |
| 67 |
|
66 |
|
| @@ -69,9 +68,10 @@ fn load_config( |
| 69 |
match oxwm::config::parse_lua_config(&config_string, config_directory) { |
68 |
match oxwm::config::parse_lua_config(&config_string, config_directory) { |
| 70 |
Ok(config) => (config, false), |
69 |
Ok(config) => (config, false), |
| 71 |
Err(_error) => { |
70 |
Err(_error) => { |
| 72 |
- let config = oxwm::config::parse_lua_config(TEMPLATE, None).map_err(|error| { |
71 |
+ let config = match oxwm::config::parse_lua_config(TEMPLATE, None) { |
| 73 |
- format!("Error: Failed to parse default template config:\n{}", error) |
72 |
+ Ok(c) => c, |
| 74 |
- })?; |
73 |
+ Err(e) => return Err(MainError::FailedReadConfigTemplate(e)), |
|
|
74 |
+ }; |
| 75 |
(config, true) |
75 |
(config, true) |
| 76 |
} |
76 |
} |
| 77 |
}; |
77 |
}; |
| @@ -79,13 +79,17 @@ fn load_config( |
| 79 |
Ok((config, had_broken_config)) |
79 |
Ok((config, had_broken_config)) |
| 80 |
} |
80 |
} |
| 81 |
|
81 |
|
| 82 |
-fn init_config() -> Result<(), Box<dyn std::error::Error>> { |
82 |
+fn init_config() -> Result<(), MainError> { |
| 83 |
- let config_directory = get_config_path(); |
83 |
+ let config_directory = get_config_path()?; |
| 84 |
- std::fs::create_dir_all(&config_directory)?; |
84 |
+ if let Err(e) = std::fs::create_dir_all(&config_directory) { |
|
|
85 |
+ return Err(MainError::CouldNotCreateConfigDir(e)); |
|
|
86 |
+ } |
| 85 |
|
87 |
|
| 86 |
let config_template = TEMPLATE; |
88 |
let config_template = TEMPLATE; |
| 87 |
let config_path = config_directory.join(CONFIG_FILE); |
89 |
let config_path = config_directory.join(CONFIG_FILE); |
| 88 |
- std::fs::write(&config_path, config_template)?; |
90 |
+ if let Err(e) = std::fs::write(&config_path, config_template) { |
|
|
91 |
+ return Err(MainError::CouldNotWriteConfig(e)); |
|
|
92 |
+ } |
| 89 |
|
93 |
|
| 90 |
println!("✓ Config created at {:?}", config_path); |
94 |
println!("✓ Config created at {:?}", config_path); |
| 91 |
println!(" Edit the file and reload with Mod+Shift+R"); |
95 |
println!(" Edit the file and reload with Mod+Shift+R"); |
| @@ -94,10 +98,11 @@ fn init_config() -> Result<(), Box<dyn std::error::Error>> { |
| 94 |
Ok(()) |
98 |
Ok(()) |
| 95 |
} |
99 |
} |
| 96 |
|
100 |
|
| 97 |
-fn get_config_path() -> PathBuf { |
101 |
+fn get_config_path() -> Result<PathBuf, MainError> { |
| 98 |
- dirs::config_dir() |
102 |
+ match dirs::config_dir() { |
| 99 |
- .expect("Could not find config directory") |
103 |
+ Some(p) => Ok(p.join("oxwm")), |
| 100 |
- .join("oxwm") |
104 |
+ None => Err(MainError::NoConfigDir), |
|
|
105 |
+ } |
| 101 |
} |
106 |
} |
| 102 |
|
107 |
|
| 103 |
fn print_help() { |
108 |
fn print_help() { |
| @@ -123,7 +128,7 @@ fn process_args() -> Args { |
| 123 |
let mut args = std::env::args(); |
128 |
let mut args = std::env::args(); |
| 124 |
let name = match args.next() { |
129 |
let name = match args.next() { |
| 125 |
Some(n) => n, |
130 |
Some(n) => n, |
| 126 |
- None => return Args::Error("Error: Program name can't be extracted from args".to_string()), |
131 |
+ None => return Args::Error(MainError::NoProgramName), |
| 127 |
}; |
132 |
}; |
| 128 |
let switch = args.next(); |
133 |
let switch = args.next(); |
| 129 |
let path = args.next(); |
134 |
let path = args.next(); |
| @@ -144,35 +149,35 @@ fn process_args() -> Args { |
| 144 |
} |
149 |
} |
| 145 |
"--init" => match init_config() { |
150 |
"--init" => match init_config() { |
| 146 |
Ok(_) => Args::Exit, |
151 |
Ok(_) => Args::Exit, |
| 147 |
- Err(e) => Args::Error(format!("Error: Failed to create default config:\n{e}")), |
152 |
+ Err(e) => Args::Error(e), |
| 148 |
}, |
153 |
}, |
| 149 |
"--config" => match check_custom_config(path) { |
154 |
"--config" => match check_custom_config(path) { |
| 150 |
Ok(p) => Args::Arguments(vec![name, switch, p]), |
155 |
Ok(p) => Args::Arguments(vec![name, switch, p]), |
| 151 |
Err(e) => Args::Error(e), |
156 |
Err(e) => Args::Error(e), |
| 152 |
}, |
157 |
}, |
| 153 |
- _ => Args::Error(format!("Error: {switch} is an unknown argument")), |
158 |
+ _ => Args::Error(MainError::InvalidArguments), |
| 154 |
} |
159 |
} |
| 155 |
} |
160 |
} |
| 156 |
|
161 |
|
| 157 |
-fn check_custom_config(path: Option<String>) -> Result<String, String> { |
162 |
+fn check_custom_config(path: Option<String>) -> Result<String, MainError> { |
| 158 |
let path = match path { |
163 |
let path = match path { |
| 159 |
Some(p) => p, |
164 |
Some(p) => p, |
| 160 |
None => { |
165 |
None => { |
| 161 |
- return Err("Error: --config requires a valid path argument".to_string()); |
166 |
+ return Err(MainError::NoConfigPath); |
| 162 |
} |
167 |
} |
| 163 |
}; |
168 |
}; |
| 164 |
|
169 |
|
| 165 |
match std::fs::exists(&path) { |
170 |
match std::fs::exists(&path) { |
| 166 |
Ok(b) => match b { |
171 |
Ok(b) => match b { |
| 167 |
true => Ok(path), |
172 |
true => Ok(path), |
| 168 |
- false => Err(format!("Error: {path} does not exist")), |
173 |
+ false => Err(MainError::BadConfigPath), |
| 169 |
}, |
174 |
}, |
| 170 |
- Err(e) => Err(format!("Error: Failed to check config exists:\n{e}")), |
175 |
+ Err(e) => Err(MainError::FailedCheckExist(e)), |
| 171 |
} |
176 |
} |
| 172 |
} |
177 |
} |
| 173 |
|
178 |
|
| 174 |
-fn check_convert(path: &Path) -> Result<(), &str> { |
179 |
+fn check_convert(path: &Path) -> Result<(), MainError> { |
| 175 |
- let config_directory = get_config_path(); |
180 |
+ let config_directory = get_config_path()?; |
| 176 |
|
181 |
|
| 177 |
if !path.exists() { |
182 |
if !path.exists() { |
| 178 |
let ron_path = config_directory.join("config.ron"); |
183 |
let ron_path = config_directory.join("config.ron"); |
| @@ -180,9 +185,7 @@ fn check_convert(path: &Path) -> Result<(), &str> { |
| 180 |
|
185 |
|
| 181 |
println!("No config found at {:?}", config_directory); |
186 |
println!("No config found at {:?}", config_directory); |
| 182 |
println!("Creating default Lua config..."); |
187 |
println!("Creating default Lua config..."); |
| 183 |
- if init_config().is_err() { |
188 |
+ init_config()?; |
| 184 |
- return Err("Error: Failed to create default lua"); |
|
|
| 185 |
- } |
|
|
| 186 |
|
189 |
|
| 187 |
if had_ron_config { |
190 |
if had_ron_config { |
| 188 |
println!("\n NOTICE: OXWM has migrated to Lua configuration."); |
191 |
println!("\n NOTICE: OXWM has migrated to Lua configuration."); |