Skip to content

Commit 4fb6b97

Browse files
refactor(disable): reduce complexity of run() in disable.rs (#1489)
Extract ApplyOutcome enum and apply_action() helper so the per-definition loop body is a flat three-arm match instead of nested match-if-match. - New: ApplyOutcome { Skipped, Patched, Failed } enum - New: apply_action() — handles one Action, prints status, returns outcome - run() loop reduced from ~25 lines of nested control flow to 6 lines Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 058b69a commit 4fb6b97

1 file changed

Lines changed: 50 additions & 27 deletions

File tree

src/disable.rs

Lines changed: 50 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ use log::debug;
1818
use std::path::{Path, PathBuf};
1919

2020
use crate::ado::{
21-
MatchedDefinition, match_definitions, patch_queue_status, resolve_ado_context, resolve_auth,
21+
AdoAuth, AdoContext, MatchedDefinition, match_definitions, patch_queue_status,
22+
resolve_ado_context, resolve_auth,
2223
};
2324
use crate::detect;
2425

@@ -100,6 +101,50 @@ pub struct DisableOptions<'a> {
100101
pub dry_run: bool,
101102
}
102103

104+
/// Outcome of applying one [`Action`] (used to tally the final summary).
105+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
106+
enum ApplyOutcome {
107+
Skipped,
108+
Patched,
109+
Failed,
110+
}
111+
112+
/// Apply a single action against ADO, print a one-line status, and return the
113+
/// outcome so the caller can tally results without nesting.
114+
async fn apply_action(
115+
action: Action,
116+
client: &reqwest::Client,
117+
ado_ctx: &AdoContext,
118+
auth: &AdoAuth,
119+
dry_run: bool,
120+
) -> ApplyOutcome {
121+
match action {
122+
Action::Skip { id, name, reason } => {
123+
println!("↻ skip: {} (id={}, {})", name, id, reason);
124+
ApplyOutcome::Skipped
125+
}
126+
Action::Patch { id, name, from, to } => {
127+
if dry_run {
128+
println!(
129+
"[dry-run] ▶ would patch: {} (id={}, {} → {})",
130+
name, id, from, to
131+
);
132+
return ApplyOutcome::Patched;
133+
}
134+
match patch_queue_status(client, ado_ctx, auth, id, to).await {
135+
Ok(()) => {
136+
println!("▶ patched: {} (id={}, {} → {})", name, id, from, to);
137+
ApplyOutcome::Patched
138+
}
139+
Err(e) => {
140+
eprintln!("✗ failed: {} (id={}): {:#}", name, id, e);
141+
ApplyOutcome::Failed
142+
}
143+
}
144+
}
145+
}
146+
}
147+
103148
/// Run the `disable` command.
104149
pub async fn run(opts: DisableOptions<'_>) -> Result<()> {
105150
let repo_path: PathBuf = match opts.path {
@@ -165,32 +210,10 @@ pub async fn run(opts: DisableOptions<'_>) -> Result<()> {
165210
for m in &matched {
166211
let action = decide_action(m, target);
167212
debug!("definition {}: action={:?}", m.id, action);
168-
169-
match action {
170-
Action::Skip { id, name, reason } => {
171-
println!("↻ skip: {} (id={}, {})", name, id, reason);
172-
skipped += 1;
173-
}
174-
Action::Patch { id, name, from, to } => {
175-
if opts.dry_run {
176-
println!(
177-
"[dry-run] ▶ would patch: {} (id={}, {} → {})",
178-
name, id, from, to
179-
);
180-
patched += 1;
181-
continue;
182-
}
183-
match patch_queue_status(&client, &ado_ctx, &auth, id, to).await {
184-
Ok(()) => {
185-
println!("▶ patched: {} (id={}, {} → {})", name, id, from, to);
186-
patched += 1;
187-
}
188-
Err(e) => {
189-
eprintln!("✗ failed: {} (id={}): {:#}", name, id, e);
190-
failure += 1;
191-
}
192-
}
193-
}
213+
match apply_action(action, &client, &ado_ctx, &auth, opts.dry_run).await {
214+
ApplyOutcome::Skipped => skipped += 1,
215+
ApplyOutcome::Patched => patched += 1,
216+
ApplyOutcome::Failed => failure += 1,
194217
}
195218
}
196219

0 commit comments

Comments
 (0)