From 7f2d36023140da86f792f9d2cd0d182068f905bf Mon Sep 17 00:00:00 2001 From: Casey Rodarmor Date: Sun, 7 May 2017 14:45:23 -0700 Subject: [PATCH] Write argument_range() in terms of min_arguments() and max_arguments() (#193) --- src/lib.rs | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index f4d941b..4d61d96 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -259,12 +259,18 @@ fn run_backtick<'a>( impl<'a> Recipe<'a> { fn argument_range(&self) -> Range { + self.min_arguments()..self.max_arguments() + 1 + } + + fn min_arguments(&self) -> usize { self.parameters.iter().filter(|p| !p.default.is_some()).count() - .. + } + + fn max_arguments(&self) -> usize { if self.parameters.iter().any(|p| p.variadic) { - std::usize::MAX + std::usize::MAX - 1 } else { - self.parameters.len() + 1 + self.parameters.len() } } @@ -1230,13 +1236,13 @@ impl<'a, 'b> Justfile<'a> where 'a: 'b { grouped.push((recipe, &tail[0..0])); } else { let argument_range = recipe.argument_range(); - let argument_count = cmp::min(tail.len(), argument_range.end - 1); + let argument_count = cmp::min(tail.len(), recipe.max_arguments()); if !contains(&argument_range, argument_count) { return Err(RunError::ArgumentCountMismatch { recipe: recipe.name, found: tail.len(), - min: argument_range.start, - max: argument_range.end - 1, + min: recipe.min_arguments(), + max: recipe.max_arguments(), }); } grouped.push((recipe, &tail[0..argument_count]));