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

Parsing commands + use &str instead of String

The &str type should be lighter than String.
parent 6fbcf34f
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
......@@ -7,9 +7,9 @@ use std::{process::abort, string::String, sync::Arc};
#[allow(dead_code)]
#[derive(Clone)]
enum CmdHandler {
Simple(Arc<dyn Fn(Vec<String>) -> bool>),
Echo(Arc<dyn Fn(Vec<String>) -> String>),
MultiEcho(Arc<dyn Fn(Vec<String>) -> Vec<String>>),
Simple(Arc<dyn Fn(Vec<&str>) -> bool>),
Echo(Arc<dyn Fn(Vec<&str>) -> String>),
MultiEcho(Arc<dyn Fn(Vec<&str>) -> Vec<String>>),
}
#[allow(dead_code)]
......@@ -25,12 +25,12 @@ unsafe impl Send for Cmd {}
impl Cmd {
#[allow(dead_code)]
pub fn matches(&self, name: &String, args: &Vec<String>) -> bool {
pub fn matches(&self, name: &str, args: &Vec<&str>) -> bool {
(self.name == *name) && (self.argc as usize == args.len())
}
#[allow(dead_code)]
pub fn exec(&self, args: Vec<String>) -> String {
pub fn exec(&self, args: Vec<&str>) -> String {
if args.len() != self.argc as usize {
error!("Arg count doesn't match");
abort();
......@@ -52,7 +52,7 @@ impl Cmd {
}
#[allow(dead_code)]
pub fn new_simple(name: String, argc: u32, f: Arc<dyn Fn(Vec<String>) -> bool>) -> Cmd {
pub fn new_simple(name: String, argc: u32, f: Arc<dyn Fn(Vec<&str>) -> bool>) -> Cmd {
Cmd {
name: name,
argc: argc,
......@@ -61,7 +61,7 @@ impl Cmd {
}
#[allow(dead_code)]
pub fn new_echo(name: String, argc: u32, f: Arc<dyn Fn(Vec<String>) -> String>) -> Cmd {
pub fn new_echo(name: String, argc: u32, f: Arc<dyn Fn(Vec<&str>) -> String>) -> Cmd {
Cmd {
name: name,
argc: argc,
......@@ -73,7 +73,7 @@ impl Cmd {
pub fn new_multi_echo(
name: String,
argc: u32,
f: Arc<dyn Fn(Vec<String>) -> Vec<String>>,
f: Arc<dyn Fn(Vec<&str>) -> Vec<String>>,
) -> Cmd {
Cmd {
name: name,
......
......@@ -17,7 +17,7 @@ pub struct CmdPackage {
impl CmdPackage {
#[allow(dead_code)]
pub fn has_command(&self, user_cmd: &Vec<String>) -> bool {
pub fn has_command(&self, user_cmd: &Vec<&str>) -> bool {
match user_cmd.split_first() {
None => false,
Some((name, args)) => match self
......@@ -32,7 +32,7 @@ impl CmdPackage {
}
#[allow(dead_code)]
pub fn handle(&self, room: &JoinedRoom, user_id: &UserId, user_cmd: Vec<String>) -> String {
pub fn handle(&self, room: &JoinedRoom, user_id: &UserId, user_cmd: Vec<&str>) -> String {
match CmdAcl::action_permited(&self.admin_register, &self.acl, room, user_id) {
Err(string) => "Action not permited: ".to_string() + &string,
Ok(()) => match user_cmd.split_first() {
......
......@@ -98,25 +98,24 @@ pub async fn connect_and_handle(config: Config) -> Result<()> {
GLOBAL_PKG.lock().unwrap().push(pkg);
let on_msg_room_event =
move |ev: SyncMessageEvent<MessageEventContent>,
let on_msg_room_event = |ev: SyncMessageEvent<MessageEventContent>,
room: Room,
_encryption_info: Option<EncryptionInfo>| async move {
_encryption_info: Option<EncryptionInfo>| async {
let sender_id = ev.sender;
if sender_id.as_str() == room.own_user_id().as_str() {
return;
return ();
}
if room.name().is_none() {
warn!("Unsupported rooms without names");
return;
return ();
}
if let Room::Joined(joined) = room {
if let MessageType::Text(TextMessageEventContent { body, .. }) = ev.content.msgtype
{
if let MessageType::Text(TextMessageEventContent { body, .. }) = ev.content.msgtype {
if body.starts_with("%") {
let args = vec![];
if let Some(stripped) = body.strip_prefix("%") {
let args = stripped.split(' ').collect::<Vec<&str>>();
let mut res: String = format!("Command not found");
if let Some(pkg) = GLOBAL_PKG
.lock()
......@@ -139,6 +138,7 @@ pub async fn connect_and_handle(config: Config) -> Result<()> {
}
}
}
}
// if let Room::Invited(_) = room {
// warn!("Bot was invited by {sender_id} to room {room_id} a.k.a. {room_name}",);
......@@ -147,6 +147,7 @@ pub async fn connect_and_handle(config: Config) -> Result<()> {
// if let Room::Left(_) = room {
// error!("Bot left room {room_id} a.k.a. {room_name}")
// }
return ();
};
let alice = UserId::try_from(config.user_name)?;
......
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Veuillez vous inscrire ou vous pour commenter