From ffbbabbddb170a160b2df8b6136e57e39f2f2cde Mon Sep 17 00:00:00 2001 From: Arthur Beck Date: Thu, 20 Mar 2025 12:34:54 +0000 Subject: [PATCH] Made VSCode use clippy and cleaned up code a bit --- .vscode/settings.json | 3 + src/main.rs | 278 +++++++++++++++++++++--------------------- 2 files changed, 142 insertions(+), 139 deletions(-) create mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..660eb93 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "rust-analyzer.check.command": "clippy" +} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 208f729..20f5697 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,12 +1,11 @@ #![feature(random)] -#![feature(inline_const_pat)] +use clap::Parser; use std::io::{ErrorKind, prelude::*}; use std::net::{TcpListener, TcpStream}; use std::random; use std::sync::{Arc, Mutex}; use std::thread::{self, JoinHandle, sleep}; -use clap::Parser; #[derive(Parser, Debug)] #[command(version, about, long_about = None)] @@ -18,19 +17,19 @@ struct Args { #[derive(Clone, PartialEq, Eq)] enum UserStatus { - ONLINE, - DISCONNECTED, - IDLE, - CUSTOM(String), + Online, + Disconnected, + Idle, + Custom(String), } impl UserStatus { fn get_status_string(&self) -> String { match self { - UserStatus::ONLINE => "\x1b[0m\x1b[32monline\x1b[0m".to_string(), - UserStatus::IDLE => "\x1b[0m\x1b[33midle\x1b[0m".to_string(), - UserStatus::DISCONNECTED => "\x1b[0m\x1b[31mdisconnected\x1b[0m".to_string(), - UserStatus::CUSTOM(val) => format!("\x1b[0m\x1b[34m{}\x1b[0m", val), + UserStatus::Online => "\x1b[0m\x1b[32monline\x1b[0m".to_string(), + UserStatus::Idle => "\x1b[0m\x1b[33midle\x1b[0m".to_string(), + UserStatus::Disconnected => "\x1b[0m\x1b[31mdisconnected\x1b[0m".to_string(), + UserStatus::Custom(val) => format!("\x1b[0m\x1b[34m{}\x1b[0m", val), } } } @@ -97,7 +96,7 @@ fn handle_client(mut stream: TcpStream, channel: Channel) { } for user in &channel.lock().unwrap().online_users { if user.name == new_name { - if user.status == UserStatus::DISCONNECTED { + if user.status == UserStatus::Disconnected { stream .write_all(b"\x1b[0m\x1b[33mEnter your login token(up to 50 characters):\x1b[0m\x1b[4m\n") .unwrap(); @@ -155,7 +154,7 @@ fn handle_client(mut stream: TcpStream, channel: Channel) { let mut user = User { name: String::from_utf8_lossy(&(buf[..i])).to_string(), - status: UserStatus::ONLINE, + status: UserStatus::Online, token: format!("{:x}", random::random::()), }; @@ -168,7 +167,7 @@ fn handle_client(mut stream: TcpStream, channel: Channel) { let netbot = User { name: "Netbot".to_owned(), - status: UserStatus::ONLINE, + status: UserStatus::Online, token: "".to_owned(), }; @@ -198,36 +197,39 @@ fn handle_client(mut stream: TcpStream, channel: Channel) { recieve_thread = thread::spawn(move || { let mut current_message_count = channel.lock().unwrap().messages.len(); loop { - if let Ok(_) = recieve_terminate_recv.try_recv() { + if recieve_terminate_recv.try_recv().is_ok() { stream.shutdown(std::net::Shutdown::Both).unwrap(); panic!("recieve thread exiting"); } - let new_message_count = channel.lock().unwrap().messages.len(); - if current_message_count != new_message_count { - let new_messages = &channel.lock().unwrap().messages[current_message_count..]; - for msg in new_messages { - if !msg.me_message { - stream - .write_fmt(format_args!( - "\x1b[0m\x1b[34m[{}]\x1b[0m \x1b[1m{}\x1b[0m \x1b[31m⇛\x1b[0m \x1b[35m{}\x1b[0m\n", - msg.timestamp.format(&time_format).unwrap(), - msg.sender.name, - msg.contents - )) - .unwrap(); - } else { - stream - .write_fmt(format_args!( - "\x1b[0m\x1b[34m[{}]\x1b[0m \x1b[1;3m{}\x1b[0m \x1b[3m{}\x1b[0m\n", - msg.timestamp.format(&time_format).unwrap(), - msg.sender.name, - msg.contents - )) - .unwrap(); + { + let lock = channel.lock().unwrap(); + let new_message_count = lock.messages.len(); + if current_message_count != new_message_count { + let new_messages = &lock.messages[current_message_count..]; + for msg in new_messages { + if !msg.me_message { + stream + .write_fmt(format_args!( + "\x1b[0m\x1b[34m[{}]\x1b[0m \x1b[1m{}\x1b[0m \x1b[31m⇛\x1b[0m \x1b[35m{}\x1b[0m\n", + msg.timestamp.format(&time_format).unwrap(), + msg.sender.name, + msg.contents + )) + .unwrap(); + } else { + stream + .write_fmt(format_args!( + "\x1b[0m\x1b[34m[{}]\x1b[0m \x1b[1;3m{}\x1b[0m \x1b[3m{}\x1b[0m\n", + msg.timestamp.format(&time_format).unwrap(), + msg.sender.name, + msg.contents + )) + .unwrap(); + } } - } - current_message_count = new_message_count; + current_message_count = new_message_count; + } } // so that not all the threads will sync at once @@ -250,7 +252,7 @@ fn handle_client(mut stream: TcpStream, channel: Channel) { loop { if let Err(err) = stream.read_exact(&mut buf1) { if err.kind() == ErrorKind::UnexpectedEof { - user.status = UserStatus::DISCONNECTED; + user.status = UserStatus::Disconnected; for usr in &mut channel.lock().unwrap().online_users { if usr.name == user.name { usr.status = user.status.clone(); @@ -275,7 +277,7 @@ fn handle_client(mut stream: TcpStream, channel: Channel) { buf.pop(); } - if buf1[0] == b'\n' && buf.len() > 0 { + if buf1[0] == b'\n' && !buf.is_empty() { let contents = String::from_utf8_lossy(&buf).to_string(); if !contents.starts_with("/") { channel.lock().unwrap().messages.push(Message { @@ -284,11 +286,10 @@ fn handle_client(mut stream: TcpStream, channel: Channel) { timestamp: time::OffsetDateTime::now_local().unwrap(), me_message: false, }); - } else { - if contents.split(" ").collect::>()[0] == "/help" - || contents.split(" ").collect::>()[0] == "/?" - { - stream + } else if contents.split(" ").collect::>()[0] == "/help" + || contents.split(" ").collect::>()[0] == "/?" + { + stream .write_all( b"\x1b[0m\x1b[32mValid commands:\n \ /help, /?: Print this help.\n \ @@ -299,120 +300,119 @@ fn handle_client(mut stream: TcpStream, channel: Channel) { /me x: Say that you are doing x(for example, \"/me purrs\" as netbot would result in the output of \"netbot purrs\").\x1b[0m\n", ) .unwrap(); - } else if contents.split(" ").collect::>()[0] == "/me" { - if contents.split(" ").collect::>().len() < 2 - || contents.split(" ").collect::>()[1] == "" - { - stream + } else if contents.split(" ").collect::>()[0] == "/me" { + if contents.split(" ").collect::>().len() < 2 + || contents.split(" ").collect::>()[1].is_empty() + { + stream .write_fmt(format_args!( "\x1b[0m\x1b[31mYou need to provide at least one argument to /me!\n\x1b[0m", )) .unwrap(); - } else { - channel.lock().unwrap().messages.push(Message { - sender: user.clone(), - contents: contents.replace("/me ", ""), - timestamp: time::OffsetDateTime::now_local().unwrap(), - me_message: true, - }); - } - } else if contents.split(" ").collect::>()[0] == "/status" - || contents == "/user" - { - let contents = contents.split(" ").collect::>(); - if contents.len() == 1 { - stream - .write_fmt(format_args!( - "\x1b[0m\x1b[32mYou are currently marked as {}\n\x1b[0m", - user.clone().status.get_status_string() - )) - .unwrap(); - } else { - match contents[1] { - "online" => { - user.status = UserStatus::ONLINE; - } - "idle" => { - user.status = UserStatus::IDLE; - } - "disconnected" => { - user.status = UserStatus::DISCONNECTED; - } - other => { - user.status = UserStatus::CUSTOM(other.to_owned()); - } + } else { + channel.lock().unwrap().messages.push(Message { + sender: user.clone(), + contents: contents.replace("/me ", ""), + timestamp: time::OffsetDateTime::now_local().unwrap(), + me_message: true, + }); + } + } else if contents.split(" ").collect::>()[0] == "/status" + || contents == "/user" + { + let contents = contents.split(" ").collect::>(); + if contents.len() == 1 { + stream + .write_fmt(format_args!( + "\x1b[0m\x1b[32mYou are currently marked as {}\n\x1b[0m", + user.clone().status.get_status_string() + )) + .unwrap(); + } else { + match contents[1] { + "online" => { + user.status = UserStatus::Online; } - for usr in &mut channel.lock().unwrap().online_users { - if usr.name == user.name { - usr.status = user.status.clone(); - } + "idle" => { + user.status = UserStatus::Idle; + } + "disconnected" => { + user.status = UserStatus::Disconnected; + } + other => { + user.status = UserStatus::Custom(other.to_owned()); } - stream - .write_fmt(format_args!( - "\x1b[0m\x1b[32mYou are now marked as {}\n\x1b[0m", - user.status.get_status_string() - )) - .unwrap(); } - } else if contents.split(" ").collect::>()[0] == "/server" { - let contents = contents.split(" ").collect::>(); - if contents.len() == 1 { - stream - .write_fmt(format_args!( - "\x1b[0m\x1b[32mCurrent server name: {}.\n\x1b[0m", - channel.lock().unwrap().name - )) - .unwrap(); - } else { - channel.lock().unwrap().name = contents[1].to_owned(); - stream - .write_fmt(format_args!( - "\x1b[0m\x1b[32mNew server name: {}.\n\x1b[0m", - channel.lock().unwrap().name - )) - .unwrap(); - } - } else if contents.split(" ").collect::>()[0] == "/user" { - let contents = contents.split(" ").collect::>(); - let mut found_user = false; for usr in &mut channel.lock().unwrap().online_users { - if usr.name == contents[1] { - stream - .write_fmt(format_args!( - "\x1b[0m\x1b[32mUser {} is currently {}.\n\x1b[0m", - usr.name, - usr.status.get_status_string() - )) - .unwrap(); - found_user = true; - break; + if usr.name == user.name { + usr.status = user.status.clone(); } } - if !found_user { + stream + .write_fmt(format_args!( + "\x1b[0m\x1b[32mYou are now marked as {}\n\x1b[0m", + user.status.get_status_string() + )) + .unwrap(); + } + } else if contents.split(" ").collect::>()[0] == "/server" { + let contents = contents.split(" ").collect::>(); + if contents.len() == 1 { + stream + .write_fmt(format_args!( + "\x1b[0m\x1b[32mCurrent server name: {}.\n\x1b[0m", + channel.lock().unwrap().name + )) + .unwrap(); + } else { + channel.lock().unwrap().name = contents[1].to_owned(); + stream + .write_fmt(format_args!( + "\x1b[0m\x1b[32mNew server name: {}.\n\x1b[0m", + channel.lock().unwrap().name + )) + .unwrap(); + } + } else if contents.split(" ").collect::>()[0] == "/user" { + let contents = contents.split(" ").collect::>(); + let mut found_user = false; + for usr in &mut channel.lock().unwrap().online_users { + if usr.name == contents[1] { stream .write_fmt(format_args!( - "\x1b[0m\x1b[32mUnknown user \"{}\".\n\x1b[0m", - contents[1] - )) - .unwrap(); - } - } else if contents.starts_with("/users") { - stream.write_all(b"\x1b[0m\x1b[32mUsers:\x1b[0m").unwrap(); - for usr in &channel.lock().unwrap().online_users { - stream - .write_fmt(format_args!( - "\n{} (currently {})", + "\x1b[0m\x1b[32mUser {} is currently {}.\n\x1b[0m", usr.name, usr.status.get_status_string() )) .unwrap(); + found_user = true; + break; } - stream.write_all(b"\n\x1b[0m").unwrap(); - } else { + } + if !found_user { stream - .write_all(b"\x1b[0m\x1b[31;1mThat's an invalid command!\n\x1b[0m") + .write_fmt(format_args!( + "\x1b[0m\x1b[32mUnknown user \"{}\".\n\x1b[0m", + contents[1] + )) .unwrap(); } + } else if contents.starts_with("/users") { + stream.write_all(b"\x1b[0m\x1b[32mUsers:\x1b[0m").unwrap(); + for usr in &channel.lock().unwrap().online_users { + stream + .write_fmt(format_args!( + "\n{} (currently {})", + usr.name, + usr.status.get_status_string() + )) + .unwrap(); + } + stream.write_all(b"\n\x1b[0m").unwrap(); + } else { + stream + .write_all(b"\x1b[0m\x1b[31;1mThat's an invalid command!\n\x1b[0m") + .unwrap(); } buf.clear(); }