46 lines
1.1 KiB
Rust
46 lines
1.1 KiB
Rust
use crate::commands::parser::Command;
|
|
|
|
use tokio::io::{AsyncBufReadExt, BufReader, Error, ReadHalf};
|
|
use tokio::net::TcpStream;
|
|
|
|
#[cfg(feature = "tls")]
|
|
use tokio_rustls::client::TlsStream;
|
|
|
|
pub struct Read {
|
|
#[cfg(feature = "tls")]
|
|
pub recv: BufReader<ReadHalf<TlsStream<TcpStream>>>,
|
|
#[cfg(not(feature = "tls"))]
|
|
pub recv: BufReader<ReadHalf<TcpStream>>,
|
|
}
|
|
|
|
impl Read {
|
|
#[must_use]
|
|
#[cfg(feature = "tls")]
|
|
pub fn new(recv: BufReader<ReadHalf<TlsStream<TcpStream>>>) -> Self {
|
|
Self { recv }
|
|
}
|
|
|
|
#[must_use]
|
|
#[cfg(not(feature = "tls"))]
|
|
pub fn new(recv: BufReader<ReadHalf<TcpStream>>) -> Self {
|
|
Self { recv }
|
|
}
|
|
|
|
/// # Errors
|
|
/// Returns errors from the ``TcpStream``.
|
|
pub async fn read(&mut self) -> Result<Option<Command>, Error> {
|
|
let mut buf = String::new();
|
|
let bytes = match self.recv.read_line(&mut buf).await {
|
|
Ok(b) => b,
|
|
Err(e) => return Err(e),
|
|
};
|
|
|
|
if bytes == 0 {
|
|
return Ok(None);
|
|
}
|
|
|
|
let command = Command::command_from_str(buf).await;
|
|
Ok(Some(command))
|
|
}
|
|
}
|