Skip to content

Commit f15dee2

Browse files
centralize commands
1 parent b13d19a commit f15dee2

File tree

4 files changed

+68
-23
lines changed

4 files changed

+68
-23
lines changed

crates/djls-django/src/django.rs

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::gis::{check_gis_setup, GISError};
22
use djls_ipc::v1::*;
3+
use djls_ipc::IpcCommand;
34
use djls_ipc::{ProcessError, PythonProcess, TransportError};
45
use djls_python::Python;
56
use std::fmt;
@@ -35,23 +36,12 @@ impl DjangoProject {
3536
});
3637
}
3738

38-
let request = messages::Request {
39-
command: Some(messages::request::Command::DjangoGetProjectInfo(
40-
django::GetProjectInfoRequest {},
41-
)),
42-
};
43-
44-
let response = python
45-
.send(request)
46-
.map_err(|e| ProjectError::Transport(e))?;
39+
let response = django::GetProjectInfoRequest::execute(&mut python)?;
4740

4841
let version = match response.result {
4942
Some(messages::response::Result::DjangoGetProjectInfo(response)) => {
5043
response.project.unwrap().version
5144
}
52-
Some(messages::response::Result::Error(e)) => {
53-
return Err(ProjectError::Process(ProcessError::Health(e.message)));
54-
}
5545
_ => {
5646
return Err(ProjectError::Process(ProcessError::Response));
5747
}

crates/djls-ipc/src/commands.rs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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+
}

crates/djls-ipc/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
mod commands;
12
mod process;
23
mod proto;
34
mod transport;
45

6+
pub use commands::IpcCommand;
57
pub use process::ProcessError;
68
pub use process::PythonProcess;
79
pub use proto::v1;

crates/djls-python/src/python.rs

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::packaging::{Packages, PackagingError};
22
use djls_ipc::v1::*;
3+
use djls_ipc::IpcCommand;
34
use djls_ipc::{ProcessError, PythonProcess, TransportError};
45
use serde::Deserialize;
56
use std::fmt;
@@ -90,22 +91,12 @@ pub struct Python {
9091

9192
impl Python {
9293
pub fn setup(python: &mut PythonProcess) -> Result<Self, PythonError> {
93-
let request = messages::Request {
94-
command: Some(messages::request::Command::PythonGetEnvironment(
95-
python::GetEnvironmentRequest {},
96-
)),
97-
};
98-
99-
let response = python.send(request).map_err(PythonError::Transport)?;
100-
94+
let response = python::GetEnvironmentRequest::execute(python)?;
10195
match response.result {
10296
Some(messages::response::Result::PythonGetEnvironment(response)) => response
10397
.python
10498
.ok_or_else(|| PythonError::Process(ProcessError::Response))
10599
.map(Into::into),
106-
Some(messages::response::Result::Error(e)) => {
107-
Err(PythonError::Process(ProcessError::Health(e.message)))
108-
}
109100
_ => Err(PythonError::Process(ProcessError::Response)),
110101
}
111102
}

0 commit comments

Comments
 (0)