Skip to content

Commit 683724e

Browse files
committed
refactor(rust): reduce workspace clippy warning noise
1 parent 9848d04 commit 683724e

File tree

20 files changed

+366
-383
lines changed

20 files changed

+366
-383
lines changed

Cargo.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/openshell-bootstrap/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ tracing = { workspace = true }
2525

2626
[dev-dependencies]
2727
tempfile = "3"
28+
temp-env = "0.3"
2829

2930
[lints]
3031
workspace = true

crates/openshell-bootstrap/src/docker.rs

Lines changed: 45 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -304,22 +304,25 @@ pub async fn find_gateway_container(docker: &Docker, port: Option<u16>) -> Resul
304304

305305
let matches: Vec<String> = containers
306306
.iter()
307-
.filter(|c| is_gateway_image(c) && port.map_or(true, |p| has_port(c, p)))
307+
.filter(|c| is_gateway_image(c) && port.is_none_or(|p| has_port(c, p)))
308308
.filter_map(container_name)
309309
.collect();
310310

311311
match matches.len() {
312312
0 => {
313-
let hint = if let Some(p) = port {
314-
format!(
315-
"No openshell gateway container found listening on port {p}.\n\
313+
let hint = port.map_or_else(
314+
|| {
315+
"No openshell gateway container found.\n\
316316
Is the gateway running? Check with: docker ps"
317-
)
318-
} else {
319-
"No openshell gateway container found.\n\
320-
Is the gateway running? Check with: docker ps"
321-
.to_string()
322-
};
317+
.to_string()
318+
},
319+
|p| {
320+
format!(
321+
"No openshell gateway container found listening on port {p}.\n\
322+
Is the gateway running? Check with: docker ps"
323+
)
324+
},
325+
);
323326
Err(miette::miette!("{hint}"))
324327
}
325328
1 => Ok(matches.into_iter().next().unwrap()),
@@ -748,22 +751,22 @@ pub async fn check_port_conflicts(
748751

749752
let ports = container.ports.as_deref().unwrap_or_default();
750753
for port in ports {
751-
if let Some(public) = port.public_port {
752-
if needed_ports.contains(&public) {
753-
let cname = names
754-
.first()
755-
.map(|n| n.trim_start_matches('/').to_string())
756-
.unwrap_or_else(|| {
757-
container
758-
.id
759-
.clone()
760-
.unwrap_or_else(|| "<unknown>".to_string())
761-
});
762-
conflicts.push(PortConflict {
763-
container_name: cname,
764-
host_port: public,
765-
});
766-
}
754+
if let Some(public) = port.public_port
755+
&& needed_ports.contains(&public)
756+
{
757+
let cname = names.first().map_or_else(
758+
|| {
759+
container
760+
.id
761+
.clone()
762+
.unwrap_or_else(|| "<unknown>".to_string())
763+
},
764+
|n| n.trim_start_matches('/').to_string(),
765+
);
766+
conflicts.push(PortConflict {
767+
container_name: cname,
768+
host_port: public,
769+
});
767770
}
768771
}
769772
}
@@ -1091,6 +1094,7 @@ fn is_port_conflict(err: &BollardError) -> bool {
10911094
#[cfg(test)]
10921095
mod tests {
10931096
use super::*;
1097+
use temp_env::with_var;
10941098

10951099
#[test]
10961100
fn normalize_arch_x86_64() {
@@ -1152,36 +1156,22 @@ mod tests {
11521156
#[test]
11531157
fn docker_not_reachable_error_with_docker_host() {
11541158
// Simulate: DOCKER_HOST is set but daemon unresponsive.
1155-
// We set the env var temporarily (this is test-only).
1156-
let prev_docker_host = std::env::var("DOCKER_HOST").ok();
1157-
// SAFETY: test-only, single-threaded test runner for this test
1158-
unsafe {
1159-
std::env::set_var("DOCKER_HOST", "unix:///tmp/fake-docker.sock");
1160-
}
1161-
1162-
let err = docker_not_reachable_error(
1163-
"daemon not responding",
1164-
"Docker socket exists but the daemon is not responding",
1165-
);
1166-
let msg = format!("{err:?}");
1167-
1168-
// Restore env
1169-
// SAFETY: test-only, restoring previous state
1170-
unsafe {
1171-
match prev_docker_host {
1172-
Some(val) => std::env::set_var("DOCKER_HOST", val),
1173-
None => std::env::remove_var("DOCKER_HOST"),
1174-
}
1175-
}
1159+
with_var("DOCKER_HOST", Some("unix:///tmp/fake-docker.sock"), || {
1160+
let err = docker_not_reachable_error(
1161+
"daemon not responding",
1162+
"Docker socket exists but the daemon is not responding",
1163+
);
1164+
let msg = format!("{err:?}");
11761165

1177-
assert!(
1178-
msg.contains("DOCKER_HOST"),
1179-
"should mention DOCKER_HOST when it is set"
1180-
);
1181-
assert!(
1182-
msg.contains("unix:///tmp/fake-docker.sock"),
1183-
"should show the current DOCKER_HOST value"
1184-
);
1166+
assert!(
1167+
msg.contains("DOCKER_HOST"),
1168+
"should mention DOCKER_HOST when it is set"
1169+
);
1170+
assert!(
1171+
msg.contains("unix:///tmp/fake-docker.sock"),
1172+
"should show the current DOCKER_HOST value"
1173+
);
1174+
});
11851175
}
11861176

11871177
#[test]

crates/openshell-bootstrap/src/lib.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -305,8 +305,8 @@ where
305305
}
306306

307307
// Ensure the image is available on the target Docker daemon
308+
log("[status] Downloading gateway".to_string());
308309
if remote_opts.is_some() {
309-
log("[status] Downloading gateway".to_string());
310310
let on_log_clone = Arc::clone(&on_log);
311311
let progress_cb = move |msg: String| {
312312
if let Ok(mut f) = on_log_clone.lock() {
@@ -323,7 +323,6 @@ where
323323
.await?;
324324
} else {
325325
// Local deployment: ensure image exists (pull if needed)
326-
log("[status] Downloading gateway".to_string());
327326
ensure_image(
328327
&target_docker,
329328
&image_ref,
@@ -662,16 +661,16 @@ pub async fn gateway_container_logs<W: std::io::Write>(
662661
Ok(())
663662
}
664663

665-
/// Fetch the last `n` lines of container logs for a local gateway as a
666-
/// `String`. This is a convenience wrapper for diagnostic call sites (e.g.
667-
/// failure diagnosis in the CLI) that do not hold a Docker client handle.
664+
/// Fetch the last `n` lines of container logs for a local gateway as a `String`.
665+
///
666+
/// This is a convenience wrapper for diagnostic call sites (e.g. failure
667+
/// diagnosis in the CLI) that do not hold a Docker client handle.
668668
///
669669
/// Returns an empty string on any Docker/connection error so callers don't
670670
/// need to worry about error handling.
671671
pub async fn fetch_gateway_logs(name: &str, n: usize) -> String {
672-
let docker = match Docker::connect_with_local_defaults() {
673-
Ok(d) => d,
674-
Err(_) => return String::new(),
672+
let Ok(docker) = Docker::connect_with_local_defaults() else {
673+
return String::new();
675674
};
676675
let container = container_name(name);
677676
fetch_recent_logs(&docker, &container, n).await

crates/openshell-core/src/config.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
22
// SPDX-License-Identifier: Apache-2.0
33

4-
//! Configuration management for OpenShell components.
4+
//! Configuration management for `OpenShell` components.
55
66
use serde::{Deserialize, Serialize};
77
use std::net::SocketAddr;
@@ -39,7 +39,7 @@ pub struct Config {
3939
#[serde(default)]
4040
pub sandbox_image_pull_policy: String,
4141

42-
/// gRPC endpoint for sandboxes to connect back to OpenShell.
42+
/// gRPC endpoint for sandboxes to connect back to `OpenShell`.
4343
/// Used by sandbox pods to fetch their policy at startup.
4444
#[serde(default)]
4545
pub grpc_endpoint: String,

crates/openshell-core/src/error.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
// SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
22
// SPDX-License-Identifier: Apache-2.0
33

4-
//! Common error types for OpenShell.
4+
//! Common error types for `OpenShell`.
55
66
use miette::Diagnostic;
77
use thiserror::Error;
88

9-
/// Result type alias using OpenShell's error type.
9+
/// Result type alias using `OpenShell`'s error type.
1010
pub type Result<T> = std::result::Result<T, Error>;
1111

12-
/// OpenShell error type.
12+
/// `OpenShell` error type.
1313
#[derive(Debug, Error, Diagnostic)]
1414
pub enum Error {
1515
/// Configuration error.

crates/openshell-core/src/forward.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -135,18 +135,17 @@ pub fn pid_matches_forward(pid: u32, port: u16, sandbox_id: Option<&str>) -> boo
135135
/// match is expected.
136136
pub fn find_forward_by_port(port: u16) -> Result<Option<String>> {
137137
let dir = forward_pid_dir()?;
138-
let entries = match std::fs::read_dir(&dir) {
139-
Ok(e) => e,
140-
Err(_) => return Ok(None),
138+
let Ok(entries) = std::fs::read_dir(&dir) else {
139+
return Ok(None);
141140
};
142141
let suffix = format!("-{port}.pid");
143142
for entry in entries.flatten() {
144143
let file_name = entry.file_name();
145144
let file_name = file_name.to_string_lossy();
146-
if let Some(name) = file_name.strip_suffix(&suffix) {
147-
if !name.is_empty() {
148-
return Ok(Some(name.to_string()));
149-
}
145+
if let Some(name) = file_name.strip_suffix(&suffix)
146+
&& !name.is_empty()
147+
{
148+
return Ok(Some(name.to_string()));
150149
}
151150
}
152151
Ok(None)
@@ -671,9 +670,8 @@ mod tests {
671670
// `python3 -m http.server` which listens on [::] by default. The
672671
// IPv4-only TcpListener::bind("127.0.0.1", port) might succeed, but
673672
// lsof should detect the listener and the check should still fail.
674-
let listener = match TcpListener::bind("[::]:0") {
675-
Ok(l) => l,
676-
Err(_) => return, // IPv6 not available, skip
673+
let Ok(listener) = TcpListener::bind("[::]:0") else {
674+
return; // IPv6 not available, skip
677675
};
678676
let port = listener.local_addr().unwrap().port();
679677

crates/openshell-core/src/inference.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -105,17 +105,17 @@ pub fn profile_for(provider_type: &str) -> Option<&'static InferenceProviderProf
105105
/// need the auth/header information (e.g. the sandbox bundle-to-route
106106
/// conversion).
107107
pub fn auth_for_provider_type(provider_type: &str) -> (AuthHeader, Vec<(String, String)>) {
108-
match profile_for(provider_type) {
109-
Some(profile) => {
108+
profile_for(provider_type).map_or_else(
109+
|| (AuthHeader::Bearer, Vec::new()),
110+
|profile| {
110111
let headers = profile
111112
.default_headers
112113
.iter()
113114
.map(|(k, v)| ((*k).to_string(), (*v).to_string()))
114115
.collect();
115116
(profile.auth.clone(), headers)
116-
}
117-
None => (AuthHeader::Bearer, Vec::new()),
118-
}
117+
},
118+
)
119119
}
120120

121121
// ---------------------------------------------------------------------------

crates/openshell-core/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
22
// SPDX-License-Identifier: Apache-2.0
33

4-
//! OpenShell Core - shared library for OpenShell components.
4+
//! `OpenShell` Core - shared library for `OpenShell` components.
55
//!
66
//! This crate provides:
77
//! - Protocol buffer definitions and generated code

0 commit comments

Comments
 (0)