diff --git a/src/cli.rs b/src/cli.rs index 9d82f1c..89b9005 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -1,9 +1,14 @@ +// Package ships with built-in crate for cli managment of server +// It's possible to create other interfaces with use of other crates inside package +// This is just an example and for fast usages. + use std::{env, process::exit}; use strum::{EnumIter, IntoEnumIterator}; use crate::{decoding::decode_json, networking::{get_json, send_json}}; +// Command which we use for basic processing #[derive(Debug, EnumIter)] enum KnownArguments { Help, @@ -12,22 +17,25 @@ enum KnownArguments { Wrong, } +// RESTfull endpoint. const URL: &str = "https://dixxe.top/cctweaked-beta"; -fn gratefull_exit() { - println!("Please provide correct arguments or look at --help"); +// Method to disable program in non-panic way :) +fn gracefull_exit(msg: &str) { + println!("{msg}"); exit(0) } -pub fn process_cli_args() { +pub fn basic_interface() { let args: Vec<_> = env::args().collect(); if args.len() < 2 { - gratefull_exit(); + gracefull_exit("Please provide correct arguments or look at --help"); } // Removing execution path probably. let args = &args[1..]; + // Translate input args to enum value... let arg_type = match args[0].to_lowercase().as_str() { "help" => KnownArguments::Help, "--help" => KnownArguments::Help, @@ -37,16 +45,18 @@ pub fn process_cli_args() { _ => KnownArguments::Wrong, }; + // Match for every enum value specific processing method! match arg_type { KnownArguments::Help => process_help(args), KnownArguments::Send => process_send(args), KnownArguments::Recieve => process_recieve(args), - KnownArguments::Wrong => gratefull_exit(), + KnownArguments::Wrong => gracefull_exit("Please provide correct arguments or look at --help"), }; } fn process_help(_: &[String]) { println!("All posible arguments:"); + // Printing fancy compile-time checked help message. for argument in KnownArguments::iter() { match argument { KnownArguments::Help => println!("{:?} - Shows help message.", argument), @@ -56,12 +66,13 @@ fn process_help(_: &[String]) { } } } + fn process_send(args: &[String]) { if args.len() < 3 { - println!("Not enough arguments for 'send', please refer to --help"); - exit(0); + gracefull_exit("Not enough arguments for 'send', please refer to --help"); } + // Variables that will be used to assemble decoding::CctweakedMessage let input_content = &args[1]; let input_type = &args[2].parse::().expect("Wrong message type"); @@ -71,10 +82,11 @@ fn process_send(args: &[String]) { println!("{}, {}", resp.status(), resp.text().unwrap()); } + fn process_recieve(_: &[String]) { let resp = get_json(URL.to_owned()) - .expect("Expected a value, got error."); + .expect("Expected a value, got error."); println!("{:#?}", decode_json(resp)); diff --git a/src/decoding.rs b/src/decoding.rs index d6c2789..ec4c6e4 100644 --- a/src/decoding.rs +++ b/src/decoding.rs @@ -1,11 +1,13 @@ use serde::{Deserialize, Serialize}; +// Message that used to communicate with server. #[derive(Serialize, Deserialize, Debug)] pub struct CctweakedMessage { content: String, msg_type: i32 } +// Simple builder for that message pub fn create_cctweaked_message(content: String, msg_type: i32) -> CctweakedMessage { CctweakedMessage { content, diff --git a/src/lib.rs b/src/lib.rs index 1e85fdc..0b2c5e9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,5 @@ pub mod decoding; pub mod cli; -pub mod networking; \ No newline at end of file +pub mod networking; + +// Assembling library from different files \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 1c7e93c..7ff5c16 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,8 +1,7 @@ -use cctweaked_messager::cli::process_cli_args; - +use cctweaked_messager::cli::basic_interface; fn main() { - process_cli_args(); + basic_interface(); } diff --git a/src/networking.rs b/src/networking.rs index 2c0f696..8aeb919 100644 --- a/src/networking.rs +++ b/src/networking.rs @@ -1,3 +1,6 @@ +// Code that handles server data readings. Pretty simple +// Just a GET json and POST json functions + use reqwest::{blocking::{Client, Response}, Error, StatusCode}; use crate::decoding::create_cctweaked_message; @@ -9,6 +12,7 @@ pub fn get_json(url: String) -> Result { let status = resp.status(); match resp.text() { + // Replacing `\` for desirialization to work Ok(body) => Ok(String::from(body.trim().replace("\\", ""))), _ => Err(status) } @@ -19,6 +23,7 @@ pub fn send_json(url: String, content: String, msg_type: i32) -> Result