Skip to content
Extraits de code Groupes Projets
Vérifiée Valider 8974cb27 rédigé par Kubat's avatar Kubat
Parcourir les fichiers

Refactor: split the connect_and_handle function

parent 2ad38687
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
...@@ -4,16 +4,18 @@ pub mod acl; ...@@ -4,16 +4,18 @@ pub mod acl;
pub mod package; pub mod package;
use log::error; use log::error;
use std::{process::abort, string::String, sync::Arc}; use std::{boxed::Box, process::abort, string::String};
trait CmdFnSimple = Fn(Vec<&str>) -> bool + Send + Sync;
trait CmdFnEcho = Fn(Vec<&str>) -> String + Send + Sync;
trait CmdFnMultiEcho = Fn(Vec<&str>) -> Vec<String> + Send + Sync;
#[derive(Clone)]
enum CmdHandler { enum CmdHandler {
Simple(Arc<dyn Fn(Vec<&str>) -> bool + Send + Sync>), Simple(Box<dyn CmdFnSimple>),
Echo(Arc<dyn Fn(Vec<&str>) -> String + Send + Sync>), Echo(Box<dyn CmdFnEcho>),
MultiEcho(Arc<dyn Fn(Vec<&str>) -> Vec<String> + Send + Sync>), MultiEcho(Box<dyn CmdFnMultiEcho>),
} }
#[derive(Clone)]
pub struct Cmd { pub struct Cmd {
name: String, name: String,
argc: u32, argc: u32,
...@@ -45,39 +47,36 @@ impl Cmd { ...@@ -45,39 +47,36 @@ impl Cmd {
&self.name &self.name
} }
pub fn new_simple( pub fn new_simple<F: 'static>(name: String, argc: u32, f: F) -> Cmd
name: String, where
argc: u32, F: Fn(Vec<&str>) -> bool + Send + Sync,
f: Arc<dyn Fn(Vec<&str>) -> bool + Send + Sync>, {
) -> Cmd {
Cmd { Cmd {
name: name, name: name,
argc: argc, argc: argc,
handler: CmdHandler::Simple(f), handler: CmdHandler::Simple(Box::new(f)),
} }
} }
pub fn new_echo( pub fn new_echo<F: 'static>(name: String, argc: u32, f: F) -> Cmd
name: String, where
argc: u32, F: Fn(Vec<&str>) -> String + Send + Sync,
f: Arc<dyn Fn(Vec<&str>) -> String + Send + Sync>, {
) -> Cmd {
Cmd { Cmd {
name: name, name: name,
argc: argc, argc: argc,
handler: CmdHandler::Echo(f), handler: CmdHandler::Echo(Box::new(f)),
} }
} }
pub fn new_multi_echo( pub fn new_multi_echo<F: 'static>(name: String, argc: u32, f: F) -> Cmd
name: String, where
argc: u32, F: Fn(Vec<&str>) -> Vec<String> + Send + Sync,
f: Arc<dyn Fn(Vec<&str>) -> Vec<String> + Send + Sync>, {
) -> Cmd {
Cmd { Cmd {
name: name, name: name,
argc: argc, argc: argc,
handler: CmdHandler::MultiEcho(f), handler: CmdHandler::MultiEcho(Box::new(f)),
} }
} }
} }
...@@ -6,7 +6,6 @@ use ini::Properties as IniSection; ...@@ -6,7 +6,6 @@ use ini::Properties as IniSection;
use matrix_sdk::{room::Joined as JoinedRoom, ruma::UserId}; use matrix_sdk::{room::Joined as JoinedRoom, ruma::UserId};
use std::{convert::TryFrom, fmt}; use std::{convert::TryFrom, fmt};
#[derive(Clone)]
pub struct CmdPackage { pub struct CmdPackage {
name: String, name: String,
acl: CmdAcl, acl: CmdAcl,
......
#![feature(new_uninit, once_cell)] #![feature(new_uninit, once_cell, trait_alias)]
mod cmd; mod cmd;
mod config; mod config;
...@@ -20,7 +20,8 @@ async fn main() -> matrix_sdk::Result<()> { ...@@ -20,7 +20,8 @@ async fn main() -> matrix_sdk::Result<()> {
]; ];
matrix::Bot::new(env::args().last().unwrap()) matrix::Bot::new(env::args().last().unwrap())
.connect().await? .connect()
.await?
.add_package(format!("basic"), basic_cmds) .add_package(format!("basic"), basic_cmds)
.listen() .listen()
.await .await
......
...@@ -232,7 +232,7 @@ impl Bot { ...@@ -232,7 +232,7 @@ impl Bot {
} }
pub fn add_package(&self, name: String, cmds: Vec<Cmd>) -> &Self { pub fn add_package(&self, name: String, cmds: Vec<Cmd>) -> &Self {
let pkg = match self.config.section("basic") { let pkg = match self.config.section(&name) {
Some(section) => CmdPackage::from_config(&section, cmds), Some(section) => CmdPackage::from_config(&section, cmds),
None => { None => {
abort(); abort();
...@@ -245,8 +245,8 @@ impl Bot { ...@@ -245,8 +245,8 @@ impl Bot {
pub async fn connect_and_handle(config: Config) -> Result<()> { pub async fn connect_and_handle(config: Config) -> Result<()> {
// TODO: Refactor // TODO: Refactor
let pkg_commands_vec = vec![ let pkg_commands_vec = vec![
Cmd::new_simple(format!("ping"), 0, Arc::new(|_x| true)), Cmd::new_simple(format!("ping"), 0, |_x| true),
Cmd::new_echo(format!("bot_name"), 0, Arc::new(move |_x| format!("toto"))), Cmd::new_echo(format!("bot_name"), 0, |_x| format!("toto")),
]; ];
let pkg = match config.section("basic") { let pkg = match config.section("basic") {
Some(section) => CmdPackage::from_config(&section, pkg_commands_vec), Some(section) => CmdPackage::from_config(&section, pkg_commands_vec),
......
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter