tony / oxwm

Standardized errors and made some of the logic more concise. Browse files
Scott DeJong committed 2025-12-15 parent 5936396 commit 7fe603c531841b45bfc642d770a58750f4720d17
Showing 1 changed file with 104 additions and 67 deletions. UnifiedSplit
+104 −67 src/bin/main.rs
@@ -1,43 +1,48 @@
1 1 use std::path::PathBuf;
2 2
3 -fn main() -> Result<(), Box<dyn std::error::Error>> {
4 - let arguments: Vec<String> = std::env::args().collect();
3 +static CONFIG_FILE: &str = "config.lua";
4 +static TEMPLATE: &str = include_str!("../../templates/config.lua");
5 5
6 - let mut custom_config_path: Option<PathBuf> = None;
6 +enum Args {
7 + Exit,
8 + Arguments(Vec<String>),
9 + Error(String),
10 +}
7 11
8 - match arguments.get(1).map(|string| string.as_str()) {
9 - Some("--version") => {
10 - println!("oxwm {}", env!("CARGO_PKG_VERSION"));
11 - return Ok(());
12 - }
13 - Some("--help") => {
14 - print_help();
15 - return Ok(());
16 - }
17 - Some("--init") => {
18 - init_config()?;
19 - return Ok(());
20 - }
21 - Some("--config") => {
22 - if let Some(path) = arguments.get(2) {
23 - custom_config_path = Some(PathBuf::from(path));
24 - } else {
25 - eprintln!("Error: --config requires a path argument");
26 - std::process::exit(1);
27 - }
12 +fn main() {
13 + let arguments = match process_args() {
14 + Args::Exit => return,
15 + Args::Arguments(v) => v,
16 + Args::Error(e) => panic!("Could not get valid arguments:\n{}", e),
17 + };
18 +
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)
28 25 }
29 - _ => {}
30 - }
26 + };
31 27
32 - let (config, had_broken_config) = load_config(custom_config_path)?;
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),
31 + };
33 32
34 - let mut window_manager = oxwm::window_manager::WindowManager::new(config)?;
33 + let mut window_manager = match oxwm::window_manager::WindowManager::new(config) {
34 + Ok(wm) => wm,
35 + Err(e) => panic!("Could not start window manager:\n{}", e),
36 + };
35 37
36 38 if had_broken_config {
37 39 window_manager.show_migration_overlay();
38 40 }
39 41
40 - let should_restart = window_manager.run()?;
42 + let should_restart = match window_manager.run() {
43 + Ok(sr) => sr,
44 + Err(e) => panic!("{}", e),
45 + };
41 46
42 47 drop(window_manager);
43 48
@@ -48,53 +53,21 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
48 53 .exec();
49 54 eprintln!("Failed to restart: {}", error);
50 55 }
51 -
52 - Ok(())
53 56 }
54 57
55 -fn load_config(
56 - custom_path: Option<PathBuf>,
57 -) -> Result<(oxwm::Config, bool), Box<dyn std::error::Error>> {
58 - let config_path = if let Some(path) = custom_path {
59 - path
60 - } else {
61 - let config_directory = get_config_path();
62 - let lua_path = config_directory.join("config.lua");
63 -
64 - if !lua_path.exists() {
65 - let ron_path = config_directory.join("config.ron");
66 - let had_ron_config = ron_path.exists();
67 -
68 - println!("No config found at {:?}", config_directory);
69 - println!("Creating default Lua config...");
70 - init_config()?;
71 -
72 - if had_ron_config {
73 - println!("\n NOTICE: OXWM has migrated to Lua configuration.");
74 - println!(" Your old config.ron has been preserved, but is no longer used.");
75 - println!(" Your settings have been reset to defaults.");
76 - println!(" Please manually port your configuration to the new Lua format.");
77 - println!(" See the new config.lua template for examples.\n");
78 - }
79 - }
80 -
81 - lua_path
82 - };
83 -
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))?;
84 61 let config_string = std::fs::read_to_string(&config_path)
85 - .map_err(|error| format!("Failed to read config file: {}", error))?;
62 + .map_err(|error| format!("Failed to read config file:\n{}", error))?;
86 63
87 64 let config_directory = config_path.parent();
88 65
89 66 match oxwm::config::parse_lua_config(&config_string, config_directory) {
90 - Ok(mut config) => {
91 - config.path = Some(config_path);
92 - Ok((config, false))
93 - }
67 + Ok(config) => Ok((config, false)),
94 68 Err(_error) => {
95 - let template = include_str!("../../templates/config.lua");
96 - let config = oxwm::config::parse_lua_config(template, None)
97 - .map_err(|error| format!("Failed to parse default template config: {}", error))?;
69 + let config = oxwm::config::parse_lua_config(TEMPLATE, None)
70 + .map_err(|error| format!("Failed to parse default template config:\n{}", error))?;
98 71 Ok((config, true))
99 72 }
100 73 }
@@ -139,3 +112,67 @@ fn print_help() {
139 112 println!(" Run 'oxwm --init' to create a config file");
140 113 println!(" Or just start oxwm and it will create one automatically\n");
141 114 }
115 +
116 +fn process_args() -> Args {
117 + let name = match std::env::args().nth(0) {
118 + Some(n) => n,
119 + None => return Args::Error("Program name can't be extracted from args".to_string()),
120 + };
121 + let switch = std::env::args().nth(1);
122 + let path = std::env::args().nth(2);
123 +
124 + let switch = match switch {
125 + Some(s) => s,
126 + None => return Args::Arguments(vec![name]),
127 + };
128 +
129 + match switch.as_str() {
130 + "--version" => {
131 + println!("{name} {}", env!("CARGO_PKG_VERSION"));
132 + Args::Exit
133 + }
134 + "--help" => {
135 + print_help();
136 + Args::Exit
137 + }
138 + "--init" => {
139 + init_config().expect("Failed to create default config");
140 + Args::Exit
141 + }
142 + "--config" => {
143 + if let Some(path) = path
144 + && std::fs::exists(&path).is_ok()
145 + && std::fs::exists(&path).unwrap() == true
146 + {
147 + Args::Arguments(vec![name, switch, path])
148 + } else {
149 + Args::Error("Error: --config requires a valid path argument".to_string())
150 + }
151 + }
152 + _ => Args::Error(format!("Error: {switch} is an unknown argument")),
153 + }
154 +}
155 +
156 +fn check_convert(path: &PathBuf) -> Result<(), &str> {
157 + let config_directory = get_config_path();
158 +
159 + if !path.exists() {
160 + let ron_path = config_directory.join("config.ron");
161 + let had_ron_config = ron_path.exists();
162 +
163 + println!("No config found at {:?}", config_directory);
164 + println!("Creating default Lua config...");
165 + if init_config().is_err() {
166 + return Err("Failed to create default lua");
167 + }
168 +
169 + if had_ron_config {
170 + println!("\n NOTICE: OXWM has migrated to Lua configuration.");
171 + println!(" Your old config.ron has been preserved, but is no longer used.");
172 + println!(" Your settings have been reset to defaults.");
173 + println!(" Please manually port your configuration to the new Lua format.");
174 + println!(" See the new config.lua template for examples.\n");
175 + }
176 + }
177 + Ok(())
178 +}