From 3dc71b2f5160ef9bd46143eed0edb5d2557b58e6 Mon Sep 17 00:00:00 2001 From: aur-build Date: Sun, 22 Nov 2026 10:36:24 +0000 Subject: rustfmt,lints: pin the formatting, adopt the idiom lints, pay them off rustfmt.toml fixes the shape the tree is written in (120 columns, small items kept on one line) and one canonical pass makes it the truth. [lints.clippy] names the idiom lints worth enforcing and leaves out the two that would add doc-comment boilerplate rather than remove it. The tree pays them off here: write!/writeln! instead of push_str(&format!()), let-else instead of match-on-Option, borrows where a thread entry point only reads what it owns, no `&self` on a Copy state and no `self` nobody reads. ops::to_prometheus gains one gauge helper: a metric's HELP, TYPE and value are now emitted by the same call instead of three. --- src/sandbox/agent.rs | 52 ++++++++++++++++------------------------------------ 1 file changed, 16 insertions(+), 36 deletions(-) (limited to 'src/sandbox/agent.rs') diff --git a/src/sandbox/agent.rs b/src/sandbox/agent.rs index 3eaa799..fa813cf 100644 --- a/src/sandbox/agent.rs +++ b/src/sandbox/agent.rs @@ -22,17 +22,10 @@ pub struct AgentOptions { pub fn serve(opts: &AgentOptions) -> Result<()> { let stream = connect_with_retry(&opts.hub, Duration::from_secs(15))?; - let agent_id = opts - .agent_id - .clone() - .unwrap_or_else(|| format!("sandbox-{}", std::process::id())); + let agent_id = opts.agent_id.clone().unwrap_or_else(|| format!("sandbox-{}", std::process::id())); wire::write_frame( &mut stream.try_clone()?, - &Frame::Hello { - token: opts.token.clone(), - agent_id: agent_id.clone(), - profiles: vec![opts.profile.clone()], - }, + &Frame::Hello { token: opts.token.clone(), agent_id: agent_id.clone(), profiles: vec![opts.profile.clone()] }, )?; let mut reader = BufReader::new(stream.try_clone()?); @@ -50,8 +43,7 @@ pub fn serve(opts: &AgentOptions) -> Result<()> { let child = Arc::new(Mutex::new(None::)); let alive = Arc::new(AtomicBool::new(true)); { - let (last, child, alive, ttl) = - (last_inbound.clone(), child.clone(), alive.clone(), ttl); + let (last, child, alive, ttl) = (last_inbound.clone(), child.clone(), alive.clone(), ttl); std::thread::spawn(crate::time::inherit(move || loop { crate::time::sleep(Duration::from_secs(1)); let quiet = last.lock().unwrap().elapsed(); @@ -102,17 +94,12 @@ pub fn serve(opts: &AgentOptions) -> Result<()> { outcome } -fn handle( - frame: Frame, - writer: &Arc>, - blobs: &Path, - child: &Arc>>, -) -> Result { +fn handle(frame: Frame, writer: &Arc>, blobs: &Path, child: &Arc>>) -> Result { match frame { Frame::Destroy {} => Ok(true), Frame::Ping {} => Ok(false), Frame::Put { req, digest, data } => { - let bytes = wire::unhex(&data).map_err(io_err)?; + let bytes = wire::unhex(&data).map_err(|e| Error::other(e.to_string()))?; if crate::hash::hex(&bytes) != digest { reply(writer, req, Err("put: data does not hash to the claimed digest"))?; return Ok(false); @@ -121,16 +108,18 @@ fn handle( reply(writer, req, Ok(()))?; Ok(false) } - Frame::Get { req, digest } => match std::fs::read(safe_blob_path(blobs, &digest)?) { - Ok(bytes) => { - wire::write_frame(writer.lock().unwrap().deref_mut(), &Frame::Blob { req, data: wire::hex_bytes(&bytes) })?; + Frame::Get { req, digest } => { + if let Ok(bytes) = std::fs::read(safe_blob_path(blobs, &digest)?) { + wire::write_frame( + writer.lock().unwrap().deref_mut(), + &Frame::Blob { req, data: wire::hex_bytes(&bytes) }, + )?; Ok(false) - } - Err(_) => { + } else { reply(writer, req, Err("get: no such blob"))?; Ok(false) } - }, + } Frame::Run { req, argv, cwd, env } => { run_child(req, argv, cwd, env, writer, blobs, child)?; Ok(false) @@ -149,16 +138,14 @@ fn run_child( child: &Arc>>, ) -> Result<()> { let mut argv = argv; - for a in argv.iter_mut() { + for a in &mut argv { if let Some(digest) = a.strip_prefix("blob:") { *a = safe_blob_path(blobs, digest)?.display().to_string(); } } let program = argv.first().ok_or_else(|| Error::invalid("run: empty argv"))?; let mut c = std::process::Command::new(program); - c.args(&argv[1..]) - .stdout(Stdio::piped()) - .stderr(Stdio::piped()); + c.args(&argv[1..]).stdout(Stdio::piped()).stderr(Stdio::piped()); if let Some(dir) = cwd { c.current_dir(dir); } @@ -205,10 +192,7 @@ fn run_child( } let status = proc.wait()?; *child.lock().unwrap() = None; - wire::write_frame( - writer.lock().unwrap().deref_mut(), - &Frame::Exit { req, code: status.code() }, - )?; + wire::write_frame(writer.lock().unwrap().deref_mut(), &Frame::Exit { req, code: status.code() })?; Ok(()) } @@ -246,8 +230,4 @@ fn safe_blob_path(blobs: &Path, digest: &str) -> Result { Ok(blobs.join(digest)) } -fn io_err(e: std::io::Error) -> Error { - Error::other(e.to_string()) -} - use std::ops::DerefMut; -- cgit v1.2.3