From 91a7db6c4857c4c1babebfe6eda35d174dc53052 Mon Sep 17 00:00:00 2001 From: famfo Date: Wed, 3 Nov 2021 16:50:23 +0100 Subject: [PATCH] Changed PING in from_str in Command --- Cargo.lock | 42 ------------------------------------------ Cargo.toml | 3 +-- src/lib.rs | 42 ++++++++++++++++++++++-------------------- 3 files changed, 23 insertions(+), 64 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 66f1507..c99bcc8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,48 +2,6 @@ # 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" diff --git a/Cargo.toml b/Cargo.toml index 37d1bf0..aad6b7b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,5 +6,4 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -regex = "1" -lazy_static = "1.4.0" + diff --git a/src/lib.rs b/src/lib.rs index 06cae05..347ccde 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,12 +1,6 @@ -use lazy_static::lazy_static; -use regex::Regex; +use std::borrow::Cow; use std::io::{Error, Read, Write}; use std::net::TcpStream; -use std::borrow::Cow; - -lazy_static! { - static ref PING_RE: Regex = Regex::new(r"^PING\s*:").unwrap(); -} pub struct Client { stream: TcpStream, @@ -22,23 +16,22 @@ pub struct Config { pub enum CapMode { LS, - END + END, } pub enum Command { PING(String), CAP(CapMode), USER(String, String, String, String), - NICK(String) + NICK(String), } impl Command { fn from_str(s: &str) -> Option { let new = s.trim(); - if new.contains("PING") { - let command = String::from(PING_RE.replace_all(new, "")); - + if new.starts_with("PING") { + let command: String = String::from(new.split_whitespace().collect::>()[1]); return Some(Self::PING(command)); } @@ -50,13 +43,22 @@ impl Client { pub fn new(host: &str, port: u16, config: Config) -> Result { let stream = TcpStream::connect(format!("{}:{}", host, port))?; - Ok(Self { stream, config, has_identified: false }) + Ok(Self { + stream, + config, + has_identified: false, + }) } pub fn try_identify(&mut self) -> Result<(), Error> { if !self.has_identified { self.write_command(Command::CAP(CapMode::LS))?; - self.write_command(Command::USER(self.config.username.clone(), "*".into(), "*".into(), self.config.username.clone()))?; + self.write_command(Command::USER( + self.config.username.clone(), + "*".into(), + "*".into(), + self.config.username.clone(), + ))?; self.write_command(Command::NICK(self.config.nickname.clone()))?; self.write_command(Command::CAP(CapMode::END))?; } @@ -74,7 +76,7 @@ impl Client { println!("error occured {}", e); return None; - }, + } }; Some(String::from_utf8_lossy(&buffer).into()) @@ -103,20 +105,20 @@ impl Client { use CapMode::*; Cow::Borrowed(match mode { LS => "CAP LS 302", - END => "CAP END" + END => "CAP END", }) as Cow - }, + } USER(username, s1, s2, realname) => { let formatted = format!("USER {} {} {} :{}", username, s1, s2, realname); Cow::Owned(formatted) as Cow - }, + } NICK(nickname) => { let formatted = format!("NICK {}", nickname); Cow::Owned(formatted) as Cow - }, - _ => Cow::Borrowed("") as Cow + } + _ => Cow::Borrowed("") as Cow, }; self.write(&computed)?;