diff options
| author | Fuwn <[email protected]> | 2021-05-01 23:34:55 +0000 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2021-05-01 23:34:55 +0000 |
| commit | cb58dab1828f847ffdf51e336c34bc71d973706d (patch) | |
| tree | 7f8bf76c58befda2b901d26ad4bc1980fa9e399f /src/server/cmd/commands/text.rs | |
| parent | ci(actions): fix rust action not running on changes performed on the src dire... (diff) | |
| download | whirl-cb58dab1828f847ffdf51e336c34bc71d973706d.tar.xz whirl-cb58dab1828f847ffdf51e336c34bc71d973706d.zip | |
feat(cmds): trait based commands
This commit adds the abiltity to implement a series of given traits for a command, where each
command should implement **at least** one of these traits; Parsable, ParsableWithArguments,
Creatable. These changed are put in place to ensure that when implementing a command, the proper
methods are implemented in the proper format.
Diffstat (limited to 'src/server/cmd/commands/text.rs')
| -rw-r--r-- | src/server/cmd/commands/text.rs | 39 |
1 files changed, 29 insertions, 10 deletions
diff --git a/src/server/cmd/commands/text.rs b/src/server/cmd/commands/text.rs index 3029eac..047dedf 100644 --- a/src/server/cmd/commands/text.rs +++ b/src/server/cmd/commands/text.rs @@ -5,21 +5,17 @@ use std::str::from_utf8; use bytes::{BufMut, BytesMut}; -use crate::server::cmd::constants::TEXT; +use crate::server::cmd::{ + constants::TEXT, + extendable::{Creatable, ParsableWithArguments}, +}; pub struct Text { pub sender: String, pub content: String, } -impl Text { - pub fn parse(data: Vec<u8>, username: &str) -> Self { - Self { - sender: username.to_string(), - content: from_utf8(&data[6..]).unwrap().to_string(), - } - } - - pub fn create(self) -> Vec<u8> { +impl Creatable for Text { + fn create(self) -> Vec<u8> { let mut command = BytesMut::new(); // Header @@ -44,3 +40,26 @@ impl Text { command_as_vec } } +impl ParsableWithArguments for Text { + /// The first and only element of `args` *should* be the username of the sender. + /// + /// There isn't anything currently stopping someone from passing some other value so that might be + /// annoying at times. + /// + /// Realistically, this method is mostly static so the username will *always* be passed properly + /// unless someone intentionally commits breaking changes on purpose regarding what is passed to + /// to this method where called. + /// + /// It would be neat to have some sort of ability to statically check if the `args` argument + /// contains x number of elements at compile time or something of the sort but the Rust RFC is + /// probably not focused on that. + /// + /// So, right now, trust is in the developers' hands to make sure to pass the right -- number + /// -- of elements to `args`. + fn parse(data: Vec<u8>, args: &[&str]) -> Self { + Self { + sender: args[0].to_string(), + content: from_utf8(&data[6..]).unwrap().to_string(), + } + } +} |