centralize commands (#28)

This commit is contained in:
Josh Thomas 2024-12-12 22:51:35 -06:00 committed by GitHub
parent b13d19a4bf
commit b993e35460
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 68 additions and 23 deletions

View file

@ -0,0 +1,62 @@
use crate::proto::v1::{self, messages};
use crate::{ProcessError, PythonProcess};
pub trait IpcCommand: Default {
fn into_request(&self) -> messages::Request;
fn from_response(response: messages::Response) -> Result<messages::Response, ProcessError>;
fn execute(process: &mut PythonProcess) -> Result<messages::Response, ProcessError> {
let cmd = Self::default();
let request = cmd.into_request();
let response = process.send(request).map_err(ProcessError::Transport)?;
Self::from_response(response)
}
}
impl IpcCommand for v1::check::HealthRequest {
fn into_request(&self) -> messages::Request {
messages::Request {
command: Some(messages::request::Command::CheckHealth(*self)),
}
}
fn from_response(response: messages::Response) -> Result<messages::Response, ProcessError> {
match response.result {
Some(messages::response::Result::CheckHealth(_)) => Ok(response),
Some(messages::response::Result::Error(e)) => Err(ProcessError::Health(e.message)),
_ => Err(ProcessError::Response),
}
}
}
impl IpcCommand for v1::python::GetEnvironmentRequest {
fn into_request(&self) -> messages::Request {
messages::Request {
command: Some(messages::request::Command::PythonGetEnvironment(*self)),
}
}
fn from_response(response: messages::Response) -> Result<messages::Response, ProcessError> {
match response.result {
Some(messages::response::Result::PythonGetEnvironment(_)) => Ok(response),
Some(messages::response::Result::Error(e)) => Err(ProcessError::Health(e.message)),
_ => Err(ProcessError::Response),
}
}
}
impl IpcCommand for v1::django::GetProjectInfoRequest {
fn into_request(&self) -> messages::Request {
messages::Request {
command: Some(messages::request::Command::DjangoGetProjectInfo(*self)),
}
}
fn from_response(response: messages::Response) -> Result<messages::Response, ProcessError> {
match response.result {
Some(messages::response::Result::DjangoGetProjectInfo(_)) => Ok(response),
Some(messages::response::Result::Error(e)) => Err(ProcessError::Health(e.message)),
_ => Err(ProcessError::Response),
}
}
}

View file

@ -1,7 +1,9 @@
mod commands;
mod process;
mod proto;
mod transport;
pub use commands::IpcCommand;
pub use process::ProcessError;
pub use process::PythonProcess;
pub use proto::v1;