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

@ -1,5 +1,6 @@
use crate::gis::{check_gis_setup, GISError};
use djls_ipc::v1::*;
use djls_ipc::IpcCommand;
use djls_ipc::{ProcessError, PythonProcess, TransportError};
use djls_python::Python;
use std::fmt;
@ -35,23 +36,12 @@ impl DjangoProject {
});
}
let request = messages::Request {
command: Some(messages::request::Command::DjangoGetProjectInfo(
django::GetProjectInfoRequest {},
)),
};
let response = python
.send(request)
.map_err(|e| ProjectError::Transport(e))?;
let response = django::GetProjectInfoRequest::execute(&mut python)?;
let version = match response.result {
Some(messages::response::Result::DjangoGetProjectInfo(response)) => {
response.project.unwrap().version
}
Some(messages::response::Result::Error(e)) => {
return Err(ProjectError::Process(ProcessError::Health(e.message)));
}
_ => {
return Err(ProjectError::Process(ProcessError::Response));
}

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;

View file

@ -1,5 +1,6 @@
use crate::packaging::{Packages, PackagingError};
use djls_ipc::v1::*;
use djls_ipc::IpcCommand;
use djls_ipc::{ProcessError, PythonProcess, TransportError};
use serde::Deserialize;
use std::fmt;
@ -90,22 +91,12 @@ pub struct Python {
impl Python {
pub fn setup(python: &mut PythonProcess) -> Result<Self, PythonError> {
let request = messages::Request {
command: Some(messages::request::Command::PythonGetEnvironment(
python::GetEnvironmentRequest {},
)),
};
let response = python.send(request).map_err(PythonError::Transport)?;
let response = python::GetEnvironmentRequest::execute(python)?;
match response.result {
Some(messages::response::Result::PythonGetEnvironment(response)) => response
.python
.ok_or_else(|| PythonError::Process(ProcessError::Response))
.map(Into::into),
Some(messages::response::Result::Error(e)) => {
Err(PythonError::Process(ProcessError::Health(e.message)))
}
_ => Err(PythonError::Process(ProcessError::Response)),
}
}