From 0cc707def98ff45b9d54cde010456df007b92834 Mon Sep 17 00:00:00 2001 From: Braden Godley Date: Sat, 11 May 2024 11:49:17 -0700 Subject: [PATCH] automatically put version in packets --- src/client.rs | 9 ++++----- src/main.rs | 2 +- src/packetreader.rs | 20 ++++++++++++-------- 3 files changed, 17 insertions(+), 14 deletions(-) diff --git a/src/client.rs b/src/client.rs index bdef621..04c81c6 100644 --- a/src/client.rs +++ b/src/client.rs @@ -1,8 +1,8 @@ use std::net::TcpStream; use std::io::{Read, Write, self}; +use clap::crate_version; pub struct Client { - version: String, stream: TcpStream, } @@ -12,15 +12,13 @@ enum EchoType { } impl Client { - pub fn new(host: &str, port: u16, version: &str) -> Self { + pub fn new(host: &str, port: u16) -> Self { let connection_url = format!("{}:{}", host, port); let stream = TcpStream::connect(&connection_url) .expect(&format!("Failed to connect to {}", &connection_url)); - let version = version.to_string(); Client { stream, - version, } } @@ -103,7 +101,8 @@ impl Client { } pub fn send(&mut self, command: &str, lines: Vec<&str>) -> io::Result<()> { - let version_line = format!("Rustis {}", self.version); + let version = crate_version!(); + let version_line = format!("Rustis {}", version); let message = format!("{}\n{}\n{}\n\n", version_line, command, lines.join("\n")); let bytes = message.as_bytes(); diff --git a/src/main.rs b/src/main.rs index 8631136..d122470 100644 --- a/src/main.rs +++ b/src/main.rs @@ -76,7 +76,7 @@ fn main() -> io::Result<()> { let host = matches.get_one::("host").unwrap(); let port = matches.get_one::("port").unwrap(); - let mut client = client::Client::new(host, *port, "1.0.0"); + let mut client = client::Client::new(host, *port); if let Some(matches) = matches.subcommand_matches("publish") { let channel = matches.get_one::("channel").unwrap(); diff --git a/src/packetreader.rs b/src/packetreader.rs index 8554ebb..3f2655d 100644 --- a/src/packetreader.rs +++ b/src/packetreader.rs @@ -1,6 +1,5 @@ use regex::Regex; - -const VERSION: &'static str = "1.0.0"; +use clap::crate_version; /* Packet format: @@ -49,7 +48,7 @@ impl RequestPacket { pub fn new(buf: String) -> Self { let lines: Vec<_> = buf.split("\n").collect(); - let version = if let Some(line1) = lines.get(0) { + let packet_version = if let Some(line1) = lines.get(0) { let re = Regex::new(r"^Rustis (\d{1,3}\.\d{1,4}\.\d{1,4})$").unwrap(); if let Some(caps) = re.captures(line1) { @@ -61,8 +60,10 @@ impl RequestPacket { None }; - if let Some(version) = version { - if version != VERSION { + let current_version = crate_version!(); + + if let Some(packet_version) = packet_version { + if packet_version != current_version { return RequestPacket::Invalid { error: String::from("version mismatch"), } @@ -189,7 +190,8 @@ mod test { #[test] fn publish_packet() { let mut buf = String::new(); - buf.push_str("Rustis 1.0.0\n"); + let version = crate_version!(); + buf.push_str(&format!("Rustis {}\n", version)); buf.push_str("publish\n"); buf.push_str("channel\n"); buf.push_str("message"); @@ -205,7 +207,8 @@ mod test { #[test] fn invalid_version() { let mut buf = String::new(); - buf.push_str("Rustis 99.99.99\n"); + let version = "999.999.999"; + buf.push_str(&format!("Rustis {}\n", version)); buf.push_str("publish\n"); buf.push_str("channel\n"); buf.push_str("message\n"); @@ -221,7 +224,8 @@ mod test { #[test] fn unknown_command() { let mut buf = String::new(); - buf.push_str("Rustis 1.0.0\n"); + let version = crate_version!(); + buf.push_str(&format!("Rustis {}\n", version)); buf.push_str("heebee\n"); let packet = RequestPacket::new(buf);