mirror of https://github.com/helix-editor/helix
helix-lsp cleanup
parent
960bc9f134
commit
ea6667070f
|
@ -237,12 +237,6 @@ dependencies = [
|
||||||
"wasi",
|
"wasi",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "glob"
|
|
||||||
version = "0.3.0"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "9b919933a397b79c37e33b77bb2aa3dc8eb6e165ad809e58ff75bc7db2e34574"
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "globset"
|
name = "globset"
|
||||||
version = "0.4.6"
|
version = "0.4.6"
|
||||||
|
@ -282,19 +276,15 @@ dependencies = [
|
||||||
"anyhow",
|
"anyhow",
|
||||||
"futures-executor",
|
"futures-executor",
|
||||||
"futures-util",
|
"futures-util",
|
||||||
"glob",
|
|
||||||
"helix-core",
|
"helix-core",
|
||||||
"jsonrpc-core",
|
"jsonrpc-core",
|
||||||
"log",
|
"log",
|
||||||
"lsp-types",
|
"lsp-types",
|
||||||
"once_cell",
|
|
||||||
"pathdiff",
|
|
||||||
"serde",
|
"serde",
|
||||||
"serde_json",
|
"serde_json",
|
||||||
"thiserror",
|
"thiserror",
|
||||||
"tokio",
|
"tokio",
|
||||||
"tokio-stream",
|
"tokio-stream",
|
||||||
"url",
|
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
@ -596,12 +586,6 @@ dependencies = [
|
||||||
"winapi",
|
"winapi",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "pathdiff"
|
|
||||||
version = "0.2.0"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "877630b3de15c0b64cc52f659345724fbf6bdad9bd9566699fc53688f3c34a34"
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "percent-encoding"
|
name = "percent-encoding"
|
||||||
version = "2.1.0"
|
version = "2.1.0"
|
||||||
|
|
|
@ -10,19 +10,14 @@ license = "MPL-2.0"
|
||||||
[dependencies]
|
[dependencies]
|
||||||
helix-core = { path = "../helix-core" }
|
helix-core = { path = "../helix-core" }
|
||||||
|
|
||||||
once_cell = "1.4"
|
anyhow = "1.0"
|
||||||
|
futures-executor = "0.3"
|
||||||
lsp-types = { version = "0.89", features = ["proposed"] }
|
|
||||||
tokio = { version = "1", features = ["full"] }
|
|
||||||
tokio-stream = "0.1.5"
|
|
||||||
futures-executor = { version = "0.3" }
|
|
||||||
url = "2"
|
|
||||||
pathdiff = "0.2"
|
|
||||||
glob = "0.3"
|
|
||||||
anyhow = "1"
|
|
||||||
serde_json = "1.0"
|
|
||||||
serde = { version = "1.0", features = ["derive"] }
|
|
||||||
jsonrpc-core = { version = "17.1", default-features = false } # don't pull in all of futures
|
|
||||||
futures-util = { version = "0.3", features = ["std", "async-await"], default-features = false }
|
futures-util = { version = "0.3", features = ["std", "async-await"], default-features = false }
|
||||||
|
jsonrpc-core = { version = "17.1", default-features = false } # don't pull in all of futures
|
||||||
|
log = "0.4"
|
||||||
|
lsp-types = { version = "0.89", features = ["proposed"] }
|
||||||
|
serde = { version = "1.0", features = ["derive"] }
|
||||||
|
serde_json = "1.0"
|
||||||
thiserror = "1.0"
|
thiserror = "1.0"
|
||||||
log = "~0.4"
|
tokio = { version = "1.6", features = ["full"] }
|
||||||
|
tokio-stream = "0.1.6"
|
|
@ -4,30 +4,22 @@ use crate::{
|
||||||
};
|
};
|
||||||
|
|
||||||
use helix_core::{find_root, ChangeSet, Rope};
|
use helix_core::{find_root, ChangeSet, Rope};
|
||||||
|
|
||||||
// use std::collections::HashMap;
|
|
||||||
use std::future::Future;
|
|
||||||
use std::sync::atomic::{AtomicU64, Ordering};
|
|
||||||
|
|
||||||
use jsonrpc_core as jsonrpc;
|
use jsonrpc_core as jsonrpc;
|
||||||
use lsp_types as lsp;
|
use lsp_types as lsp;
|
||||||
use serde_json::Value;
|
use serde_json::Value;
|
||||||
|
use std::future::Future;
|
||||||
use std::process::Stdio;
|
use std::process::Stdio;
|
||||||
|
use std::sync::atomic::{AtomicU64, Ordering};
|
||||||
use tokio::{
|
use tokio::{
|
||||||
io::{BufReader, BufWriter},
|
io::{BufReader, BufWriter},
|
||||||
// prelude::*,
|
|
||||||
process::{Child, Command},
|
process::{Child, Command},
|
||||||
sync::mpsc::{channel, UnboundedReceiver, UnboundedSender},
|
sync::mpsc::{channel, UnboundedReceiver, UnboundedSender},
|
||||||
};
|
};
|
||||||
|
|
||||||
pub struct Client {
|
pub struct Client {
|
||||||
_process: Child,
|
_process: Child,
|
||||||
|
server_tx: UnboundedSender<Payload>,
|
||||||
outgoing: UnboundedSender<Payload>,
|
request_counter: AtomicU64,
|
||||||
// pub incoming: Receiver<Call>,
|
|
||||||
pub request_counter: AtomicU64,
|
|
||||||
|
|
||||||
capabilities: Option<lsp::ServerCapabilities>,
|
capabilities: Option<lsp::ServerCapabilities>,
|
||||||
offset_encoding: OffsetEncoding,
|
offset_encoding: OffsetEncoding,
|
||||||
}
|
}
|
||||||
|
@ -43,40 +35,27 @@ impl Client {
|
||||||
.kill_on_drop(true)
|
.kill_on_drop(true)
|
||||||
.spawn();
|
.spawn();
|
||||||
|
|
||||||
// use std::io::ErrorKind;
|
let mut process = process?;
|
||||||
let mut process = match process {
|
|
||||||
Ok(process) => process,
|
|
||||||
Err(err) => match err.kind() {
|
|
||||||
// ErrorKind::NotFound | ErrorKind::PermissionDenied => {
|
|
||||||
// return Err(Error::Other(err.into()))
|
|
||||||
// }
|
|
||||||
_kind => return Err(Error::Other(err.into())),
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
// TODO: do we need bufreader/writer here? or do we use async wrappers on unblock?
|
// TODO: do we need bufreader/writer here? or do we use async wrappers on unblock?
|
||||||
let writer = BufWriter::new(process.stdin.take().expect("Failed to open stdin"));
|
let writer = BufWriter::new(process.stdin.take().expect("Failed to open stdin"));
|
||||||
let reader = BufReader::new(process.stdout.take().expect("Failed to open stdout"));
|
let reader = BufReader::new(process.stdout.take().expect("Failed to open stdout"));
|
||||||
let stderr = BufReader::new(process.stderr.take().expect("Failed to open stderr"));
|
let stderr = BufReader::new(process.stderr.take().expect("Failed to open stderr"));
|
||||||
|
|
||||||
let (incoming, outgoing) = Transport::start(reader, writer, stderr);
|
let (server_rx, server_tx) = Transport::start(reader, writer, stderr);
|
||||||
|
|
||||||
let client = Self {
|
let client = Self {
|
||||||
_process: process,
|
_process: process,
|
||||||
|
server_tx,
|
||||||
outgoing,
|
|
||||||
// incoming,
|
|
||||||
request_counter: AtomicU64::new(0),
|
request_counter: AtomicU64::new(0),
|
||||||
|
|
||||||
capabilities: None,
|
capabilities: None,
|
||||||
// diagnostics: HashMap::new(),
|
|
||||||
offset_encoding: OffsetEncoding::Utf8,
|
offset_encoding: OffsetEncoding::Utf8,
|
||||||
};
|
};
|
||||||
|
|
||||||
// TODO: async client.initialize()
|
// TODO: async client.initialize()
|
||||||
// maybe use an arc<atomic> flag
|
// maybe use an arc<atomic> flag
|
||||||
|
|
||||||
Ok((client, incoming))
|
Ok((client, server_rx))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn next_request_id(&self) -> jsonrpc::Id {
|
fn next_request_id(&self) -> jsonrpc::Id {
|
||||||
|
@ -106,7 +85,7 @@ impl Client {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Execute a RPC request on the language server.
|
/// Execute a RPC request on the language server.
|
||||||
pub async fn request<R: lsp::request::Request>(&self, params: R::Params) -> Result<R::Result>
|
async fn request<R: lsp::request::Request>(&self, params: R::Params) -> Result<R::Result>
|
||||||
where
|
where
|
||||||
R::Params: serde::Serialize,
|
R::Params: serde::Serialize,
|
||||||
R::Result: core::fmt::Debug, // TODO: temporary
|
R::Result: core::fmt::Debug, // TODO: temporary
|
||||||
|
@ -118,17 +97,20 @@ impl Client {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Execute a RPC request on the language server.
|
/// Execute a RPC request on the language server.
|
||||||
pub fn call<R: lsp::request::Request>(
|
fn call<R: lsp::request::Request>(
|
||||||
&self,
|
&self,
|
||||||
params: R::Params,
|
params: R::Params,
|
||||||
) -> impl Future<Output = Result<Value>>
|
) -> impl Future<Output = Result<Value>>
|
||||||
where
|
where
|
||||||
R::Params: serde::Serialize,
|
R::Params: serde::Serialize,
|
||||||
{
|
{
|
||||||
let outgoing = self.outgoing.clone();
|
let server_tx = self.server_tx.clone();
|
||||||
let id = self.next_request_id();
|
let id = self.next_request_id();
|
||||||
|
|
||||||
async move {
|
async move {
|
||||||
|
use std::time::Duration;
|
||||||
|
use tokio::time::timeout;
|
||||||
|
|
||||||
let params = serde_json::to_value(params)?;
|
let params = serde_json::to_value(params)?;
|
||||||
|
|
||||||
let request = jsonrpc::MethodCall {
|
let request = jsonrpc::MethodCall {
|
||||||
|
@ -140,16 +122,13 @@ impl Client {
|
||||||
|
|
||||||
let (tx, mut rx) = channel::<Result<Value>>(1);
|
let (tx, mut rx) = channel::<Result<Value>>(1);
|
||||||
|
|
||||||
outgoing
|
server_tx
|
||||||
.send(Payload::Request {
|
.send(Payload::Request {
|
||||||
chan: tx,
|
chan: tx,
|
||||||
value: request,
|
value: request,
|
||||||
})
|
})
|
||||||
.map_err(|e| Error::Other(e.into()))?;
|
.map_err(|e| Error::Other(e.into()))?;
|
||||||
|
|
||||||
use std::time::Duration;
|
|
||||||
use tokio::time::timeout;
|
|
||||||
|
|
||||||
timeout(Duration::from_secs(2), rx.recv())
|
timeout(Duration::from_secs(2), rx.recv())
|
||||||
.await
|
.await
|
||||||
.map_err(|_| Error::Timeout)? // return Timeout
|
.map_err(|_| Error::Timeout)? // return Timeout
|
||||||
|
@ -158,14 +137,14 @@ impl Client {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Send a RPC notification to the language server.
|
/// Send a RPC notification to the language server.
|
||||||
pub fn notify<R: lsp::notification::Notification>(
|
fn notify<R: lsp::notification::Notification>(
|
||||||
&self,
|
&self,
|
||||||
params: R::Params,
|
params: R::Params,
|
||||||
) -> impl Future<Output = Result<()>>
|
) -> impl Future<Output = Result<()>>
|
||||||
where
|
where
|
||||||
R::Params: serde::Serialize,
|
R::Params: serde::Serialize,
|
||||||
{
|
{
|
||||||
let outgoing = self.outgoing.clone();
|
let server_tx = self.server_tx.clone();
|
||||||
|
|
||||||
async move {
|
async move {
|
||||||
let params = serde_json::to_value(params)?;
|
let params = serde_json::to_value(params)?;
|
||||||
|
@ -176,7 +155,7 @@ impl Client {
|
||||||
params: Self::value_into_params(params),
|
params: Self::value_into_params(params),
|
||||||
};
|
};
|
||||||
|
|
||||||
outgoing
|
server_tx
|
||||||
.send(Payload::Notification(notification))
|
.send(Payload::Notification(notification))
|
||||||
.map_err(|e| Error::Other(e.into()))?;
|
.map_err(|e| Error::Other(e.into()))?;
|
||||||
|
|
||||||
|
@ -205,7 +184,7 @@ impl Client {
|
||||||
}),
|
}),
|
||||||
};
|
};
|
||||||
|
|
||||||
self.outgoing
|
self.server_tx
|
||||||
.send(Payload::Response(output))
|
.send(Payload::Response(output))
|
||||||
.map_err(|e| Error::Other(e.into()))?;
|
.map_err(|e| Error::Other(e.into()))?;
|
||||||
|
|
||||||
|
@ -216,7 +195,7 @@ impl Client {
|
||||||
// General messages
|
// General messages
|
||||||
// -------------------------------------------------------------------------------------------
|
// -------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
pub async fn initialize(&mut self) -> Result<()> {
|
pub(crate) async fn initialize(&mut self) -> Result<()> {
|
||||||
// TODO: delay any requests that are triggered prior to initialize
|
// TODO: delay any requests that are triggered prior to initialize
|
||||||
let root = find_root(None).and_then(|root| lsp::Url::from_file_path(root).ok());
|
let root = find_root(None).and_then(|root| lsp::Url::from_file_path(root).ok());
|
||||||
|
|
||||||
|
|
|
@ -1,28 +1,27 @@
|
||||||
mod client;
|
mod client;
|
||||||
mod transport;
|
mod transport;
|
||||||
|
|
||||||
|
pub use client::Client;
|
||||||
|
pub use futures_executor::block_on;
|
||||||
|
pub use jsonrpc::Call;
|
||||||
pub use jsonrpc_core as jsonrpc;
|
pub use jsonrpc_core as jsonrpc;
|
||||||
|
pub use lsp::{Position, Url};
|
||||||
pub use lsp_types as lsp;
|
pub use lsp_types as lsp;
|
||||||
|
|
||||||
pub use client::Client;
|
use futures_util::stream::select_all::SelectAll;
|
||||||
pub use lsp::{Position, Url};
|
|
||||||
|
|
||||||
pub type Result<T> = core::result::Result<T, Error>;
|
|
||||||
|
|
||||||
use helix_core::syntax::LanguageConfiguration;
|
use helix_core::syntax::LanguageConfiguration;
|
||||||
|
|
||||||
use thiserror::Error;
|
|
||||||
|
|
||||||
use std::{
|
use std::{
|
||||||
collections::{hash_map::Entry, HashMap},
|
collections::{hash_map::Entry, HashMap},
|
||||||
sync::Arc,
|
sync::Arc,
|
||||||
};
|
};
|
||||||
|
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
use thiserror::Error;
|
||||||
use tokio_stream::wrappers::UnboundedReceiverStream;
|
use tokio_stream::wrappers::UnboundedReceiverStream;
|
||||||
|
|
||||||
pub use futures_executor::block_on;
|
pub type Result<T> = core::result::Result<T, Error>;
|
||||||
|
type LanguageId = String;
|
||||||
|
|
||||||
#[derive(Error, Debug)]
|
#[derive(Error, Debug)]
|
||||||
pub enum Error {
|
pub enum Error {
|
||||||
|
@ -30,6 +29,8 @@ pub enum Error {
|
||||||
Rpc(#[from] jsonrpc::Error),
|
Rpc(#[from] jsonrpc::Error),
|
||||||
#[error("failed to parse: {0}")]
|
#[error("failed to parse: {0}")]
|
||||||
Parse(#[from] serde_json::Error),
|
Parse(#[from] serde_json::Error),
|
||||||
|
#[error("IO Error: {0}")]
|
||||||
|
IO(#[from] std::io::Error),
|
||||||
#[error("request timed out")]
|
#[error("request timed out")]
|
||||||
Timeout,
|
Timeout,
|
||||||
#[error("server closed the stream")]
|
#[error("server closed the stream")]
|
||||||
|
@ -126,8 +127,6 @@ pub mod util {
|
||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
// apply_insert_replace_edit
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Clone)]
|
#[derive(Debug, PartialEq, Clone)]
|
||||||
|
@ -173,12 +172,6 @@ impl Notification {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub use jsonrpc::Call;
|
|
||||||
|
|
||||||
type LanguageId = String;
|
|
||||||
|
|
||||||
use futures_util::stream::select_all::SelectAll;
|
|
||||||
|
|
||||||
pub struct Registry {
|
pub struct Registry {
|
||||||
inner: HashMap<LanguageId, Arc<Client>>,
|
inner: HashMap<LanguageId, Arc<Client>>,
|
||||||
|
|
||||||
|
|
|
@ -1,15 +1,9 @@
|
||||||
use std::collections::HashMap;
|
use crate::Result;
|
||||||
use std::io;
|
|
||||||
|
|
||||||
use log::{error, info};
|
|
||||||
|
|
||||||
use crate::Error;
|
|
||||||
|
|
||||||
type Result<T> = core::result::Result<T, Error>;
|
|
||||||
|
|
||||||
use jsonrpc_core as jsonrpc;
|
use jsonrpc_core as jsonrpc;
|
||||||
|
use log::{error, info};
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
use serde_json::Value;
|
use serde_json::Value;
|
||||||
|
use std::collections::HashMap;
|
||||||
use tokio::{
|
use tokio::{
|
||||||
io::{AsyncBufRead, AsyncBufReadExt, AsyncReadExt, AsyncWriteExt, BufReader, BufWriter},
|
io::{AsyncBufRead, AsyncBufReadExt, AsyncReadExt, AsyncWriteExt, BufReader, BufWriter},
|
||||||
process::{ChildStderr, ChildStdin, ChildStdout},
|
process::{ChildStderr, ChildStdin, ChildStdout},
|
||||||
|
@ -26,12 +20,11 @@ pub enum Payload {
|
||||||
Response(jsonrpc::Output),
|
Response(jsonrpc::Output),
|
||||||
}
|
}
|
||||||
|
|
||||||
use serde::{Deserialize, Serialize};
|
|
||||||
/// A type representing all possible values sent from the server to the client.
|
/// A type representing all possible values sent from the server to the client.
|
||||||
#[derive(Debug, PartialEq, Clone, Deserialize, Serialize)]
|
#[derive(Debug, PartialEq, Clone, Deserialize, Serialize)]
|
||||||
#[serde(deny_unknown_fields)]
|
#[serde(deny_unknown_fields)]
|
||||||
#[serde(untagged)]
|
#[serde(untagged)]
|
||||||
enum Message {
|
enum ServerMessage {
|
||||||
/// A regular JSON-RPC request output (single response).
|
/// A regular JSON-RPC request output (single response).
|
||||||
Output(jsonrpc::Output),
|
Output(jsonrpc::Output),
|
||||||
/// A JSON-RPC request or notification.
|
/// A JSON-RPC request or notification.
|
||||||
|
@ -39,34 +32,32 @@ enum Message {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Transport {
|
pub struct Transport {
|
||||||
incoming: UnboundedSender<jsonrpc::Call>,
|
client_tx: UnboundedSender<jsonrpc::Call>,
|
||||||
outgoing: UnboundedReceiver<Payload>,
|
client_rx: UnboundedReceiver<Payload>,
|
||||||
|
|
||||||
pending_requests: HashMap<jsonrpc::Id, Sender<Result<Value>>>,
|
pending_requests: HashMap<jsonrpc::Id, Sender<Result<Value>>>,
|
||||||
headers: HashMap<String, String>,
|
|
||||||
|
|
||||||
writer: BufWriter<ChildStdin>,
|
server_stdin: BufWriter<ChildStdin>,
|
||||||
reader: BufReader<ChildStdout>,
|
server_stdout: BufReader<ChildStdout>,
|
||||||
stderr: BufReader<ChildStderr>,
|
server_stderr: BufReader<ChildStderr>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Transport {
|
impl Transport {
|
||||||
pub fn start(
|
pub fn start(
|
||||||
reader: BufReader<ChildStdout>,
|
server_stdout: BufReader<ChildStdout>,
|
||||||
writer: BufWriter<ChildStdin>,
|
server_stdin: BufWriter<ChildStdin>,
|
||||||
stderr: BufReader<ChildStderr>,
|
server_stderr: BufReader<ChildStderr>,
|
||||||
) -> (UnboundedReceiver<jsonrpc::Call>, UnboundedSender<Payload>) {
|
) -> (UnboundedReceiver<jsonrpc::Call>, UnboundedSender<Payload>) {
|
||||||
let (incoming, rx) = unbounded_channel();
|
let (client_tx, rx) = unbounded_channel();
|
||||||
let (tx, outgoing) = unbounded_channel();
|
let (tx, client_rx) = unbounded_channel();
|
||||||
|
|
||||||
let transport = Self {
|
let transport = Self {
|
||||||
reader,
|
server_stdout,
|
||||||
writer,
|
server_stdin,
|
||||||
stderr,
|
server_stderr,
|
||||||
incoming,
|
client_tx,
|
||||||
outgoing,
|
client_rx,
|
||||||
pending_requests: HashMap::default(),
|
pending_requests: HashMap::default(),
|
||||||
headers: HashMap::default(),
|
|
||||||
};
|
};
|
||||||
|
|
||||||
tokio::spawn(transport.duplex());
|
tokio::spawn(transport.duplex());
|
||||||
|
@ -74,105 +65,104 @@ impl Transport {
|
||||||
(rx, tx)
|
(rx, tx)
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn recv(
|
async fn recv_server_message(
|
||||||
reader: &mut (impl AsyncBufRead + Unpin + Send),
|
reader: &mut (impl AsyncBufRead + Unpin + Send),
|
||||||
headers: &mut HashMap<String, String>,
|
buffer: &mut String,
|
||||||
) -> core::result::Result<Message, std::io::Error> {
|
) -> Result<ServerMessage> {
|
||||||
// read headers
|
let mut content_length = None;
|
||||||
loop {
|
loop {
|
||||||
let mut header = String::new();
|
buffer.truncate(0);
|
||||||
// detect pipe closed if 0
|
reader.read_line(buffer).await?;
|
||||||
reader.read_line(&mut header).await?;
|
let header = buffer.trim();
|
||||||
let header = header.trim();
|
|
||||||
|
|
||||||
if header.is_empty() {
|
if header.is_empty() {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
let parts: Vec<&str> = header.split(": ").collect();
|
let mut parts = header.split(": ");
|
||||||
if parts.len() != 2 {
|
|
||||||
return Err(std::io::Error::new(
|
match (parts.next(), parts.next(), parts.next()) {
|
||||||
std::io::ErrorKind::Other,
|
(Some("Content-Length"), Some(value), None) => {
|
||||||
"Failed to parse header",
|
content_length = Some(value.parse().unwrap());
|
||||||
));
|
}
|
||||||
|
(Some(_), Some(_), None) => {}
|
||||||
|
_ => {
|
||||||
|
return Err(std::io::Error::new(
|
||||||
|
std::io::ErrorKind::Other,
|
||||||
|
"Failed to parse header",
|
||||||
|
)
|
||||||
|
.into());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
headers.insert(parts[0].to_string(), parts[1].to_string());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// find content-length
|
let content_length = content_length.unwrap();
|
||||||
let content_length = headers.get("Content-Length").unwrap().parse().unwrap();
|
|
||||||
|
|
||||||
|
//TODO: reuse vector
|
||||||
let mut content = vec![0; content_length];
|
let mut content = vec![0; content_length];
|
||||||
reader.read_exact(&mut content).await?;
|
reader.read_exact(&mut content).await?;
|
||||||
let msg = String::from_utf8(content).unwrap();
|
let msg = String::from_utf8(content).unwrap();
|
||||||
|
|
||||||
// read data
|
|
||||||
|
|
||||||
info!("<- {}", msg);
|
info!("<- {}", msg);
|
||||||
|
|
||||||
// try parsing as output (server response) or call (server request)
|
// try parsing as output (server response) or call (server request)
|
||||||
let output: serde_json::Result<Message> = serde_json::from_str(&msg);
|
let output: serde_json::Result<ServerMessage> = serde_json::from_str(&msg);
|
||||||
|
|
||||||
Ok(output?)
|
Ok(output?)
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn err(
|
async fn recv_server_error(
|
||||||
err: &mut (impl AsyncBufRead + Unpin + Send),
|
err: &mut (impl AsyncBufRead + Unpin + Send),
|
||||||
) -> core::result::Result<(), std::io::Error> {
|
buffer: &mut String,
|
||||||
let mut line = String::new();
|
) -> Result<()> {
|
||||||
err.read_line(&mut line).await?;
|
buffer.truncate(0);
|
||||||
error!("err <- {}", line);
|
err.read_line(buffer).await?;
|
||||||
|
error!("err <- {}", buffer);
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn send_payload(&mut self, payload: Payload) -> io::Result<()> {
|
async fn send_payload_to_server(&mut self, payload: Payload) -> Result<()> {
|
||||||
match payload {
|
//TODO: reuse string
|
||||||
|
let json = match payload {
|
||||||
Payload::Request { chan, value } => {
|
Payload::Request { chan, value } => {
|
||||||
self.pending_requests.insert(value.id.clone(), chan);
|
self.pending_requests.insert(value.id.clone(), chan);
|
||||||
|
serde_json::to_string(&value)?
|
||||||
let json = serde_json::to_string(&value)?;
|
|
||||||
self.send(json).await
|
|
||||||
}
|
}
|
||||||
Payload::Notification(value) => {
|
Payload::Notification(value) => serde_json::to_string(&value)?,
|
||||||
let json = serde_json::to_string(&value)?;
|
Payload::Response(error) => serde_json::to_string(&error)?,
|
||||||
self.send(json).await
|
};
|
||||||
}
|
self.send_string_to_server(json).await
|
||||||
Payload::Response(error) => {
|
|
||||||
let json = serde_json::to_string(&error)?;
|
|
||||||
self.send(json).await
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn send(&mut self, request: String) -> io::Result<()> {
|
async fn send_string_to_server(&mut self, request: String) -> Result<()> {
|
||||||
info!("-> {}", request);
|
info!("-> {}", request);
|
||||||
|
|
||||||
// send the headers
|
// send the headers
|
||||||
self.writer
|
self.server_stdin
|
||||||
.write_all(format!("Content-Length: {}\r\n\r\n", request.len()).as_bytes())
|
.write_all(format!("Content-Length: {}\r\n\r\n", request.len()).as_bytes())
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
// send the body
|
// send the body
|
||||||
self.writer.write_all(request.as_bytes()).await?;
|
self.server_stdin.write_all(request.as_bytes()).await?;
|
||||||
|
|
||||||
self.writer.flush().await?;
|
self.server_stdin.flush().await?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn recv_msg(&mut self, msg: Message) -> anyhow::Result<()> {
|
async fn process_server_message(&mut self, msg: ServerMessage) -> Result<()> {
|
||||||
match msg {
|
match msg {
|
||||||
Message::Output(output) => self.recv_response(output).await?,
|
ServerMessage::Output(output) => self.process_request_response(output).await?,
|
||||||
Message::Call(call) => {
|
ServerMessage::Call(call) => {
|
||||||
self.incoming.send(call).unwrap();
|
self.client_tx.send(call).unwrap();
|
||||||
// let notification = Notification::parse(&method, params);
|
// let notification = Notification::parse(&method, params);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn recv_response(&mut self, output: jsonrpc::Output) -> io::Result<()> {
|
async fn process_request_response(&mut self, output: jsonrpc::Output) -> Result<()> {
|
||||||
let (id, result) = match output {
|
let (id, result) = match output {
|
||||||
jsonrpc::Output::Success(jsonrpc::Success { id, result, .. }) => {
|
jsonrpc::Output::Success(jsonrpc::Success { id, result, .. }) => {
|
||||||
info!("<- {}", result);
|
info!("<- {}", result);
|
||||||
|
@ -200,29 +190,33 @@ impl Transport {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn duplex(mut self) {
|
async fn duplex(mut self) {
|
||||||
|
let mut recv_buffer = String::new();
|
||||||
|
let mut err_buffer = String::new();
|
||||||
loop {
|
loop {
|
||||||
tokio::select! {
|
tokio::select! {
|
||||||
// client -> server
|
// client -> server
|
||||||
msg = self.outgoing.recv() => {
|
msg = self.client_rx.recv() => {
|
||||||
if msg.is_none() {
|
match msg {
|
||||||
break;
|
Some(msg) => {
|
||||||
|
self.send_payload_to_server(msg).await.unwrap()
|
||||||
|
},
|
||||||
|
None => break
|
||||||
}
|
}
|
||||||
let msg = msg.unwrap();
|
|
||||||
|
|
||||||
self.send_payload(msg).await.unwrap();
|
|
||||||
}
|
}
|
||||||
// server <- client
|
// server -> client
|
||||||
msg = Self::recv(&mut self.reader, &mut self.headers) => {
|
msg = Self::recv_server_message(&mut self.server_stdout, &mut recv_buffer) => {
|
||||||
if msg.is_err() {
|
match msg {
|
||||||
error!("err: <- {:?}", msg);
|
Ok(msg) => {
|
||||||
break;
|
self.process_server_message(msg).await.unwrap();
|
||||||
|
}
|
||||||
|
Err(_) => {
|
||||||
|
error!("err: <- {:?}", msg);
|
||||||
|
break;
|
||||||
|
},
|
||||||
}
|
}
|
||||||
let msg = msg.unwrap();
|
|
||||||
|
|
||||||
self.recv_msg(msg).await.unwrap();
|
|
||||||
}
|
}
|
||||||
_msg = Self::err(&mut self.stderr) => {}
|
_msg = Self::recv_server_error(&mut self.server_stderr, &mut err_buffer) => {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue