Write argument_range() in terms of min_arguments() and max_arguments() (#193)
This commit is contained in:
parent
1990c58a21
commit
7f2d360231
18
src/lib.rs
18
src/lib.rs
@ -259,12 +259,18 @@ fn run_backtick<'a>(
|
|||||||
|
|
||||||
impl<'a> Recipe<'a> {
|
impl<'a> Recipe<'a> {
|
||||||
fn argument_range(&self) -> Range<usize> {
|
fn argument_range(&self) -> Range<usize> {
|
||||||
|
self.min_arguments()..self.max_arguments() + 1
|
||||||
|
}
|
||||||
|
|
||||||
|
fn min_arguments(&self) -> usize {
|
||||||
self.parameters.iter().filter(|p| !p.default.is_some()).count()
|
self.parameters.iter().filter(|p| !p.default.is_some()).count()
|
||||||
..
|
}
|
||||||
|
|
||||||
|
fn max_arguments(&self) -> usize {
|
||||||
if self.parameters.iter().any(|p| p.variadic) {
|
if self.parameters.iter().any(|p| p.variadic) {
|
||||||
std::usize::MAX
|
std::usize::MAX - 1
|
||||||
} else {
|
} 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]));
|
grouped.push((recipe, &tail[0..0]));
|
||||||
} else {
|
} else {
|
||||||
let argument_range = recipe.argument_range();
|
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) {
|
if !contains(&argument_range, argument_count) {
|
||||||
return Err(RunError::ArgumentCountMismatch {
|
return Err(RunError::ArgumentCountMismatch {
|
||||||
recipe: recipe.name,
|
recipe: recipe.name,
|
||||||
found: tail.len(),
|
found: tail.len(),
|
||||||
min: argument_range.start,
|
min: recipe.min_arguments(),
|
||||||
max: argument_range.end - 1,
|
max: recipe.max_arguments(),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
grouped.push((recipe, &tail[0..argument_count]));
|
grouped.push((recipe, &tail[0..argument_count]));
|
||||||
|
Loading…
Reference in New Issue
Block a user