Comments | 0.1.1 | Stable

Small refactors and comments for clarity
This commit is contained in:
Max Chaev 2025-03-12 21:29:04 +03:00
parent 83d71ac2ac
commit 23b28abb8c
5 changed files with 32 additions and 12 deletions

View File

@ -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::<i32>().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));

View File

@ -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,

View File

@ -1,3 +1,5 @@
pub mod decoding;
pub mod cli;
pub mod networking;
pub mod networking;
// Assembling library from different files

View File

@ -1,8 +1,7 @@
use cctweaked_messager::cli::process_cli_args;
use cctweaked_messager::cli::basic_interface;
fn main() {
process_cli_args();
basic_interface();
}

View File

@ -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<String, StatusCode> {
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<Response
let client = Client::new();
let resp = client.post(url)
// A bit crazy stuff, but it works.
.body(serde_json::to_string(&message).expect("Failed to create json from message"))
.send();
resp