Skip to content

Commit c0b4900

Browse files
authored
Merge pull request gi-dellav#183 from xavierforge/fix/clippy-collapsible-if
fix(clippy): collapse nested if statements into let-chains
2 parents ad09e30 + ccbf9ec commit c0b4900

5 files changed

Lines changed: 39 additions & 49 deletions

File tree

src/logging.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ pub fn install_panic_hook() {
3535

3636
fn write_crash_report(info: &std::panic::PanicHookInfo) -> Option<PathBuf> {
3737
let path = resolve_crash_log_path();
38-
if let Some(parent) = path.parent() {
39-
if fs::create_dir_all(parent).is_err() {
40-
return None;
41-
}
38+
if let Some(parent) = path.parent()
39+
&& fs::create_dir_all(parent).is_err()
40+
{
41+
return None;
4242
}
4343

4444
let mut content = String::from("zerostack crash report\n");

src/main.rs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -391,23 +391,21 @@ async fn main() -> anyhow::Result<()> {
391391
let need_pricing = session.input_token_cost == 0.0 && session.output_token_cost == 0.0;
392392
let need_ctx = cfg.context_window.is_none()
393393
&& config::Config::catalog_context_window("openrouter", model.as_str()).is_none();
394-
if need_pricing || need_ctx {
395-
if let Ok(infos) = provider::fetch_openrouter_pricing(
394+
if (need_pricing || need_ctx)
395+
&& let Ok(infos) = provider::fetch_openrouter_pricing(
396396
cli.api_key.as_deref(),
397397
&cfg.custom_providers_map(),
398398
cfg.api_keys.as_ref(),
399399
)
400400
.await
401-
{
402-
if let Some(info) = infos.get(model.as_str()) {
403-
if need_pricing {
404-
session.input_token_cost = info.input_cost;
405-
session.output_token_cost = info.output_cost;
406-
}
407-
if need_ctx && let Some(cw) = info.context_length {
408-
session.update_context_window(cw);
409-
}
410-
}
401+
&& let Some(info) = infos.get(model.as_str())
402+
{
403+
if need_pricing {
404+
session.input_token_cost = info.input_cost;
405+
session.output_token_cost = info.output_cost;
406+
}
407+
if need_ctx && let Some(cw) = info.context_length {
408+
session.update_context_window(cw);
411409
}
412410
}
413411
}

src/provider.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -873,7 +873,7 @@ pub(crate) fn build_http_client(
873873
}
874874

875875
fn is_localhost(url: Option<&str>) -> bool {
876-
url.map_or(false, |u| {
876+
url.is_some_and(|u| {
877877
u.starts_with("http://localhost")
878878
|| u.starts_with("http://127.")
879879
|| u.starts_with("http://[::1]")

src/session/storage.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -130,10 +130,9 @@ pub fn find_sessions_by_prefix(prefix: &str) -> anyhow::Result<Vec<Session>> {
130130
&& let Some(stem) = path.file_stem().and_then(|s| s.to_str())
131131
&& let Ok(json) = std::fs::read_to_string(&path)
132132
&& let Ok(session) = serde_json::from_str::<Session>(&json)
133+
&& (stem.starts_with(prefix) || session.name.to_lowercase().contains(&lower))
133134
{
134-
if stem.starts_with(prefix) || session.name.to_lowercase().contains(&lower) {
135-
sessions.push(session);
136-
}
135+
sessions.push(session);
137136
}
138137
}
139138
sessions.sort_by(|a, b| b.updated_at.cmp(&a.updated_at));
@@ -158,10 +157,9 @@ pub fn find_session_by_name(name: &str) -> anyhow::Result<Option<Session>> {
158157
if path.extension().is_some_and(|e| e == "json")
159158
&& let Ok(json) = std::fs::read_to_string(&path)
160159
&& let Ok(session) = serde_json::from_str::<Session>(&json)
160+
&& session.name.to_lowercase() == lower
161161
{
162-
if session.name.to_lowercase() == lower {
163-
return Ok(Some(session));
164-
}
162+
return Ok(Some(session));
165163
}
166164
}
167165
Ok(None)

src/ui/slash/providers.rs

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -178,25 +178,22 @@ async fn apply_model(ctx: &mut SlashCtx<'_>, model_id: &str) {
178178
if let Some((input, output)) = lookup_pricing_from_cache(&ctx.session.provider, model_id) {
179179
ctx.session.input_token_cost = input;
180180
ctx.session.output_token_cost = output;
181-
} else if ctx.session.provider == "openrouter" {
182-
if let Ok(prices) = crate::provider::fetch_openrouter_pricing(
181+
} else if ctx.session.provider == "openrouter"
182+
&& let Ok(prices) = crate::provider::fetch_openrouter_pricing(
183183
ctx.cli.api_key.as_deref(),
184184
&ctx.cfg.custom_providers_map(),
185185
ctx.cfg.api_keys.as_ref(),
186186
)
187187
.await
188+
&& let Some(info) = prices.get(model_id)
189+
{
190+
ctx.session.input_token_cost = info.input_cost;
191+
ctx.session.output_token_cost = info.output_cost;
192+
if ctx.cfg.context_window.is_none()
193+
&& crate::config::Config::catalog_context_window("openrouter", model_id).is_none()
194+
&& let Some(cw) = info.context_length
188195
{
189-
if let Some(info) = prices.get(model_id) {
190-
ctx.session.input_token_cost = info.input_cost;
191-
ctx.session.output_token_cost = info.output_cost;
192-
if ctx.cfg.context_window.is_none()
193-
&& crate::config::Config::catalog_context_window("openrouter", model_id)
194-
.is_none()
195-
&& let Some(cw) = info.context_length
196-
{
197-
ctx.session.update_context_window(cw);
198-
}
199-
}
196+
ctx.session.update_context_window(cw);
200197
}
201198
}
202199
write_ok(ctx.renderer, format!("switched to model: {}", new_model));
@@ -290,25 +287,22 @@ async fn handle_model(parts: &[&str], ctx: &mut SlashCtx<'_>) -> anyhow::Result<
290287
if let Some((input, output)) = lookup_pricing_from_cache(&ctx.session.provider, &new_model) {
291288
ctx.session.input_token_cost = input;
292289
ctx.session.output_token_cost = output;
293-
} else if ctx.session.provider == "openrouter" {
294-
if let Ok(prices) = crate::provider::fetch_openrouter_pricing(
290+
} else if ctx.session.provider == "openrouter"
291+
&& let Ok(prices) = crate::provider::fetch_openrouter_pricing(
295292
ctx.cli.api_key.as_deref(),
296293
&ctx.cfg.custom_providers_map(),
297294
ctx.cfg.api_keys.as_ref(),
298295
)
299296
.await
297+
&& let Some(info) = prices.get(&*new_model)
298+
{
299+
ctx.session.input_token_cost = info.input_cost;
300+
ctx.session.output_token_cost = info.output_cost;
301+
if ctx.cfg.context_window.is_none()
302+
&& crate::config::Config::catalog_context_window("openrouter", &new_model).is_none()
303+
&& let Some(cw) = info.context_length
300304
{
301-
if let Some(info) = prices.get(&*new_model) {
302-
ctx.session.input_token_cost = info.input_cost;
303-
ctx.session.output_token_cost = info.output_cost;
304-
if ctx.cfg.context_window.is_none()
305-
&& crate::config::Config::catalog_context_window("openrouter", &new_model)
306-
.is_none()
307-
&& let Some(cw) = info.context_length
308-
{
309-
ctx.session.update_context_window(cw);
310-
}
311-
}
305+
ctx.session.update_context_window(cw);
312306
}
313307
}
314308
write_ok(ctx.renderer, format!("switched to model: {}", new_model));

0 commit comments

Comments
 (0)