From 0adb6b994d26ae8230ab37121e8690b633148a35 Mon Sep 17 00:00:00 2001
From: Kubat <mael.martin31@gmail.com>
Date: Tue, 14 Dec 2021 19:14:38 +0100
Subject: [PATCH] Really create commands and pack them into a package

---
 src/cmd/mod.rs | 41 ++++++++++++++++++++++++++++++++++++-----
 src/matrix.rs  | 17 ++++++++++++++---
 2 files changed, 50 insertions(+), 8 deletions(-)

diff --git a/src/cmd/mod.rs b/src/cmd/mod.rs
index 0db744d..c818e49 100644
--- a/src/cmd/mod.rs
+++ b/src/cmd/mod.rs
@@ -5,10 +5,10 @@ use log::error;
 use std::{process::abort, string::String};
 
 #[allow(dead_code)]
-pub enum CmdHandler {
-    Simple(fn(Vec<String>) -> bool),
-    Echo(fn(Vec<String>) -> String),
-    MultiEcho(fn(Vec<String>) -> Vec<String>),
+enum CmdHandler {
+    Simple(Box<dyn Fn(Vec<String>) -> bool>),
+    Echo(Box<dyn Fn(Vec<String>) -> String>),
+    MultiEcho(Box<dyn Fn(Vec<String>) -> Vec<String>>),
 }
 
 #[allow(dead_code)]
@@ -31,7 +31,7 @@ impl Cmd {
             abort();
         }
 
-        match self.handler {
+        match &self.handler {
             CmdHandler::Simple(f) => match f(args) {
                 true => "<b>Ok</b>".to_string(),
                 false => "<b>Failed!</b>".to_string(),
@@ -45,4 +45,35 @@ impl Cmd {
     pub fn get_name(&self) -> &str {
         &self.name
     }
+
+    #[allow(dead_code)]
+    pub fn new_simple(name: String, argc: u32, f: Box<dyn Fn(Vec<String>) -> bool>) -> Cmd {
+        Cmd {
+            name: name,
+            argc: argc,
+            handler: CmdHandler::Simple(f),
+        }
+    }
+
+    #[allow(dead_code)]
+    pub fn new_echo(name: String, argc: u32, f: Box<dyn Fn(Vec<String>) -> String>) -> Cmd {
+        Cmd {
+            name: name,
+            argc: argc,
+            handler: CmdHandler::Echo(f),
+        }
+    }
+
+    #[allow(dead_code)]
+    pub fn new_multi_echo(
+        name: String,
+        argc: u32,
+        f: Box<dyn Fn(Vec<String>) -> Vec<String>>,
+    ) -> Cmd {
+        Cmd {
+            name: name,
+            argc: argc,
+            handler: CmdHandler::MultiEcho(f),
+        }
+    }
 }
diff --git a/src/matrix.rs b/src/matrix.rs
index 2b2e4aa..371832b 100644
--- a/src/matrix.rs
+++ b/src/matrix.rs
@@ -1,4 +1,4 @@
-use crate::{config::Config, cmd::package::CmdPackage, cmd::{ Cmd, CmdHandler}};
+use crate::{cmd::package::CmdPackage, cmd::Cmd, config::Config};
 use futures::future::join_all;
 use log::{error, info, warn};
 use matrix_sdk::{
@@ -16,7 +16,7 @@ use matrix_sdk::{
     },
     Client, Result, SyncSettings,
 };
-use std::{convert::TryFrom,process::abort};
+use std::{convert::TryFrom, process::abort};
 use tokio::time::{sleep, Duration};
 
 async fn join_room(room: &Invited) {
@@ -129,8 +129,19 @@ async fn startup_and_log_rooms(client: &Client) {
 
 pub async fn connect_and_handle(config: Config) -> Result<()> {
     // TODO: Refactor
+    let username_copy = config.user_name.clone();
     let _pkg = match config.section("basic") {
-        Some(section) => CmdPackage::from_config(&section, vec![]),
+        Some(section) => CmdPackage::from_config(
+            &section,
+            vec![
+                Cmd::new_simple(format!("ping"), 0, Box::new(|_x| true)),
+                Cmd::new_echo(
+                    format!("bot_name"),
+                    0,
+                    Box::new(move |_x| username_copy.clone()),
+                ),
+            ],
+        ),
         None => {
             abort();
         }
-- 
GitLab