Skip to content

Commit e928c94

Browse files
committed
Add CliAction::config_dir accessor
Collapses the verbose nine-variant match in main.rs into a single call. Putting the projection on the type keeps the variant list in one place, so adding a future variant only updates args.rs.
1 parent d4298fd commit e928c94

2 files changed

Lines changed: 20 additions & 15 deletions

File tree

src/args.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,24 @@ pub enum CliAction {
129129
},
130130
}
131131

132+
impl CliAction {
133+
/// Returns the custom config directory carried by this action, if any.
134+
pub fn config_dir(&self) -> Option<&str> {
135+
match self {
136+
Self::Run { config_dir, .. }
137+
| Self::TestCommand { config_dir, .. }
138+
| Self::GeoCommand { config_dir, .. }
139+
| Self::PresetCommand { config_dir, .. }
140+
| Self::SetCommand { config_dir, .. }
141+
| Self::GetCommand { config_dir, .. }
142+
| Self::StopCommand { config_dir, .. }
143+
| Self::RestartCommand { config_dir, .. }
144+
| Self::Simulate { config_dir, .. } => config_dir.as_deref(),
145+
_ => None,
146+
}
147+
}
148+
}
149+
132150
/// Result of parsing command-line arguments.
133151
pub struct ParsedArgs {
134152
pub action: CliAction,

src/main.rs

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,8 @@ use sunsetr::{
1717
fn main() -> Result<()> {
1818
let parsed_args = ParsedArgs::from_env();
1919

20-
let config_dir = match &parsed_args.action {
21-
CliAction::Run { config_dir, .. }
22-
| CliAction::TestCommand { config_dir, .. }
23-
| CliAction::GeoCommand { config_dir, .. }
24-
| CliAction::PresetCommand { config_dir, .. }
25-
| CliAction::SetCommand { config_dir, .. }
26-
| CliAction::GetCommand { config_dir, .. }
27-
| CliAction::StopCommand { config_dir, .. }
28-
| CliAction::RestartCommand { config_dir, .. }
29-
| CliAction::Simulate { config_dir, .. } => config_dir.clone(),
30-
_ => None,
31-
};
32-
33-
if let Some(dir) = config_dir
34-
&& let Err(e) = config::set_config_dir(Some(dir))
20+
if let Some(dir) = parsed_args.action.config_dir()
21+
&& let Err(e) = config::set_config_dir(Some(dir.to_string()))
3522
{
3623
log_error_exit!("{}", e);
3724
}

0 commit comments

Comments
 (0)