Skip to content

Commit

Permalink
Slake Clippy's thirst for blood (#347)
Browse files Browse the repository at this point in the history
  • Loading branch information
casey committed Aug 28, 2018
1 parent b14d1ec commit 4e6585d
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 72 deletions.
4 changes: 2 additions & 2 deletions src/assignment_evaluator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ mod test {
#[test]
fn backtick_code() {
match parse_success("a:\n echo {{`f() { return 100; }; f`}}")
.run(no_cwd_err(), &["a"], &Default::default()).unwrap_err() {
.run(&no_cwd_err(), &["a"], &Default::default()).unwrap_err() {
RuntimeError::Backtick{token, output_error: OutputError::Code(code)} => {
assert_eq!(code, 100);
assert_eq!(token.lexeme, "`f() { return 100; }; f`");
Expand All @@ -196,7 +196,7 @@ recipe:
..Default::default()
};

match parse_success(text).run(no_cwd_err(), &["recipe"], &configuration).unwrap_err() {
match parse_success(text).run(&no_cwd_err(), &["recipe"], &configuration).unwrap_err() {
RuntimeError::Backtick{token, output_error: OutputError::Code(_)} => {
assert_eq!(token.lexeme, "`echo $exported_variable`");
},
Expand Down
2 changes: 1 addition & 1 deletion src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub use misc::{default, empty};
pub use parameter::Parameter;
pub use parser::Parser;
pub use range_ext::RangeExt;
pub use recipe::Recipe;
pub use recipe::{Recipe, RecipeContext};
pub use recipe_resolver::RecipeResolver;
pub use runtime_error::{RuntimeError, RunResult};
pub use shebang::Shebang;
Expand Down
55 changes: 23 additions & 32 deletions src/justfile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@ pub struct Justfile<'a> {
pub exports: Set<&'a str>,
}

impl<'a, 'b> Justfile<'a>
where
'a: 'b,
{
impl<'a> Justfile<'a> where {
pub fn first(&self) -> Option<&Recipe> {
let mut first: Option<&Recipe> = None;
for recipe in self.recipes.values() {
Expand Down Expand Up @@ -47,9 +44,9 @@ where

pub fn run(
&'a self,
invocation_directory: Result<PathBuf, String>,
invocation_directory: &'a Result<PathBuf, String>,
arguments: &[&'a str],
configuration: &Configuration<'a>,
configuration: &'a Configuration<'a>,
) -> RunResult<'a, ()> {
let unknown_overrides = configuration
.overrides
Expand All @@ -68,7 +65,7 @@ where

let scope = AssignmentEvaluator::evaluate_assignments(
&self.assignments,
&invocation_directory,
invocation_directory,
&dotenv,
&configuration.overrides,
configuration.quiet,
Expand Down Expand Up @@ -128,52 +125,46 @@ where
});
}

let context = RecipeContext{invocation_directory, configuration, scope};

let mut ran = empty();
for (recipe, arguments) in grouped {
self.run_recipe(
&invocation_directory,
&context,
recipe,
arguments,
&scope,
&dotenv,
configuration,
&mut ran,
)?
}

Ok(())
}

fn run_recipe<'c>(
&'c self,
invocation_directory: &Result<PathBuf, String>,
fn run_recipe<'b>(
&self,
context: &'b RecipeContext<'a>,
recipe: &Recipe<'a>,
arguments: &[&'a str],
scope: &Map<&'c str, String>,
dotenv: &Map<String, String>,
configuration: &Configuration<'a>,
ran: &mut Set<&'a str>,
) -> RunResult<()> {
for dependency_name in &recipe.dependencies {
if !ran.contains(dependency_name) {
self.run_recipe(
invocation_directory,
context,
&self.recipes[dependency_name],
&[],
scope,
dotenv,
configuration,
ran,
)?;
}
}
recipe.run(
invocation_directory,
context,
arguments,
scope,
dotenv,
&self.exports,
configuration,
)?;
ran.insert(recipe.name);
Ok(())
Expand Down Expand Up @@ -217,7 +208,7 @@ mod test {
#[test]
fn unknown_recipes() {
match parse_success("a:\nb:\nc:")
.run(no_cwd_err(), &["a", "x", "y", "z"], &Default::default())
.run(&no_cwd_err(), &["a", "x", "y", "z"], &Default::default())
.unwrap_err()
{
UnknownRecipes {
Expand Down Expand Up @@ -250,7 +241,7 @@ a:
";

match parse_success(text)
.run(no_cwd_err(), &["a"], &Default::default())
.run(&no_cwd_err(), &["a"], &Default::default())
.unwrap_err()
{
Code {
Expand All @@ -269,7 +260,7 @@ a:
#[test]
fn code_error() {
match parse_success("fail:\n @exit 100")
.run(no_cwd_err(), &["fail"], &Default::default())
.run(&no_cwd_err(), &["fail"], &Default::default())
.unwrap_err()
{
Code {
Expand All @@ -292,7 +283,7 @@ a return code:
@x() { {{return}} {{code + "0"}}; }; x"#;

match parse_success(text)
.run(no_cwd_err(), &["a", "return", "15"], &Default::default())
.run(&no_cwd_err(), &["a", "return", "15"], &Default::default())
.unwrap_err()
{
Code {
Expand All @@ -311,7 +302,7 @@ a return code:
#[test]
fn missing_some_arguments() {
match parse_success("a b c d:")
.run(no_cwd_err(), &["a", "b", "c"], &Default::default())
.run(&no_cwd_err(), &["a", "b", "c"], &Default::default())
.unwrap_err()
{
ArgumentCountMismatch {
Expand All @@ -332,7 +323,7 @@ a return code:
#[test]
fn missing_some_arguments_variadic() {
match parse_success("a b c +d:")
.run(no_cwd_err(), &["a", "B", "C"], &Default::default())
.run(&no_cwd_err(), &["a", "B", "C"], &Default::default())
.unwrap_err()
{
ArgumentCountMismatch {
Expand All @@ -353,7 +344,7 @@ a return code:
#[test]
fn missing_all_arguments() {
match parse_success("a b c d:\n echo {{b}}{{c}}{{d}}")
.run(no_cwd_err(), &["a"], &Default::default())
.run(&no_cwd_err(), &["a"], &Default::default())
.unwrap_err()
{
ArgumentCountMismatch {
Expand All @@ -374,7 +365,7 @@ a return code:
#[test]
fn missing_some_defaults() {
match parse_success("a b c d='hello':")
.run(no_cwd_err(), &["a", "b"], &Default::default())
.run(&no_cwd_err(), &["a", "b"], &Default::default())
.unwrap_err()
{
ArgumentCountMismatch {
Expand All @@ -395,7 +386,7 @@ a return code:
#[test]
fn missing_all_defaults() {
match parse_success("a b c='r' d='h':")
.run(no_cwd_err(), &["a"], &Default::default())
.run(&no_cwd_err(), &["a"], &Default::default())
.unwrap_err()
{
ArgumentCountMismatch {
Expand All @@ -419,7 +410,7 @@ a return code:
configuration.overrides.insert("foo", "bar");
configuration.overrides.insert("baz", "bob");
match parse_success("a:\n echo {{`f() { return 100; }; f`}}")
.run(no_cwd_err(), &["a"], &configuration)
.run(&no_cwd_err(), &["a"], &configuration)
.unwrap_err()
{
UnknownOverrides { overrides } => {
Expand Down Expand Up @@ -447,7 +438,7 @@ wut:
};

match parse_success(text)
.run(no_cwd_err(), &["wut"], &configuration)
.run(&no_cwd_err(), &["wut"], &configuration)
.unwrap_err()
{
Code {
Expand Down
4 changes: 2 additions & 2 deletions src/misc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ pub fn write_error_context(
i += c.len_utf8();
}
let line_number_width = line_number.to_string().len();
write!(f, "{0:1$} |\n", "", line_number_width)?;
write!(f, "{} | {}\n", line_number, space_line)?;
writeln!(f, "{0:1$} |", "", line_number_width)?;
writeln!(f, "{} | {}", line_number, space_line)?;
write!(f, "{0:1$} |", "", line_number_width)?;
if width == None {
write!(f, " {0:1$}{2}^{3}", "", space_column, red.prefix(), red.suffix())?;
Expand Down
46 changes: 26 additions & 20 deletions src/recipe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,19 @@ pub struct Recipe<'a> {
pub shebang: bool,
}

pub struct RecipeContext<'a> {
pub invocation_directory: &'a Result<PathBuf, String>,
pub configuration: &'a Configuration<'a>,
pub scope: Map<&'a str, String>,
}

impl<'a> Recipe<'a> {
pub fn argument_range(&self) -> Range<usize> {
self.min_arguments()..self.max_arguments() + 1
}

pub fn min_arguments(&self) -> usize {
self.parameters.iter().filter(|p| !p.default.is_some()).count()
self.parameters.iter().filter(|p| p.default.is_none()).count()
}

pub fn max_arguments(&self) -> usize {
Expand All @@ -50,13 +56,13 @@ impl<'a> Recipe<'a> {

pub fn run(
&self,
invocation_directory: &Result<PathBuf, String>,
arguments: &[&'a str],
scope: &Map<&'a str, String>,
dotenv: &Map<String, String>,
exports: &Set<&'a str>,
configuration: &Configuration,
context: &RecipeContext<'a>,
arguments: &[&'a str],
dotenv: &Map<String, String>,
exports: &Set<&'a str>,
) -> RunResult<'a, ()> {
let configuration = &context.configuration;

if configuration.verbose {
let color = configuration.color.stderr().banner();
eprintln!("{}===> Running recipe `{}`...{}", color.prefix(), self.name, color.suffix());
Expand Down Expand Up @@ -86,16 +92,16 @@ impl<'a> Recipe<'a> {
}

let mut evaluator = AssignmentEvaluator {
assignments: &empty(),
invocation_directory,
dry_run: configuration.dry_run,
evaluated: empty(),
overrides: &empty(),
quiet: configuration.quiet,
shell: configuration.shell,
assignments: &empty(),
dry_run: configuration.dry_run,
evaluated: empty(),
invocation_directory: context.invocation_directory,
overrides: &empty(),
quiet: configuration.quiet,
scope: &context.scope,
shell: configuration.shell,
dotenv,
exports,
scope,
};

if self.shebang {
Expand Down Expand Up @@ -157,7 +163,7 @@ impl<'a> Recipe<'a> {
let mut command = Platform::make_shebang_command(&path, interpreter, argument)
.map_err(|output_error| RuntimeError::Cygpath{recipe: self.name, output_error})?;

command.export_environment_variables(scope, dotenv, exports)?;
command.export_environment_variables(&context.scope, dotenv, exports)?;

// run it!
match InterruptHandler::guard(|| command.status()) {
Expand Down Expand Up @@ -232,7 +238,7 @@ impl<'a> Recipe<'a> {
cmd.stdout(Stdio::null());
}

cmd.export_environment_variables(scope, dotenv, exports)?;
cmd.export_environment_variables(&context.scope, dotenv, exports)?;

match InterruptHandler::guard(|| cmd.status()) {
Ok(exit_status) => if let Some(code) = exit_status.code() {
Expand Down Expand Up @@ -271,7 +277,7 @@ impl<'a> Display for Recipe<'a> {

for (i, pieces) in self.lines.iter().enumerate() {
if i == 0 {
writeln!(f, "")?;
writeln!(f)?;
}
for (j, piece) in pieces.iter().enumerate() {
if j == 0 {
Expand All @@ -280,11 +286,11 @@ impl<'a> Display for Recipe<'a> {
match *piece {
Fragment::Text{ref text} => write!(f, "{}", text.lexeme)?,
Fragment::Expression{ref expression, ..} =>
write!(f, "{}{}{}", "{{", expression, "}}")?,
write!(f, "{{{{{}}}}}", expression)?,
}
}
if i + 1 < self.lines.len() {
write!(f, "\n")?;
writeln!(f)?;
}
}
Ok(())
Expand Down
2 changes: 1 addition & 1 deletion src/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ pub fn run() {
}

if let Err(run_error) = justfile.run(
invocation_directory,
&invocation_directory,
&arguments,
&configuration)
{
Expand Down
Loading

0 comments on commit 4e6585d

Please sign in to comment.