From 83d71ac2ac7525f0d46190273c957bcd845724f6 Mon Sep 17 00:00:00 2001 From: Max Chaev Date: Wed, 12 Mar 2025 21:15:11 +0300 Subject: [PATCH] Cli reworks | 0.1.1 | Stable Seperated json creation from cli.rs and added better help message. Also user-friendly message creation --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/cli.rs | 22 ++++++++++++++++------ src/networking.rs | 6 ++++-- 4 files changed, 22 insertions(+), 10 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6ec3f42..7cb4163 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -79,7 +79,7 @@ dependencies = [ [[package]] name = "cctweaked-messager" -version = "0.1.0" +version = "0.1.1" dependencies = [ "reqwest", "serde", diff --git a/Cargo.toml b/Cargo.toml index c60a7d5..8bb6945 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "cctweaked-messager" -version = "0.1.0" +version = "0.1.1" edition = "2024" [dependencies] diff --git a/src/cli.rs b/src/cli.rs index 759bf27..9d82f1c 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -2,7 +2,7 @@ use std::{env, process::exit}; use strum::{EnumIter, IntoEnumIterator}; -use crate::{decoding::{decode_json, CctweakedMessage}, networking::{get_json, send_json}}; +use crate::{decoding::decode_json, networking::{get_json, send_json}}; #[derive(Debug, EnumIter)] enum KnownArguments { @@ -28,7 +28,7 @@ pub fn process_cli_args() { // Removing execution path probably. let args = &args[1..]; - let arg_type = match args[0].as_str() { + let arg_type = match args[0].to_lowercase().as_str() { "help" => KnownArguments::Help, "--help" => KnownArguments::Help, "-h" => KnownArguments::Help, @@ -48,14 +48,24 @@ pub fn process_cli_args() { fn process_help(_: &[String]) { println!("All posible arguments:"); for argument in KnownArguments::iter() { - println!("{:?}", argument); + match argument { + KnownArguments::Help => println!("{:?} - Shows help message.", argument), + KnownArguments::Send => println!("{:?} - sends a cctweaked message to server", argument), + KnownArguments::Recieve => println!("{:?} - Fetches cctweaked message from server", argument), + _ => (), + } } } fn process_send(args: &[String]) { - let inputed_json: CctweakedMessage = serde_json::from_str(&args[1]) - .expect("Wrong CCTweaked Message. Try to encapsulate it in '' "); + if args.len() < 3 { + println!("Not enough arguments for 'send', please refer to --help"); + exit(0); + } - let resp = send_json(URL.to_owned(), inputed_json) + let input_content = &args[1]; + let input_type = &args[2].parse::().expect("Wrong message type"); + + let resp = send_json(URL.to_owned(), input_content.to_owned(), *input_type) .expect("Failed to send json"); println!("{}, {}", resp.status(), resp.text().unwrap()); diff --git a/src/networking.rs b/src/networking.rs index 621beb5..2c0f696 100644 --- a/src/networking.rs +++ b/src/networking.rs @@ -1,6 +1,6 @@ use reqwest::{blocking::{Client, Response}, Error, StatusCode}; -use crate::decoding::CctweakedMessage; +use crate::decoding::create_cctweaked_message; pub fn get_json(url: String) -> Result { let resp = reqwest::blocking::get(url) @@ -14,7 +14,9 @@ pub fn get_json(url: String) -> Result { } } -pub fn send_json(url: String, message: CctweakedMessage) -> Result { +pub fn send_json(url: String, content: String, msg_type: i32) -> Result { + let message = create_cctweaked_message(content, msg_type); + let client = Client::new(); let resp = client.post(url) .body(serde_json::to_string(&message).expect("Failed to create json from message"))