diff options
Diffstat (limited to 'src/framework/standard/args.rs')
| -rw-r--r-- | src/framework/standard/args.rs | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/src/framework/standard/args.rs b/src/framework/standard/args.rs index 2700bfb..6841bd7 100644 --- a/src/framework/standard/args.rs +++ b/src/framework/standard/args.rs @@ -348,6 +348,51 @@ impl Args { self.args.get(self.offset).map(|t| t.lit.as_str()) } + /// Trims the current argument off leading and trailing whitespace. + /// + /// # Examples + /// + /// ```rust + /// use serenity::framework::standard::Args; + /// + /// let mut args = Args::new(" 42 ", &[]); + /// + /// assert_eq!(args.trim().current(), Some("42")); + /// ``` + pub fn trim(&mut self) -> &mut Self { + if self.is_empty() { + return self; + } + + self.args[self.offset].lit = self.args[self.offset].lit.trim().to_string(); + + self + } + + /// Trims all of the arguments after the offset off leading and trailing whitespace. + /// + /// # Examples + /// + /// ```rust + /// use serenity::framework::standard::Args; + /// + /// let mut args = Args::new(" 42 , 84 ,\t168\t", &[",".to_string()]); + /// + /// args.trim_all(); + /// assert_eq!(args.single::<String>().unwrap(), "42"); + /// assert_eq!(args.single::<String>().unwrap(), "84"); + /// assert_eq!(args.single::<String>().unwrap(), "168"); + /// ``` + pub fn trim_all(&mut self) { + if self.is_empty() { + return; + } + + for token in &mut self.args[self.offset..] { + token.lit = token.lit.trim().to_string(); + } + } + /// Parses the current argument and advances. /// /// # Examples |