tony / oxwm

Branch: 0d527c6
Switch branches/tags
master
Scott DeJong Added custom error handling to the changes to main.rs 0d527c6 2025-12-19
199 lines · 6.2 KB Raw Blame History
1 use oxwm::errors::MainError;
2 use std::path::Path;
3 use std::path::PathBuf;
4
5 static CONFIG_FILE: &str = "config.lua";
6 static TEMPLATE: &str = include_str!("../../templates/config.lua");
7
8 enum Args {
9 Exit,
10 Arguments(Vec<String>),
11 Error(MainError),
12 }
13
14 fn main() -> Result<(), MainError> {
15 let arguments = match process_args() {
16 Args::Exit => return Ok(()),
17 Args::Arguments(v) => v,
18 Args::Error(e) => return Err(e),
19 };
20
21 let (config, had_broken_config) = load_config(arguments.get(2))?;
22
23 let mut window_manager = match oxwm::window_manager::WindowManager::new(config) {
24 Ok(wm) => wm,
25 Err(e) => return Err(MainError::CouldNotStartWm(e)),
26 };
27
28 if had_broken_config {
29 window_manager.show_migration_overlay();
30 }
31
32 let should_restart = match window_manager.run() {
33 Ok(sr) => sr,
34 Err(e) => return Err(MainError::BadRestartStatus(e)),
35 };
36
37 drop(window_manager);
38
39 if should_restart {
40 use std::os::unix::process::CommandExt;
41 let error = std::process::Command::new(&arguments[0])
42 .args(&arguments[1..])
43 .exec();
44 eprintln!("Error: Failed to restart: {}", error);
45 }
46 Ok(())
47 }
48
49 fn load_config(config_path: Option<&String>) -> Result<(oxwm::Config, bool), MainError> {
50 let path = match config_path {
51 None => {
52 let config_dir = get_config_path()?;
53 let config_path = config_dir.join(CONFIG_FILE);
54 check_convert(&config_path)?;
55 config_path
56 }
57 Some(p) => PathBuf::from(p),
58 };
59
60 let config_string = match std::fs::read_to_string(&path) {
61 Ok(c) => c,
62 Err(e) => return Err(MainError::FailedReadConfig(e)),
63 };
64
65 let config_directory = path.parent();
66
67 let (mut config, had_broken_config) =
68 match oxwm::config::parse_lua_config(&config_string, config_directory) {
69 Ok(config) => (config, false),
70 Err(_error) => {
71 let config = match oxwm::config::parse_lua_config(TEMPLATE, None) {
72 Ok(c) => c,
73 Err(e) => return Err(MainError::FailedReadConfigTemplate(e)),
74 };
75 (config, true)
76 }
77 };
78 config.path = Some(path);
79 Ok((config, had_broken_config))
80 }
81
82 fn init_config() -> Result<(), MainError> {
83 let config_directory = get_config_path()?;
84 if let Err(e) = std::fs::create_dir_all(&config_directory) {
85 return Err(MainError::CouldNotCreateConfigDir(e));
86 }
87
88 let config_template = TEMPLATE;
89 let config_path = config_directory.join(CONFIG_FILE);
90 if let Err(e) = std::fs::write(&config_path, config_template) {
91 return Err(MainError::CouldNotWriteConfig(e));
92 }
93
94 println!("✓ Config created at {:?}", config_path);
95 println!(" Edit the file and reload with Mod+Shift+R");
96 println!(" No compilation needed - changes take effect immediately!");
97
98 Ok(())
99 }
100
101 fn get_config_path() -> Result<PathBuf, MainError> {
102 match dirs::config_dir() {
103 Some(p) => Ok(p.join("oxwm")),
104 None => Err(MainError::NoConfigDir),
105 }
106 }
107
108 fn print_help() {
109 println!("OXWM - A dynamic window manager written in Rust\n");
110 println!("USAGE:");
111 println!(" oxwm [OPTIONS]\n");
112 println!("OPTIONS:");
113 println!(" --init Create default config in ~/.config/oxwm/config.lua");
114 println!(" --config <PATH> Use custom config file");
115 println!(" --version Print version information");
116 println!(" --help Print this help message\n");
117 println!("CONFIG:");
118 println!(" Location: ~/.config/oxwm/config.lua");
119 println!(" Edit the config file and use Mod+Shift+R to reload");
120 println!(" No compilation needed - instant hot-reload!");
121 println!(" LSP support included with oxwm.lua type definitions\n");
122 println!("FIRST RUN:");
123 println!(" Run 'oxwm --init' to create a config file");
124 println!(" Or just start oxwm and it will create one automatically\n");
125 }
126
127 fn process_args() -> Args {
128 let mut args = std::env::args();
129 let name = match args.next() {
130 Some(n) => n,
131 None => return Args::Error(MainError::NoProgramName),
132 };
133 let switch = args.next();
134 let path = args.next();
135
136 let switch = match switch {
137 Some(s) => s,
138 None => return Args::Arguments(vec![name]),
139 };
140
141 match switch.as_str() {
142 "--version" => {
143 println!("{name} {}", env!("CARGO_PKG_VERSION"));
144 Args::Exit
145 }
146 "--help" => {
147 print_help();
148 Args::Exit
149 }
150 "--init" => match init_config() {
151 Ok(_) => Args::Exit,
152 Err(e) => Args::Error(e),
153 },
154 "--config" => match check_custom_config(path) {
155 Ok(p) => Args::Arguments(vec![name, switch, p]),
156 Err(e) => Args::Error(e),
157 },
158 _ => Args::Error(MainError::InvalidArguments),
159 }
160 }
161
162 fn check_custom_config(path: Option<String>) -> Result<String, MainError> {
163 let path = match path {
164 Some(p) => p,
165 None => {
166 return Err(MainError::NoConfigPath);
167 }
168 };
169
170 match std::fs::exists(&path) {
171 Ok(b) => match b {
172 true => Ok(path),
173 false => Err(MainError::BadConfigPath),
174 },
175 Err(e) => Err(MainError::FailedCheckExist(e)),
176 }
177 }
178
179 fn check_convert(path: &Path) -> Result<(), MainError> {
180 let config_directory = get_config_path()?;
181
182 if !path.exists() {
183 let ron_path = config_directory.join("config.ron");
184 let had_ron_config = ron_path.exists();
185
186 println!("No config found at {:?}", config_directory);
187 println!("Creating default Lua config...");
188 init_config()?;
189
190 if had_ron_config {
191 println!("\n NOTICE: OXWM has migrated to Lua configuration.");
192 println!(" Your old config.ron has been preserved, but is no longer used.");
193 println!(" Your settings have been reset to defaults.");
194 println!(" Please manually port your configuration to the new Lua format.");
195 println!(" See the new config.lua template for examples.\n");
196 }
197 }
198 Ok(())
199 }