|
| 1 | +use crate::proto::v1::{self, messages}; |
| 2 | +use crate::{ProcessError, PythonProcess}; |
| 3 | + |
| 4 | +pub trait IpcCommand: Default { |
| 5 | + fn into_request(&self) -> messages::Request; |
| 6 | + fn from_response(response: messages::Response) -> Result<messages::Response, ProcessError>; |
| 7 | + |
| 8 | + fn execute(process: &mut PythonProcess) -> Result<messages::Response, ProcessError> { |
| 9 | + let cmd = Self::default(); |
| 10 | + let request = cmd.into_request(); |
| 11 | + let response = process.send(request).map_err(ProcessError::Transport)?; |
| 12 | + Self::from_response(response) |
| 13 | + } |
| 14 | +} |
| 15 | + |
| 16 | +impl IpcCommand for v1::check::HealthRequest { |
| 17 | + fn into_request(&self) -> messages::Request { |
| 18 | + messages::Request { |
| 19 | + command: Some(messages::request::Command::CheckHealth(*self)), |
| 20 | + } |
| 21 | + } |
| 22 | + |
| 23 | + fn from_response(response: messages::Response) -> Result<messages::Response, ProcessError> { |
| 24 | + match response.result { |
| 25 | + Some(messages::response::Result::CheckHealth(_)) => Ok(response), |
| 26 | + Some(messages::response::Result::Error(e)) => Err(ProcessError::Health(e.message)), |
| 27 | + _ => Err(ProcessError::Response), |
| 28 | + } |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +impl IpcCommand for v1::python::GetEnvironmentRequest { |
| 33 | + fn into_request(&self) -> messages::Request { |
| 34 | + messages::Request { |
| 35 | + command: Some(messages::request::Command::PythonGetEnvironment(*self)), |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + fn from_response(response: messages::Response) -> Result<messages::Response, ProcessError> { |
| 40 | + match response.result { |
| 41 | + Some(messages::response::Result::PythonGetEnvironment(_)) => Ok(response), |
| 42 | + Some(messages::response::Result::Error(e)) => Err(ProcessError::Health(e.message)), |
| 43 | + _ => Err(ProcessError::Response), |
| 44 | + } |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +impl IpcCommand for v1::django::GetProjectInfoRequest { |
| 49 | + fn into_request(&self) -> messages::Request { |
| 50 | + messages::Request { |
| 51 | + command: Some(messages::request::Command::DjangoGetProjectInfo(*self)), |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + fn from_response(response: messages::Response) -> Result<messages::Response, ProcessError> { |
| 56 | + match response.result { |
| 57 | + Some(messages::response::Result::DjangoGetProjectInfo(_)) => Ok(response), |
| 58 | + Some(messages::response::Result::Error(e)) => Err(ProcessError::Health(e.message)), |
| 59 | + _ => Err(ProcessError::Response), |
| 60 | + } |
| 61 | + } |
| 62 | +} |
0 commit comments