mirror of
https://github.com/joshuadavidthomas/django-language-server.git
synced 2025-09-28 04:45:03 +00:00
centralize commands (#28)
This commit is contained in:
parent
b13d19a4bf
commit
b993e35460
4 changed files with 68 additions and 23 deletions
62
crates/djls-ipc/src/commands.rs
Normal file
62
crates/djls-ipc/src/commands.rs
Normal 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),
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue