From f4c6afe3eb48088fed57fb9fcc4dce44bd505234 Mon Sep 17 00:00:00 2001 From: zseri Date: Sat, 27 Mar 2021 08:14:31 +0100 Subject: boring changes --- src/args.rs | 2 +- src/main.rs | 8 ++--- src/makefile/mod.rs | 98 +++++++++++++++++++++-------------------------------- 3 files changed, 43 insertions(+), 65 deletions(-) diff --git a/src/args.rs b/src/args.rs index e193294..efc0f11 100644 --- a/src/args.rs +++ b/src/args.rs @@ -146,7 +146,7 @@ impl Args { } else { env_makeflags }; - let env_makeflags = env_makeflags.split_whitespace().map(|x| OsString::from(x)); + let env_makeflags = env_makeflags.split_whitespace().map(OsString::from); // per the structopt docs, the first argument will be used as the binary name, // so we need to make sure it goes in before MAKEFLAGS let arg0 = args.next().unwrap_or_else(|| env!("CARGO_PKG_NAME").into()); diff --git a/src/main.rs b/src/main.rs index 009fccf..e58188b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,14 +12,14 @@ fn main() { let mut args = Args::from_env_and_args(); // If no makefile is specified, try some options. if args.makefile.is_empty() { - if metadata("./makefile").is_ok() { - args.makefile = vec!["./makefile".into()]; + args.makefile = vec![if metadata("./makefile").is_ok() { + "./makefile".into() } else if metadata("./Makefile").is_ok() { - args.makefile = vec!["./Makefile".into()]; + "./Makefile".into() } else { // TODO handle error gracefully panic!("no makefile found"); - } + }]; } // Read in the makefile(s) specified. // TODO dump command-line args into MAKEFLAGS diff --git a/src/makefile/mod.rs b/src/makefile/mod.rs index 138d612..d55238e 100644 --- a/src/makefile/mod.rs +++ b/src/makefile/mod.rs @@ -130,15 +130,12 @@ impl CommandLine { if let Token::Text(text) = line.first_token_mut() { let mut text_chars = text.chars().peekable(); - loop { - match text_chars.peek() { - Some('-') | Some('@') | Some('+') => match text_chars.next() { - Some('-') => ignore_errors = true, - Some('@') => silent = true, - Some('+') => always_execute = true, - _ => unreachable!(), - }, - _ => break, + while let Some(x) = text_chars.next_if(|x| matches!(x, '-' | '@' | '+')) { + match x { + '-' => ignore_errors = true, + '@' => silent = true, + '+' => always_execute = true, + _ => unreachable!(), } } *text = text_chars.collect(); @@ -247,18 +244,14 @@ impl Makefile { } for r#macro in args.macros() { - let pieces = r#macro.splitn(2, '=').collect::>(); - match *pieces { - [name, value] => { - macros.insert( - name.into(), - ( - MacroSource::CommandLineOrMAKEFLAGS, - TokenString::text(value), - ), - ); - } - _ => {} + if let [name, value] = *r#macro.splitn(2, '=').collect::>() { + macros.insert( + name.into(), + ( + MacroSource::CommandLineOrMAKEFLAGS, + TokenString::text(value), + ), + ); } } @@ -281,27 +274,17 @@ impl Makefile { pub fn and_read(&mut self, source: impl BufRead) -> &mut Makefile { let mut lines_iter = source.lines().enumerate().peekable(); - while lines_iter.peek().is_some() { - let (line_number, line) = match lines_iter.next() { - Some(x) => x, - // fancy Rust trick: break-with-an-argument to return a value from a - // `loop` expression - None => break, - }; + while let Some((line_number, line)) = lines_iter.next() { // TODO handle I/O errors at all let mut line = line.expect("failed to read line of makefile!"); // handle escaped newlines - while line.ends_with(r"\") { - let next_line = match lines_iter.next() { - Some((_, x)) => x, - None => Ok("".into()), - }; - let next_line = next_line.expect("failed to read line of makefile!"); - let next_line = next_line.trim_start(); + while line.ends_with('\\') { line.pop(); line.push(' '); - line.push_str(next_line); + if let Some((_, x)) = lines_iter.next() { + line.push_str(x.expect("failed to read line of makefile!").trim_start()) + } } // handle comments @@ -339,8 +322,8 @@ impl Makefile { fn get_line_type(line_tokens: &TokenString) -> LineType { for token in line_tokens.tokens() { if let Token::Text(text) = token { - let colon_idx = text.find(":"); - let equals_idx = text.find("="); + let colon_idx = text.find(':'); + let equals_idx = text.find('='); match (colon_idx, equals_idx) { (Some(_), None) => { return LineType::Rule; @@ -369,12 +352,12 @@ impl Makefile { let targets = self.expand_macros(&targets, None); let targets = targets .split_whitespace() - .map(|x| x.into()) + .map(Into::into) .collect::>(); let (prerequisites, mut commands) = match not_targets.split_once(';') { Some((prerequisites, mut command)) => { - while command.ends_with(r"\") && lines_iter.peek().is_some() { - command.strip_suffix(r"\"); + while command.ends_with("\\") && lines_iter.peek().is_some() { + command.strip_suffix("\\"); command .extend(tokenize(&lines_iter.next().unwrap().1.unwrap())); } @@ -388,13 +371,15 @@ impl Makefile { .map(|x| x.into()) .collect::>(); - while lines_iter - .peek() - .and_then(|(_, x)| x.as_ref().ok()) - .map_or(false, |line| line.starts_with('\t') || line.is_empty()) - { - let line = lines_iter.next().unwrap().1.unwrap(); - let mut line: String = line.strip_prefix("\t").unwrap_or(&line).into(); + while let Some((_, x)) = lines_iter.next_if(|(_, x)| { + x.as_ref() + .ok() + .map_or(false, |line| line.starts_with('\t') || line.is_empty()) + }) { + let mut line = x.unwrap(); + if !line.is_empty() { + line.remove(0); + } if line.is_empty() { continue; } @@ -432,7 +417,7 @@ impl Makefile { let special_target_match = SPECIAL_TARGET.captures(&targets[0]); let inference_rule = targets.len() == 1 - && prerequisites.len() == 0 + && prerequisites.is_empty() && inference_match.is_some() && special_target_match.is_none(); if inference_rule { @@ -559,7 +544,7 @@ impl Makefile { match targets.get(target) { Some(target) => { let target = target.borrow(); - target.prerequisites.len() == 0 || target.prerequisites.contains(&name.into()) + target.prerequisites.is_empty() || target.prerequisites.iter().any(|e| e == name) } None => false, } @@ -765,7 +750,7 @@ impl Makefile { } } } - return result; + result } } @@ -787,10 +772,8 @@ impl fmt::Display for Makefile { writeln!(f)?; header(f, "Targets")?; - let targets = self.targets.borrow(); - for (_, target) in &*targets { - let target = target.borrow(); - writeln!(f, "{}", target)?; + for target in self.targets.borrow().values() { + writeln!(f, "{}", target.borrow())?; } Ok(()) @@ -920,8 +903,3 @@ fn builtin_targets() -> Vec { already_updated: Cell::new(false), }] } - -#[cfg(test)] -mod test { - use super::*; -} -- cgit v1.2.3