Skip to content

Commit

Permalink
Error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
nnmm committed Jul 21, 2024
1 parent 28d4eeb commit b46e585
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 26 deletions.
5 changes: 4 additions & 1 deletion gomori-py/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ use pyo3::prelude::*;
/// A Python module implemented in Rust.
#[pymodule]
fn gomori(py: Python, m: &PyModule) -> PyResult<()> {
m.add("IllegalCardPlayed", py.get_type::<::gomori::IllegalCardPlayedException>())?;
m.add(
"IllegalCardPlayed",
py.get_type::<::gomori::IllegalCardPlayedException>(),
)?;
m.add_class::<::gomori::Card>()?;
m.add_class::<::gomori::Rank>()?;
m.add_class::<::gomori::Suit>()?;
Expand Down
2 changes: 1 addition & 1 deletion gomori/src/board.rs
Original file line number Diff line number Diff line change
Expand Up @@ -398,8 +398,8 @@ impl Diff {
mod python {
use pyo3::pymethods;

use crate::{BoundingBox, CardToPlace, IllegalCardPlayed};
use super::*;
use crate::{BoundingBox, CardToPlace, IllegalCardPlayed};

#[pymethods]
impl Board {
Expand Down
1 change: 0 additions & 1 deletion gomori/src/board/compact_field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,6 @@ mod python {
self.into_field(i, j)
}
}

}
#[cfg(test)]
mod test {
Expand Down
2 changes: 1 addition & 1 deletion gomori/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,4 +134,4 @@ mod python {
}
}
#[cfg(feature = "python")]
pub use python::*;
pub use python::*;
52 changes: 30 additions & 22 deletions judge/src/player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ impl PlayerConfig {
}
Ok(config)
};
inner().with_context(|| format!("Trying to read config file '{}'", path.display()))
inner().with_context(|| format!("Could not read config file '{}'", path.display()))
}
}

Expand All @@ -53,7 +53,8 @@ impl Player {
.args(&config.cmd[1..])
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.spawn()?;
.spawn()
.with_context(|| format!("Failed to spawn child process {:?}", &config.cmd))?;
info!(cmd = ?config.cmd, "Spawned child process");

Ok(Self {
Expand All @@ -73,29 +74,36 @@ impl<'a> PlayerWithGameState<'a> {
}
}

pub fn perform_request<T: serde::de::DeserializeOwned + std::fmt::Debug>(
pub fn perform_request<T: serde::de::DeserializeOwned>(
&mut self,
recorder: &mut Option<Recorder>,
req: &Request,
) -> anyhow::Result<T> {
let mut req_json = serde_json::to_string(req)?;
trace!(name: "Sending request", player = &self.player.name, request = %req_json);
req_json.push('\n');
self.player.stdin.write_all(req_json.as_bytes())?;
self.player.stdin.flush()?;
self.player.buf.clear();
self.player.stdout.read_line(&mut self.player.buf)?;
let serialized_response = self.player.buf.trim_end();
let response = serde_json::from_str::<T>(serialized_response)?;
trace!(name: "Recieved response", player = &self.player.name, response = %serialized_response);

if let Some(recorder) = recorder {
recorder.store_request(
&self.player.name,
req_json,
String::from(serialized_response),
);
}
Ok(response)
let mut inner = || -> anyhow::Result<T> {
let mut req_json = serde_json::to_string(req)?;
trace!(name: "Sending request", player = &self.player.name, request = %req_json);
req_json.push('\n');
self.player
.stdin
.write_all(req_json.as_bytes())
.context("Could not send request")?;
self.player.stdin.flush()?;
self.player.buf.clear();
self.player.stdout.read_line(&mut self.player.buf)?;
let serialized_response = self.player.buf.trim_end();
let response = serde_json::from_str::<T>(serialized_response).with_context(|| {
format!("Could not parse response '{}' as JSON", serialized_response)
})?;
trace!(name: "Recieved response", player = &self.player.name, response = %serialized_response);
if let Some(recorder) = recorder {
recorder.store_request(
&self.player.name,
req_json,
String::from(serialized_response),
);
}
Ok(response)
};
inner().with_context(|| format!("Failed to make a request to '{}'", self.player.name))
}
}

0 comments on commit b46e585

Please sign in to comment.