Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix lint issues reported by clippy #50

Merged
merged 2 commits into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/bh_agent_server/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{any, net::IpAddr};
use std::net::IpAddr;

use clap::Parser;
use futures::{future, prelude::*};
Expand Down
60 changes: 28 additions & 32 deletions crates/bh_agent_server/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,19 +70,18 @@ impl BhAgentState {
Ok(modes.contains(
self.file_modes
.read()?
.get(&fd)
.get(fd)
.ok_or(InvalidFileDescriptor)?,
))
}

pub fn file_type(&self, fd: &FileId) -> Result<FileOpenType, AgentError> {
trace!("Getting file type for {}", fd);
Ok(self
.file_types
self.file_types
.read()?
.get(&fd)
.get(fd)
.ok_or(InvalidFileDescriptor)
.and_then(|t| Ok(t.clone()))?)
.map(|t| *t)
}

pub fn open_path(
Expand Down Expand Up @@ -148,7 +147,7 @@ impl BhAgentState {
config
.argv
.iter()
.map(|s| OsStr::new(s))
.map(OsStr::new)
.collect::<Vec<_>>()
.as_slice(),
popenconfig,
Expand Down Expand Up @@ -213,38 +212,34 @@ impl BhAgentState {
ProcessChannel::Stderr => &self.proc_stderr_ids,
};

channel_ids
.read()?
.get_by_left(&proc_id)
.map(|i| i.clone())
.ok_or((|| {
debug!("Failed to get process channel");
debug!("Process ID: {}", proc_id);
debug!("Channel: {:?}", channel);
let proc_is_valid = self.processes.read().unwrap().contains_key(&proc_id);
debug!("Process is valid: {}", proc_is_valid);
debug!(
"Process with valid channels: {:?}",
channel_ids.read().unwrap().left_values()
);
channel_ids.read()?.get_by_left(proc_id).copied().ok_or({
debug!("Failed to get process channel");
debug!("Process ID: {}", proc_id);
debug!("Channel: {:?}", channel);
let proc_is_valid = self.processes.read().unwrap().contains_key(proc_id);
debug!("Process is valid: {}", proc_is_valid);
debug!(
"Process with valid channels: {:?}",
channel_ids.read().unwrap().left_values()
);

InvalidProcessId
})())
InvalidProcessId
})
}

pub fn process_poll(&self, proc_id: &ProcessId) -> Result<Option<u32>, AgentError> {
trace!("Polling process {}", proc_id);
let proc = self
.processes
.read()?
.get(&proc_id)
.get(proc_id)
.ok_or(InvalidProcessId)?
.clone();
let exit_status = proc.write()?.poll();
match exit_status {
None => Ok(None),
Some(status) => match status {
subprocess::ExitStatus::Exited(code) => Ok(Some(code as u32)),
subprocess::ExitStatus::Exited(code) => Ok(Some(code)),
subprocess::ExitStatus::Signaled(code) => Ok(Some(code as u32)),
subprocess::ExitStatus::Other(code) => Ok(Some(code as u32)),
subprocess::ExitStatus::Undetermined => Err(Unknown),
Expand Down Expand Up @@ -280,7 +275,7 @@ impl BhAgentState {
let proc = self
.processes
.read()?
.get(&proc_id)
.get(proc_id)
.ok_or(InvalidProcessId)?
.clone();
let exit_status = proc.read()?.exit_status();
Expand All @@ -297,16 +292,17 @@ impl BhAgentState {

pub fn close_file(&self, fd: &FileId) -> Result<(), AgentError> {
trace!("Closing file {}", fd);
Ok(drop(
drop(
self.files
.write()?
.remove(&fd)
.remove(fd)
.ok_or(InvalidFileDescriptor)?,
))
);
Ok(())
}

pub fn is_file_closed(&self, fd: &FileId) -> Result<bool, AgentError> {
Ok(self.files.read()?.contains_key(&fd))
Ok(self.files.read()?.contains_key(fd))
}

pub fn do_mut_operation<R: Sized>(
Expand All @@ -324,23 +320,23 @@ impl BhAgentState {
}

// If these unwraps fail, the state is bad
if let Some(pid) = self.proc_stdin_ids.read()?.get_by_right(&fd) {
if let Some(pid) = self.proc_stdin_ids.read()?.get_by_right(fd) {
let procs_binding = self.processes.read()?;
let mut proc_binding = procs_binding.get(pid).unwrap().write()?;
let file = proc_binding.stdin.as_mut().unwrap();
return Ok(op(file));
} else {
trace!("Process stdin id map: {:?}", self.proc_stdin_ids.read()?);
}
if let Some(pid) = self.proc_stdout_ids.read()?.get_by_right(&fd) {
if let Some(pid) = self.proc_stdout_ids.read()?.get_by_right(fd) {
let procs_binding = self.processes.read()?;
let mut proc_binding = procs_binding.get(pid).unwrap().write()?;
let file = proc_binding.stdout.as_mut().unwrap();
return Ok(op(file));
} else {
trace!("Process stdout id map: {:?}", self.proc_stdout_ids.read()?);
}
if let Some(pid) = self.proc_stderr_ids.read()?.get_by_right(&fd) {
if let Some(pid) = self.proc_stderr_ids.read()?.get_by_right(fd) {
let procs_binding = self.processes.read()?;
let mut proc_binding = procs_binding.get(pid).unwrap().write()?;
let file = proc_binding.stderr.as_mut().unwrap();
Expand Down