Add support for reading from the socket
This commit is contained in:
parent
41bb4b9ae7
commit
9f878dd578
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -1,2 +1 @@
|
||||||
/target
|
/target
|
||||||
Cargo.lock
|
|
||||||
|
|
49
Cargo.lock
generated
Normal file
49
Cargo.lock
generated
Normal file
|
@ -0,0 +1,49 @@
|
||||||
|
# This file is automatically @generated by Cargo.
|
||||||
|
# It is not intended for manual editing.
|
||||||
|
version = 3
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "aho-corasick"
|
||||||
|
version = "0.7.18"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "1e37cfd5e7657ada45f742d6e99ca5788580b5c529dc78faf11ece6dc702656f"
|
||||||
|
dependencies = [
|
||||||
|
"memchr",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "irsc"
|
||||||
|
version = "0.1.0"
|
||||||
|
dependencies = [
|
||||||
|
"lazy_static",
|
||||||
|
"regex",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "lazy_static"
|
||||||
|
version = "1.4.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "memchr"
|
||||||
|
version = "2.4.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "308cc39be01b73d0d18f82a0e7b2a3df85245f84af96fdddc5d202d27e47b86a"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "regex"
|
||||||
|
version = "1.5.4"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "d07a8629359eb56f1e2fb1652bb04212c072a87ba68546a04065d525673ac461"
|
||||||
|
dependencies = [
|
||||||
|
"aho-corasick",
|
||||||
|
"memchr",
|
||||||
|
"regex-syntax",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "regex-syntax"
|
||||||
|
version = "0.6.25"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "f497285884f3fcff424ffc933e56d7cbca511def0c9831a7f9b5f6153e3cc89b"
|
|
@ -6,3 +6,5 @@ edition = "2021"
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
regex = "1"
|
||||||
|
lazy_static = "1.4.0"
|
||||||
|
|
80
src/lib.rs
80
src/lib.rs
|
@ -1,14 +1,84 @@
|
||||||
use std::net::TcpStream;
|
use std::net::TcpStream;
|
||||||
use std::io::Error;
|
use std::io::{Error, Read, Write};
|
||||||
|
use std::time::Duration;
|
||||||
|
use lazy_static::lazy_static;
|
||||||
|
use regex::Regex;
|
||||||
|
|
||||||
|
lazy_static! {
|
||||||
|
static ref PING_RE: Regex = Regex::new(r"^PING\s*:").unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
pub struct Client {
|
pub struct Client {
|
||||||
stream: TcpStream
|
stream: TcpStream,
|
||||||
|
config: Config
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct Config {
|
||||||
|
username: String,
|
||||||
|
nickname: String,
|
||||||
|
mode: String
|
||||||
|
}
|
||||||
|
|
||||||
|
pub enum Command {
|
||||||
|
PING(String)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
impl Command {
|
||||||
|
fn from_str(s: &str) -> Option<Self> {
|
||||||
|
let new = s.trim();
|
||||||
|
|
||||||
|
if new.contains("PING") {
|
||||||
|
let command = String::from(PING_RE.replace_all(new, ""));
|
||||||
|
|
||||||
|
return Some(Self::PING(command));
|
||||||
|
}
|
||||||
|
|
||||||
|
None
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Client {
|
impl Client {
|
||||||
pub fn new(host: &str, port: &str) -> Result<Self, Error> {
|
pub fn new(host: &str, port: u16, config: Config) -> Result<Self, Error> {
|
||||||
let mut stream = TcpStream::connect(format!("{}:{}", host, port))?;
|
let stream = TcpStream::connect(format!("{}:{}", host, port))?;
|
||||||
|
|
||||||
Ok(Self { stream })
|
Ok(Self { stream, config })
|
||||||
|
}
|
||||||
|
|
||||||
|
// temporarily pub, change this later
|
||||||
|
pub fn read_string(&mut self) -> Option<String> {
|
||||||
|
let mut buffer = [0u8; 512];
|
||||||
|
|
||||||
|
match self.stream.read(&mut buffer) {
|
||||||
|
Ok(_) => {},
|
||||||
|
Err(_) => return None
|
||||||
|
};
|
||||||
|
|
||||||
|
Some(String::from_utf8_lossy(&buffer).into())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn read(&mut self) -> Option<Command> {
|
||||||
|
if let Some(string) = self.read_string() {
|
||||||
|
return Command::from_str(&string);
|
||||||
|
}
|
||||||
|
|
||||||
|
None // if it's none, there's no new messages
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn write(&mut self, data: &str) -> Result<(), Error> {
|
||||||
|
self.stream.write_all(data.as_bytes())?;
|
||||||
|
// self.stream.flush()?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Config {
|
||||||
|
pub fn new(username: &str, nickname: Option<&str>, mode: Option<&str>) -> Self {
|
||||||
|
Self {
|
||||||
|
username: username.into(),
|
||||||
|
nickname: nickname.unwrap_or(username).into(),
|
||||||
|
mode: mode.unwrap_or("").into()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in a new issue