Skip to content

Commit

Permalink
Replace connect_{tcp,unix} with a single function
Browse files Browse the repository at this point in the history
Since config::Address now exists, there's no real need to be generic and
accept anything TcpStream::connect and UnixStream::connect accept and so
a single function taking config::Address can simplify both call sites.
  • Loading branch information
EliteTK committed Jun 4, 2024
1 parent acdf346 commit adde765
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 27 deletions.
13 changes: 5 additions & 8 deletions src/ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use anyhow::{bail, Context, Result};
use serde::de::{DeserializeOwned, IgnoredAny};
use tokio::io::BufReader;

use crate::config::{Address, Config};
use crate::config::Config;
use crate::lsp::ext::{self, LspMuxOptions, StatusResponse};
use crate::lsp::jsonrpc::{Message, Request, RequestId, Version};
use crate::lsp::transport::{LspReader, LspWriter};
Expand All @@ -15,13 +15,10 @@ pub async fn ext_request<T>(config: &Config, method: ext::Request) -> Result<T>
where
T: DeserializeOwned,
{
let (reader, writer) = match config.connect {
Address::Tcp(ip_addr, port) => Stream::connect_tcp((ip_addr, port)).await,
#[cfg(target_family = "unix")]
Address::Unix(ref path) => Stream::connect_unix(path).await,
}
.context("connect")?
.into_split();
let (reader, writer) = Stream::connect(&config.connect)
.await
.context("connect")?
.into_split();
let mut writer = LspWriter::new(writer, "lspmux");
let mut reader = LspReader::new(BufReader::new(reader), "lspmux");

Expand Down
9 changes: 2 additions & 7 deletions src/proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::env;
use anyhow::{bail, Context as _, Result};
use tokio::io::{self, BufStream};

use crate::config::{Address, Config};
use crate::config::Config;
use crate::lsp::ext::{LspMuxOptions, Request};
use crate::lsp::jsonrpc::Message;
use crate::lsp::transport::{LspReader, LspWriter};
Expand All @@ -23,12 +23,7 @@ pub async fn run(config: &Config, server: String, args: Vec<String>) -> Result<(
}
}

let mut stream = match config.connect {
Address::Tcp(ip_addr, port) => Stream::connect_tcp((ip_addr, port)).await,
#[cfg(target_family = "unix")]
Address::Unix(ref path) => Stream::connect_unix(path).await,
}
.context("connect")?;
let mut stream = Stream::connect(&config.connect).await.context("connect")?;
let mut stdio = BufStream::new(io::join(io::stdin(), io::stdout()));

// Wait for the client to send `initialize` request.
Expand Down
25 changes: 13 additions & 12 deletions src/socketwrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ use std::{io, net};

use pin_project_lite::pin_project;
use tokio::io::{AsyncRead, AsyncWrite, ReadBuf};
use tokio::net::{tcp, TcpListener, TcpStream, ToSocketAddrs};
use tokio::net::{tcp, TcpListener, TcpStream};
#[cfg(target_family = "unix")]
use tokio::net::{unix, UnixListener, UnixStream};

use crate::config::Address;

pub enum SocketAddr {
Ip(net::SocketAddr),
#[cfg(target_family = "unix")]
Expand Down Expand Up @@ -137,17 +139,16 @@ pin_project! {
}

impl Stream {
pub async fn connect_tcp<A: ToSocketAddrs>(addr: A) -> io::Result<Stream> {
Ok(Stream::Tcp {
tcp: TcpStream::connect(addr).await?,
})
}

#[cfg(target_family = "unix")]
pub async fn connect_unix<P: AsRef<Path>>(addr: P) -> io::Result<Stream> {
Ok(Stream::Unix {
unix: UnixStream::connect(addr).await?,
})
pub async fn connect(addr: &Address) -> io::Result<Stream> {
match addr {
Address::Tcp(ip_addr, port) => Ok(Stream::Tcp {
tcp: TcpStream::connect((*ip_addr, *port)).await?,
}),
#[cfg(target_family = "unix")]
Address::Unix(path) => Ok(Stream::Unix {
unix: UnixStream::connect(path).await?,
}),
}
}

pub fn into_split(self) -> (OwnedReadHalf, OwnedWriteHalf) {
Expand Down

0 comments on commit adde765

Please sign in to comment.