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}; ...@@ -7,9 +7,9 @@ use std::{process::abort, string::String, sync::Arc};
#[allow(dead_code)] #[allow(dead_code)]
#[derive(Clone)] #[derive(Clone)]
enum CmdHandler { enum CmdHandler {
Simple(Arc<dyn Fn(Vec<String>) -> bool>), Simple(Arc<dyn Fn(Vec<&str>) -> bool>),
Echo(Arc<dyn Fn(Vec<String>) -> String>), Echo(Arc<dyn Fn(Vec<&str>) -> String>),
MultiEcho(Arc<dyn Fn(Vec<String>) -> Vec<String>>), MultiEcho(Arc<dyn Fn(Vec<&str>) -> Vec<String>>),
} }
#[allow(dead_code)] #[allow(dead_code)]
...@@ -25,12 +25,12 @@ unsafe impl Send for Cmd {} ...@@ -25,12 +25,12 @@ unsafe impl Send for Cmd {}
impl Cmd { impl Cmd {
#[allow(dead_code)] #[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()) (self.name == *name) && (self.argc as usize == args.len())
} }
#[allow(dead_code)] #[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 { if args.len() != self.argc as usize {
error!("Arg count doesn't match"); error!("Arg count doesn't match");
abort(); abort();
...@@ -52,7 +52,7 @@ impl Cmd { ...@@ -52,7 +52,7 @@ impl Cmd {
} }
#[allow(dead_code)] #[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 { Cmd {
name: name, name: name,
argc: argc, argc: argc,
...@@ -61,7 +61,7 @@ impl Cmd { ...@@ -61,7 +61,7 @@ impl Cmd {
} }
#[allow(dead_code)] #[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 { Cmd {
name: name, name: name,
argc: argc, argc: argc,
...@@ -73,7 +73,7 @@ impl Cmd { ...@@ -73,7 +73,7 @@ impl Cmd {
pub fn new_multi_echo( pub fn new_multi_echo(
name: String, name: String,
argc: u32, argc: u32,
f: Arc<dyn Fn(Vec<String>) -> Vec<String>>, f: Arc<dyn Fn(Vec<&str>) -> Vec<String>>,
) -> Cmd { ) -> Cmd {
Cmd { Cmd {
name: name, name: name,
......
...@@ -17,7 +17,7 @@ pub struct CmdPackage { ...@@ -17,7 +17,7 @@ pub struct CmdPackage {
impl CmdPackage { impl CmdPackage {
#[allow(dead_code)] #[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() { match user_cmd.split_first() {
None => false, None => false,
Some((name, args)) => match self Some((name, args)) => match self
...@@ -32,7 +32,7 @@ impl CmdPackage { ...@@ -32,7 +32,7 @@ impl CmdPackage {
} }
#[allow(dead_code)] #[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) { match CmdAcl::action_permited(&self.admin_register, &self.acl, room, user_id) {
Err(string) => "Action not permited: ".to_string() + &string, Err(string) => "Action not permited: ".to_string() + &string,
Ok(()) => match user_cmd.split_first() { Ok(()) => match user_cmd.split_first() {
......
...@@ -98,25 +98,24 @@ pub async fn connect_and_handle(config: Config) -> Result<()> { ...@@ -98,25 +98,24 @@ pub async fn connect_and_handle(config: Config) -> Result<()> {
GLOBAL_PKG.lock().unwrap().push(pkg); GLOBAL_PKG.lock().unwrap().push(pkg);
let on_msg_room_event = let on_msg_room_event = |ev: SyncMessageEvent<MessageEventContent>,
move |ev: SyncMessageEvent<MessageEventContent>, room: Room,
room: Room, _encryption_info: Option<EncryptionInfo>| async {
_encryption_info: Option<EncryptionInfo>| async move { let sender_id = ev.sender;
let sender_id = ev.sender; if sender_id.as_str() == room.own_user_id().as_str() {
if sender_id.as_str() == room.own_user_id().as_str() { return ();
return; }
}
if room.name().is_none() { if room.name().is_none() {
warn!("Unsupported rooms without names"); warn!("Unsupported rooms without names");
return; return ();
} }
if let Room::Joined(joined) = room { 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("%") {
if body.starts_with("%") { if let Some(stripped) = body.strip_prefix("%") {
let args = vec![]; let args = stripped.split(' ').collect::<Vec<&str>>();
let mut res: String = format!("Command not found"); let mut res: String = format!("Command not found");
if let Some(pkg) = GLOBAL_PKG if let Some(pkg) = GLOBAL_PKG
.lock() .lock()
...@@ -139,15 +138,17 @@ pub async fn connect_and_handle(config: Config) -> Result<()> { ...@@ -139,15 +138,17 @@ pub async fn connect_and_handle(config: Config) -> Result<()> {
} }
} }
} }
}
// if let Room::Invited(_) = room { // if let Room::Invited(_) = room {
// warn!("Bot was invited by {sender_id} to room {room_id} a.k.a. {room_name}",); // warn!("Bot was invited by {sender_id} to room {room_id} a.k.a. {room_name}",);
// } // }
// if let Room::Left(_) = room { // if let Room::Left(_) = room {
// error!("Bot left room {room_id} a.k.a. {room_name}") // error!("Bot left room {room_id} a.k.a. {room_name}")
// } // }
}; return ();
};
let alice = UserId::try_from(config.user_name)?; let alice = UserId::try_from(config.user_name)?;
let client = Client::new(config.homeserver_url)?; let client = Client::new(config.homeserver_url)?;
......
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