Implement Flags parser and handle choose --everyone

- A generic Flags parser that could handle any `--flag` argument.
- `choose --everyone` overrides the Offline/DnD check.
This commit is contained in:
Natsu Kagami 2021-11-21 17:37:28 -05:00
parent 2449b09cb2
commit 2c2092eb91
Signed by: nki
GPG key ID: 7306B3D3C3AD6E51
3 changed files with 46 additions and 0 deletions

View file

@ -0,0 +1,38 @@
use serenity::prelude::Args;
use std::collections::HashSet as Set;
/// Handle flags parsing.
pub struct Flags(Set<String>);
struct Flag(pub String);
impl std::str::FromStr for Flag {
type Err = ();
fn from_str(s: &str) -> Result<Self, Self::Err> {
if s.starts_with("--") {
Ok(Flag(s.trim_start_matches("--").to_owned()))
} else {
Err(())
}
}
}
impl Flags {
/// Parses the set of flags from a given `Args` structure.
pub fn collect_from(args: &mut Args) -> Flags {
let mut set = Set::new();
loop {
if let Some(Flag(s)) = args.find().ok() {
set.insert(s);
} else {
break Flags(set);
}
}
}
/// Checks whether `flag` exists in the flags set.
pub fn contains(&self, flag: impl AsRef<str>) -> bool {
self.0.contains(flag.as_ref())
}
}