From db0ce5b966cfec2977b23cb906b41df16ed4765c Mon Sep 17 00:00:00 2001 From: Ousama Ben Younes Date: Sat, 6 Jun 2026 23:25:18 +0000 Subject: [PATCH] refactor(tui): parse provider keys once in picker row build MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Collapse the duplicated ApiProvider::parse call in picker_model_rows_for_app: the filter previously parsed each key to exclude the active provider, then the loop parsed it again via a let-else to skip unknown keys. Use a single filter_map that parses once, drops unknown keys and the active provider in one pass, and carries the parsed provider through to the loop. Pure readability change — unknown keys are still dropped and the active provider is still excluded, so picker rows are identical. Sort still keys on the provider string for deterministic ordering. Co-Authored-By: Claude Opus 4.8 (1M context) --- crates/tui/src/tui/model_picker.rs | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/crates/tui/src/tui/model_picker.rs b/crates/tui/src/tui/model_picker.rs index a55777d1..5cfa748d 100644 --- a/crates/tui/src/tui/model_picker.rs +++ b/crates/tui/src/tui/model_picker.rs @@ -391,18 +391,19 @@ fn picker_model_rows_for_app(app: &App) -> Vec { // the current provider is still reachable. Selecting one switches provider // on apply via `resolved_provider` / `build_event`. Rows are sorted by // provider key so ordering stays deterministic regardless of map iteration. - let mut other_provider_models: Vec<(&String, &String)> = app + // Parse each provider key once: drop unknown keys (cannot be applied) and + // the active provider (already listed above) in a single pass. `key` is + // kept only to keep ordering deterministic via the sort below. + let mut other_provider_models: Vec<(&String, ApiProvider, &String)> = app .provider_models .iter() - .filter(|(key, _)| ApiProvider::parse(key) != Some(app.api_provider)) + .filter_map(|(key, model)| { + let provider = ApiProvider::parse(key)?; + (provider != app.api_provider).then_some((key, provider, model)) + }) .collect(); - other_provider_models.sort_by(|(a, _), (b, _)| a.cmp(b)); - for (key, model) in other_provider_models { - let Some(provider) = ApiProvider::parse(key) else { - // Unknown provider key — we cannot switch to it, so skip rather - // than offer a row that would fail to apply. - continue; - }; + other_provider_models.sort_by(|(a, ..), (b, ..)| a.cmp(b)); + for (_key, provider, model) in other_provider_models { let model = model.trim(); if model.is_empty() { continue;