Skip to content

Commit

Permalink
Take into regard prefixless groups
Browse files Browse the repository at this point in the history
  • Loading branch information
arqunis committed Jun 10, 2019
1 parent 8c83fec commit ef15739
Showing 1 changed file with 21 additions and 12 deletions.
33 changes: 21 additions & 12 deletions src/framework/standard/parse.rs
Expand Up @@ -77,20 +77,12 @@ pub fn parse_prefix<'a>(
(Prefix::None, args.trim())
}

#[allow(clippy::enum_variant_names)]
#[derive(Debug, Clone, Copy, PartialEq)]
enum ParseMode {
BySpace,
ByLength,
}

struct CommandParser<'msg, 'groups, 'config, 'ctx> {
msg: &'msg Message,
stream: StringStream<'msg>,
groups: &'groups [&'static CommandGroup],
config: &'config Configuration,
ctx: &'ctx Context,
mode: ParseMode,
unrecognised: Option<&'msg str>,
}

Expand All @@ -108,15 +100,15 @@ impl<'msg, 'groups, 'config, 'ctx> CommandParser<'msg, 'groups, 'config, 'ctx> {
groups,
config,
ctx,
mode: if config.by_space { ParseMode::BySpace } else { ParseMode::ByLength },
unrecognised: None,
}
}

fn next_text(&self, length: impl FnOnce() -> usize) -> &'msg str {
match self.mode {
ParseMode::ByLength => self.stream.peek_for(length()),
ParseMode::BySpace => self.stream.peek_until(|s| s.is_whitespace()),
if self.config.by_space {
self.stream.peek_until(|s| s.is_whitespace())
} else {
self.stream.peek_for(length())
}
}

Expand Down Expand Up @@ -207,6 +199,23 @@ impl<'msg, 'groups, 'config, 'ctx> CommandParser<'msg, 'groups, 'config, 'ctx> {
}

fn group(&mut self, group: &'static CommandGroup) -> Result<(Option<&'msg str>, &'static CommandGroup), DispatchError> {
// Do not bother going through this group's prefixes if it doesn't contain any, but
// try if its sub-groups maybe do and are satisfied in the message.
if group.options.prefixes.is_empty() {
for sub_group in group.sub_groups {
let x = self.group(*sub_group)?;

if x.0.is_some() {
return Ok(x);
}
}

// It appears they don't. Fall back to using this group.
self.check_discrepancy(&group.options)?;

return Ok((None, group));
}

for p in group.options.prefixes {
let pp = self.next_text(|| p.chars().count());

Expand Down

0 comments on commit ef15739

Please sign in to comment.