From 9c2a20ef4074656e7f173a1ee9f9c004a791fba4 Mon Sep 17 00:00:00 2001 From: "NGnius (Graham)" Date: Sat, 29 Jan 2022 16:11:01 -0500 Subject: [PATCH] cargo clippy fixes --- mps-interpreter/src/interpretor.rs | 26 ++--- mps-interpreter/src/lang/filter.rs | 18 ++- mps-interpreter/src/lang/filter_replace.rs | 10 +- mps-interpreter/src/lang/pseudo_op.rs | 4 +- mps-interpreter/src/lang/repeated_meme.rs | 2 +- mps-interpreter/src/lang/single_op.rs | 2 +- mps-interpreter/src/lang/utility.rs | 6 +- .../src/lang/vocabulary/comment.rs | 2 +- mps-interpreter/src/lang/vocabulary/files.rs | 8 +- .../lang/vocabulary/filters/empty_filter.rs | 2 +- .../lang/vocabulary/filters/field_filter.rs | 6 +- .../vocabulary/filters/field_filter_maybe.rs | 4 +- .../vocabulary/filters/field_like_filter.rs | 8 +- .../lang/vocabulary/filters/index_filter.rs | 4 +- .../lang/vocabulary/filters/range_filter.rs | 18 +-- .../src/lang/vocabulary/filters/utility.rs | 8 +- mps-interpreter/src/lang/vocabulary/repeat.rs | 68 +++++------ .../vocabulary/sorters/bliss_next_sorter.rs | 14 +-- .../lang/vocabulary/sorters/bliss_sorter.rs | 4 +- .../lang/vocabulary/sorters/empty_sorter.rs | 2 +- .../src/lang/vocabulary/sql_query.rs | 6 +- .../src/lang/vocabulary/sql_simple_query.rs | 6 +- mps-interpreter/src/music/tag.rs | 17 +-- mps-interpreter/src/processing/filesystem.rs | 110 ++++++++---------- mps-interpreter/src/processing/sql.rs | 11 +- mps-interpreter/src/runner.rs | 2 +- mps-interpreter/src/tokens/tokenizer.rs | 12 +- 27 files changed, 173 insertions(+), 207 deletions(-) diff --git a/mps-interpreter/src/interpretor.rs b/mps-interpreter/src/interpretor.rs index adf2c8e..8187181 100644 --- a/mps-interpreter/src/interpretor.rs +++ b/mps-interpreter/src/interpretor.rs @@ -32,7 +32,7 @@ where { pub fn with_vocab(tokenizer: T, vocab: MpsLanguageDictionary) -> Self { Self { - tokenizer: tokenizer, + tokenizer, buffer: VecDeque::new(), current_stmt: None, vocabulary: vocab, @@ -42,7 +42,7 @@ where pub fn with_standard_vocab(tokenizer: T) -> Self { let mut result = Self { - tokenizer: tokenizer, + tokenizer, buffer: VecDeque::new(), current_stmt: None, vocabulary: MpsLanguageDictionary::default(), @@ -66,7 +66,7 @@ impl MpsInterpretor> { let file = File::open(path)?; let tokenizer = crate::tokens::MpsTokenizer::new(file); let mut result = Self { - tokenizer: tokenizer, + tokenizer, buffer: VecDeque::new(), current_stmt: None, vocabulary: MpsLanguageDictionary::default(), @@ -90,12 +90,7 @@ where if next_item.is_none() { is_stmt_done = true; } - match next_item { - Some(item) => { - Some(item.map_err(|e| box_error_with_ctx(e, self.tokenizer.current_line()))) - } - None => None, - } + next_item.map(|item| item.map_err(|e| box_error_with_ctx(e, self.tokenizer.current_line()))) } else { /*if self.tokenizer.end_of_file() { return None; @@ -110,28 +105,23 @@ where Ok(_) => {} Err(x) => return Some(Err(x)), } - if self.tokenizer.end_of_file() && self.buffer.len() == 0 { + if self.tokenizer.end_of_file() && self.buffer.is_empty() { return None; } let stmt = self.vocabulary.try_build_statement(&mut self.buffer); match stmt { Ok(mut stmt) => { #[cfg(debug_assertions)] - if self.buffer.len() != 0 { + if !self.buffer.is_empty() { panic!("Token buffer was not emptied! (rem: {:?})", self.buffer) } - stmt.enter(self.context.take().unwrap_or_else(|| MpsContext::default())); + stmt.enter(self.context.take().unwrap_or_default()); self.current_stmt = Some(stmt); let next_item = self.current_stmt.as_mut().unwrap().next(); if next_item.is_none() { is_stmt_done = true; } - match next_item { - Some(item) => Some( - item.map_err(|e| box_error_with_ctx(e, self.tokenizer.current_line())), - ), - None => None, - } + next_item.map(|item| item.map_err(|e| box_error_with_ctx(e, self.tokenizer.current_line()))) } Err(e) => { Some(Err(e).map_err(|e| box_error_with_ctx(e, self.tokenizer.current_line()))) diff --git a/mps-interpreter/src/lang/filter.rs b/mps-interpreter/src/lang/filter.rs index f635455..0585941 100644 --- a/mps-interpreter/src/lang/filter.rs +++ b/mps-interpreter/src/lang/filter.rs @@ -295,7 +295,7 @@ impl Iterator for MpsFilterStatement

{ } maybe_result } - Err(e) => return Some(Err(e)), + Err(e) => Some(Err(e)), }, VariableOrOp::Variable(variable_name) => { let mut variable = match self @@ -303,13 +303,13 @@ impl Iterator for MpsFilterStatement

{ .as_mut() .unwrap() .variables - .remove(&variable_name, &mut op_getter) + .remove(variable_name, &mut op_getter) { Ok(MpsType::Op(op)) => op, Ok(x) => { return Some(Err(RuntimeError { line: 0, - op: (Box::new(self_clone2.clone()) as Box).into(), + op: (Box::new(self_clone2) as Box).into(), msg: format!( "Expected operation/iterable type in variable {}, got {}", &variable_name, x @@ -353,11 +353,11 @@ impl Iterator for MpsFilterStatement

{ self.context = Some(variable.escape()); } match self.context.as_mut().unwrap().variables.declare( - &variable_name, + variable_name, MpsType::Op(variable), &mut op_getter, ) { - Err(e) => return Some(Err(e)), + Err(e) => Some(Err(e)), Ok(_) => maybe_result, } } @@ -405,7 +405,7 @@ impl + 'static> BoxedMps // single filter let tokens2: VecDeque<&MpsToken> = VecDeque::from_iter(tokens.range(start_of_predicate..tokens_len - 1)); - if tokens2.len() != 0 && check_name("if", &tokens2[0]) { + if !tokens2.is_empty() && check_name("if", tokens2[0]) { // replacement filter if let Some(colon_location) = first_colon2(&tokens2) { let tokens3 = VecDeque::from_iter(tokens.range( @@ -543,11 +543,7 @@ fn last_open_bracket_is_after_dot(tokens: &VecDeque) -> bool { inside_brackets -= 1; } } else if open_bracket_found { - if tokens[i].is_dot() { - return true; - } else { - return false; - } + return tokens[i].is_dot() } } false diff --git a/mps-interpreter/src/lang/filter_replace.rs b/mps-interpreter/src/lang/filter_replace.rs index 558a110..619c36f 100644 --- a/mps-interpreter/src/lang/filter_replace.rs +++ b/mps-interpreter/src/lang/filter_replace.rs @@ -166,7 +166,7 @@ impl Iterator for MpsFilterReplaceStatement

.as_mut() .unwrap() .variables - .remove(&variable_name, &mut op_getter) + .remove(variable_name, &mut op_getter) { Ok(MpsType::Op(op)) => op, Ok(x) => { @@ -186,7 +186,7 @@ impl Iterator for MpsFilterReplaceStatement

let item = variable.next(); self.context = Some(variable.escape()); match self.context.as_mut().unwrap().variables.declare( - &variable_name, + variable_name, MpsType::Op(variable), &mut op_getter, ) { @@ -228,7 +228,7 @@ impl Iterator for MpsFilterReplaceStatement

Ok(_) => {} } } - while let Some(item) = real_op.next() { + for item in real_op.by_ref() { self.item_cache.push_back(item); } self.context = Some(real_op.escape()); @@ -275,7 +275,7 @@ impl Iterator for MpsFilterReplaceStatement

Ok(_) => {} } } - while let Some(item) = real_op.next() { + for item in real_op.by_ref() { self.item_cache.push_back(item); } self.context = Some(real_op.escape()); @@ -303,7 +303,7 @@ impl Iterator for MpsFilterReplaceStatement

Some(Ok(item)) } } - Err(e) => return Some(Err(e)), + Err(e) => Some(Err(e)), } } Some(Err(e)) => Some(Err(e)), diff --git a/mps-interpreter/src/lang/pseudo_op.rs b/mps-interpreter/src/lang/pseudo_op.rs index 6055d97..b8e160b 100644 --- a/mps-interpreter/src/lang/pseudo_op.rs +++ b/mps-interpreter/src/lang/pseudo_op.rs @@ -36,8 +36,8 @@ impl PseudoOp { pub fn unwrap_real(self) -> Result, RuntimeError> { match self { Self::Real(op) => { - let result = Ok(op); - result + + Ok(op) } Self::Fake(_) => Err(RuntimeError { line: 0, diff --git a/mps-interpreter/src/lang/repeated_meme.rs b/mps-interpreter/src/lang/repeated_meme.rs index b0becde..b9a82a0 100644 --- a/mps-interpreter/src/lang/repeated_meme.rs +++ b/mps-interpreter/src/lang/repeated_meme.rs @@ -43,7 +43,7 @@ pub fn repeated_tokens) -> Result RepeatedTokens { pattern_ingest: ingestor, separator_ingest: move |tokens| { - if tokens.len() > 0 && check_token_raw(separator.clone(), &tokens[0]) { + if !tokens.is_empty() && check_token_raw(separator.clone(), &tokens[0]) { assert_token_raw(separator.clone(), tokens)?; Ok(true) } else { diff --git a/mps-interpreter/src/lang/single_op.rs b/mps-interpreter/src/lang/single_op.rs index b0c8142..b686cd6 100644 --- a/mps-interpreter/src/lang/single_op.rs +++ b/mps-interpreter/src/lang/single_op.rs @@ -16,7 +16,7 @@ impl SingleItem { pub fn new(item: Result) -> Self { Self { context: None, - item: item, + item, is_complete: false, } } diff --git a/mps-interpreter/src/lang/utility.rs b/mps-interpreter/src/lang/utility.rs index 835696e..68dabc1 100644 --- a/mps-interpreter/src/lang/utility.rs +++ b/mps-interpreter/src/lang/utility.rs @@ -23,7 +23,7 @@ pub fn assert_token Option>( } else { Err(SyntaxError { line: 0, - token: token, + token, got: Some(result), }) } @@ -46,7 +46,7 @@ pub fn assert_token_raw( } else { Err(SyntaxError { line: 0, - token: token, + token, got: Some(result), }) } @@ -69,7 +69,7 @@ pub fn assert_token_raw_back( } else { Err(SyntaxError { line: 0, - token: token, + token, got: Some(result), }) } diff --git a/mps-interpreter/src/lang/vocabulary/comment.rs b/mps-interpreter/src/lang/vocabulary/comment.rs index 4355754..bd7ed63 100644 --- a/mps-interpreter/src/lang/vocabulary/comment.rs +++ b/mps-interpreter/src/lang/vocabulary/comment.rs @@ -81,7 +81,7 @@ impl SimpleMpsOpFactory for CommentStatementFactory { tokens, )?; Ok(CommentStatement { - comment: comment, + comment, context: None, }) } diff --git a/mps-interpreter/src/lang/vocabulary/files.rs b/mps-interpreter/src/lang/vocabulary/files.rs index ab06a04..1a73aa2 100644 --- a/mps-interpreter/src/lang/vocabulary/files.rs +++ b/mps-interpreter/src/lang/vocabulary/files.rs @@ -56,7 +56,7 @@ impl std::clone::Clone for FilesStatement { context: None, folder: self.folder.clone(), regex: self.regex.clone(), - recursive: self.recursive.clone(), + recursive: self.recursive, file_iter: None, has_tried: self.has_tried, } @@ -86,7 +86,7 @@ impl Iterator for FilesStatement { }); } match self.file_iter.as_mut().unwrap().next() { - Some(Ok(item)) => Some(Ok(item.into())), + Some(Ok(item)) => Some(Ok(item)), Some(Err(e)) => Some(Err(RuntimeError { line: 0, op: (Box::new(self.clone()) as Box).into(), @@ -134,7 +134,7 @@ impl MpsFunctionFactory for FilesFunctionFactory { let mut root_path = None; let mut pattern = None; let mut recursive = None; - if tokens.len() != 0 { + if !tokens.is_empty() { if tokens[0].is_literal() { // folder is specified without keyword root_path = Some(assert_token( @@ -232,7 +232,7 @@ impl MpsFunctionFactory for FilesFunctionFactory { context: None, folder: root_path, regex: pattern, - recursive: recursive, + recursive, file_iter: None, has_tried: false, }) diff --git a/mps-interpreter/src/lang/vocabulary/filters/empty_filter.rs b/mps-interpreter/src/lang/vocabulary/filters/empty_filter.rs index 6d6b5ec..25a2b64 100644 --- a/mps-interpreter/src/lang/vocabulary/filters/empty_filter.rs +++ b/mps-interpreter/src/lang/vocabulary/filters/empty_filter.rs @@ -41,7 +41,7 @@ pub struct EmptyFilterFactory; impl MpsFilterFactory for EmptyFilterFactory { fn is_filter(&self, tokens: &VecDeque<&MpsToken>) -> bool { - tokens.len() == 0 + tokens.is_empty() } fn build_filter( diff --git a/mps-interpreter/src/lang/vocabulary/filters/field_filter.rs b/mps-interpreter/src/lang/vocabulary/filters/field_filter.rs index 393998b..4dd62c5 100644 --- a/mps-interpreter/src/lang/vocabulary/filters/field_filter.rs +++ b/mps-interpreter/src/lang/vocabulary/filters/field_filter.rs @@ -68,7 +68,7 @@ impl MpsFilterPredicate for FieldFilter { op: &mut OpGetter, ) -> Result { let variable = match &self.val { - VariableOrValue::Variable(name) => match ctx.variables.get(&name, op)? { + VariableOrValue::Variable(name) => match ctx.variables.get(name, op)? { MpsType::Primitive(t) => Ok(t), _ => Err(RuntimeError { line: 0, @@ -131,12 +131,12 @@ impl MpsFilterFactory for FieldFilterFactory { (tokens_len == 3 // field > variable OR field < variable && tokens[0].is_name() && (tokens[1].is_open_angle_bracket() || tokens[1].is_close_angle_bracket()) - && (tokens[2].is_name() || check_is_type(&tokens[2]))) + && (tokens[2].is_name() || check_is_type(tokens[2]))) || (tokens_len == 4 // field >= variable OR field <= variable OR field != variable && tokens[0].is_name() && (tokens[1].is_open_angle_bracket() || tokens[1].is_close_angle_bracket() || tokens[1].is_equals() || tokens[1].is_exclamation()) && tokens[2].is_equals() - && (tokens[3].is_name() || check_is_type(&tokens[3]))) + && (tokens[3].is_name() || check_is_type(tokens[3]))) } fn build_filter( diff --git a/mps-interpreter/src/lang/vocabulary/filters/field_filter_maybe.rs b/mps-interpreter/src/lang/vocabulary/filters/field_filter_maybe.rs index 1db4eaf..7341416 100644 --- a/mps-interpreter/src/lang/vocabulary/filters/field_filter_maybe.rs +++ b/mps-interpreter/src/lang/vocabulary/filters/field_filter_maybe.rs @@ -17,13 +17,13 @@ impl MpsFilterFactory for FieldFilterMaybeFactory { && tokens[0].is_name() && (tokens[1].is_interrogation() || tokens[1].is_exclamation()) && (tokens[2].is_open_angle_bracket() || tokens[2].is_close_angle_bracket()) - && (tokens[3].is_name() || check_is_type(&tokens[3]))) + && (tokens[3].is_name() || check_is_type(tokens[3]))) || (tokens_len == 5 // field >= variable OR field <= variable OR field != variable && tokens[0].is_name() && (tokens[1].is_interrogation() || tokens[1].is_exclamation()) && (tokens[2].is_open_angle_bracket() || tokens[2].is_close_angle_bracket() || tokens[2].is_equals() || tokens[2].is_exclamation()) && tokens[3].is_equals() - && (tokens[4].is_name() || check_is_type(&tokens[4]))) + && (tokens[4].is_name() || check_is_type(tokens[4]))) } fn build_filter( diff --git a/mps-interpreter/src/lang/vocabulary/filters/field_like_filter.rs b/mps-interpreter/src/lang/vocabulary/filters/field_like_filter.rs index 6e50852..6a5b43e 100644 --- a/mps-interpreter/src/lang/vocabulary/filters/field_like_filter.rs +++ b/mps-interpreter/src/lang/vocabulary/filters/field_like_filter.rs @@ -37,7 +37,7 @@ impl MpsFilterPredicate for FieldLikeFilter { op: &mut OpGetter, ) -> Result { let variable = match &self.val { - VariableOrValue::Variable(name) => match ctx.variables.get(&name, op)? { + VariableOrValue::Variable(name) => match ctx.variables.get(name, op)? { MpsType::Primitive(MpsTypePrimitive::String(s)) => Ok(s), _ => Err(RuntimeError { line: 0, @@ -50,7 +50,7 @@ impl MpsFilterPredicate for FieldLikeFilter { _ => Err(RuntimeError { line: 0, op: op(), - msg: format!("Value is not type String"), + msg: "Value is not type String".to_string(), }), }?; if let Some(field) = music_item_lut.field(&self.field_name) { @@ -85,12 +85,12 @@ impl MpsFilterFactory for FieldLikeFilterFactory { let tokens_len = tokens.len(); (tokens_len == 3 // field like variable && tokens[0].is_name() - && check_name("like", &tokens[1]) + && check_name("like", tokens[1]) && (tokens[2].is_name() || tokens[2].is_literal())) || (tokens_len == 4 // field? like variable OR field! like variable && tokens[0].is_name() && (tokens[1].is_interrogation() || tokens[1].is_exclamation()) - && check_name("like", &tokens[2]) + && check_name("like", tokens[2]) && (tokens[3].is_name() || tokens[3].is_literal())) } diff --git a/mps-interpreter/src/lang/vocabulary/filters/index_filter.rs b/mps-interpreter/src/lang/vocabulary/filters/index_filter.rs index 0073587..bf56422 100644 --- a/mps-interpreter/src/lang/vocabulary/filters/index_filter.rs +++ b/mps-interpreter/src/lang/vocabulary/filters/index_filter.rs @@ -81,8 +81,8 @@ pub struct IndexFilterFactory; impl MpsFilterFactory for IndexFilterFactory { fn is_filter(&self, tokens: &VecDeque<&MpsToken>) -> bool { - (tokens.len() == 1 && Lookup::check_is(&tokens[0])) - || (tokens.len() == 2 && tokens[0].is_exclamation() && Lookup::check_is(&tokens[1])) + (tokens.len() == 1 && Lookup::check_is(tokens[0])) + || (tokens.len() == 2 && tokens[0].is_exclamation() && Lookup::check_is(tokens[1])) } fn build_filter( diff --git a/mps-interpreter/src/lang/vocabulary/filters/range_filter.rs b/mps-interpreter/src/lang/vocabulary/filters/range_filter.rs index 298013b..fcefe78 100644 --- a/mps-interpreter/src/lang/vocabulary/filters/range_filter.rs +++ b/mps-interpreter/src/lang/vocabulary/filters/range_filter.rs @@ -120,33 +120,33 @@ impl MpsFilterFactory for RangeFilterFactory { ) || (tokens.len() == 3 && (( // ..number - tokens[0].is_dot() && tokens[1].is_dot() && Lookup::check_is(&tokens[2]) + tokens[0].is_dot() && tokens[1].is_dot() && Lookup::check_is(tokens[2]) ) || ( // number.. - Lookup::check_is(&tokens[0]) && tokens[1].is_dot() && tokens[2].is_dot() + Lookup::check_is(tokens[0]) && tokens[1].is_dot() && tokens[2].is_dot() ))) || (tokens.len() == 4 && (( // number..number - Lookup::check_is(&tokens[0]) + Lookup::check_is(tokens[0]) && tokens[1].is_dot() && tokens[2].is_dot() - && Lookup::check_is(&tokens[3]) + && Lookup::check_is(tokens[3]) ) || ( // ..=number tokens[0].is_dot() && tokens[1].is_dot() && tokens[2].is_equals() - && Lookup::check_is(&tokens[3]) + && Lookup::check_is(tokens[3]) ))) || ( // number..=number tokens.len() == 5 - && Lookup::check_is(&tokens[0]) + && Lookup::check_is(tokens[0]) && tokens[1].is_dot() && tokens[2].is_dot() && tokens[3].is_equals() - && Lookup::check_is(&tokens[4]) + && Lookup::check_is(tokens[4]) ) } @@ -180,8 +180,8 @@ impl MpsFilterFactory for RangeFilterFactory { }; Ok(RangeFilter { - start: start, - end: end, + start, + end, inclusive_end: equals_at_end, current: 0, complete: false, diff --git a/mps-interpreter/src/lang/vocabulary/filters/utility.rs b/mps-interpreter/src/lang/vocabulary/filters/utility.rs index ccc3e7a..7e8f179 100644 --- a/mps-interpreter/src/lang/vocabulary/filters/utility.rs +++ b/mps-interpreter/src/lang/vocabulary/filters/utility.rs @@ -8,7 +8,7 @@ pub fn assert_comparison_operator(tokens: &mut VecDeque) -> Result<[i8 let token1 = tokens.pop_front().unwrap(); match token1 { MpsToken::Equals => { - if tokens.len() != 0 && tokens[0].is_equals() { + if !tokens.is_empty() && tokens[0].is_equals() { // tokens: == assert_token_raw(MpsToken::Equals, tokens)?; Ok([0, 0]) @@ -16,7 +16,7 @@ pub fn assert_comparison_operator(tokens: &mut VecDeque) -> Result<[i8 Err(SyntaxError { line: 0, token: MpsToken::Equals, - got: if tokens.len() != 0 { + got: if !tokens.is_empty() { Some(tokens[0].clone()) } else { None @@ -25,7 +25,7 @@ pub fn assert_comparison_operator(tokens: &mut VecDeque) -> Result<[i8 } } MpsToken::OpenAngleBracket => { - if tokens.len() != 0 && tokens[0].is_equals() { + if !tokens.is_empty() && tokens[0].is_equals() { // tokens: <= assert_token_raw(MpsToken::Equals, tokens)?; Ok([0, -1]) @@ -35,7 +35,7 @@ pub fn assert_comparison_operator(tokens: &mut VecDeque) -> Result<[i8 } } MpsToken::CloseAngleBracket => { - if tokens.len() != 0 && tokens[0].is_equals() { + if !tokens.is_empty() && tokens[0].is_equals() { // tokens: >= assert_token_raw(MpsToken::Equals, tokens)?; Ok([0, 1]) diff --git a/mps-interpreter/src/lang/vocabulary/repeat.rs b/mps-interpreter/src/lang/vocabulary/repeat.rs index d98dee5..5afc6a0 100644 --- a/mps-interpreter/src/lang/vocabulary/repeat.rs +++ b/mps-interpreter/src/lang/vocabulary/repeat.rs @@ -69,7 +69,7 @@ impl Iterator for RepeatStatement { } if real_op.is_resetable() { while self.loop_forever || !self.inner_done { - while let Some(item) = real_op.next() { + for item in real_op.by_ref() { return Some(item); } if !self.loop_forever { @@ -124,28 +124,26 @@ impl Iterator for RepeatStatement { // inner is done if self.repetitions == 0 && !self.loop_forever { None - } else { - if self.cache.len() == 0 { - if self.loop_forever { - Some(Err(RuntimeError { - line: 0, - op: (Box::new(self.clone()) as Box).into(), - msg: "Cannot repeat nothing".into(), - })) - } else { - None - } + } else if self.cache.is_empty() { + if self.loop_forever { + Some(Err(RuntimeError { + line: 0, + op: (Box::new(self.clone()) as Box).into(), + msg: "Cannot repeat nothing".into(), + })) } else { - let music_item = self.cache[self.cache_position].clone(); - self.cache_position += 1; - if self.cache_position == self.cache.len() { - if self.repetitions != 0 { - self.repetitions -= 1; - } - self.cache_position = 0; - } - Some(Ok(music_item)) + None } + } else { + let music_item = self.cache[self.cache_position].clone(); + self.cache_position += 1; + if self.cache_position == self.cache.len() { + if self.repetitions != 0 { + self.repetitions -= 1; + } + self.cache_position = 0; + } + Some(Ok(music_item)) } } } @@ -183,19 +181,17 @@ impl MpsOp for RepeatStatement { self.repetitions = self.original_repetitions - 1; self.inner_done = false; } + } else if self.inner_done { + self.repetitions = self.original_repetitions; + self.cache_position = 0; } else { - if self.inner_done { - self.repetitions = self.original_repetitions; - self.cache_position = 0; - } else { - return Err(RuntimeError { - line: 0, - op: PseudoOp::from_printable(self), - msg: - "Cannot reset part way through repeat when inner statement is not resetable" - .to_string(), - }); - } + return Err(RuntimeError { + line: 0, + op: PseudoOp::from_printable(self), + msg: + "Cannot reset part way through repeat when inner statement is not resetable" + .to_string(), + }); } Ok(()) } @@ -220,7 +216,7 @@ impl MpsFunctionFactory for RepeatFunctionFactory { tokens.extend(end_tokens); let mut count: Option = None; let mut inner_done = false; - if tokens.len() != 0 { + if !tokens.is_empty() { // repititions specified assert_token_raw(MpsToken::Comma, tokens)?; count = Some(assert_token( @@ -244,13 +240,13 @@ impl MpsFunctionFactory for RepeatFunctionFactory { } Ok(RepeatStatement { inner_statement: inner_statement.into(), - inner_done: inner_done, + inner_done, context: None, cache: Vec::new(), cache_position: 0, repetitions: count.unwrap_or(0), loop_forever: count.is_none(), - original_repetitions: count.and_then(|c| Some(c + 1)).unwrap_or(0), + original_repetitions: count.map(|c| c + 1).unwrap_or(0), }) } } diff --git a/mps-interpreter/src/lang/vocabulary/sorters/bliss_next_sorter.rs b/mps-interpreter/src/lang/vocabulary/sorters/bliss_next_sorter.rs index 4d52f46..582dd41 100644 --- a/mps-interpreter/src/lang/vocabulary/sorters/bliss_next_sorter.rs +++ b/mps-interpreter/src/lang/vocabulary/sorters/bliss_next_sorter.rs @@ -31,13 +31,11 @@ impl BlissNextSorter { fn get_maybe(&mut self, op: &mut OpGetter) -> Option { if self.algorithm_done { None + } else if let Ok(Some(item)) = self.rx.as_ref().unwrap().recv() { + Some(item.map_err(|e| bliss_err(e, op))) } else { - if let Ok(Some(item)) = self.rx.as_ref().unwrap().recv() { - Some(item.map_err(|e| bliss_err(e, op))) - } else { - self.algorithm_done = true; - None - } + self.algorithm_done = true; + None } } @@ -127,7 +125,7 @@ impl BlissNextSorter { impl std::clone::Clone for BlissNextSorter { fn clone(&self) -> Self { Self { - up_to: self.up_to.clone(), + up_to: self.up_to, rx: None, algorithm_done: self.algorithm_done, } @@ -205,7 +203,7 @@ pub struct BlissNextSorterFactory; impl MpsSorterFactory for BlissNextSorterFactory { fn is_sorter(&self, tokens: &VecDeque<&MpsToken>) -> bool { - tokens.len() == 2 && check_name("advanced", &tokens[0]) && check_name("bliss_next", &tokens[1]) + tokens.len() == 2 && check_name("advanced", tokens[0]) && check_name("bliss_next", tokens[1]) } fn build_sorter( diff --git a/mps-interpreter/src/lang/vocabulary/sorters/bliss_sorter.rs b/mps-interpreter/src/lang/vocabulary/sorters/bliss_sorter.rs index 3a84626..ff85c33 100644 --- a/mps-interpreter/src/lang/vocabulary/sorters/bliss_sorter.rs +++ b/mps-interpreter/src/lang/vocabulary/sorters/bliss_sorter.rs @@ -74,7 +74,7 @@ impl BlissSorter { impl std::clone::Clone for BlissSorter { fn clone(&self) -> Self { Self { - up_to: self.up_to.clone(), + up_to: self.up_to, float_map: self.float_map.clone(), first_song: self.first_song.clone(), rx: None, @@ -210,7 +210,7 @@ pub struct BlissSorterFactory; impl MpsSorterFactory for BlissSorterFactory { fn is_sorter(&self, tokens: &VecDeque<&MpsToken>) -> bool { - tokens.len() == 2 && check_name("advanced", &tokens[0]) && check_name("bliss_first", &tokens[1]) + tokens.len() == 2 && check_name("advanced", tokens[0]) && check_name("bliss_first", tokens[1]) } fn build_sorter( diff --git a/mps-interpreter/src/lang/vocabulary/sorters/empty_sorter.rs b/mps-interpreter/src/lang/vocabulary/sorters/empty_sorter.rs index d649259..f2a2cc7 100644 --- a/mps-interpreter/src/lang/vocabulary/sorters/empty_sorter.rs +++ b/mps-interpreter/src/lang/vocabulary/sorters/empty_sorter.rs @@ -34,7 +34,7 @@ pub struct EmptySorterFactory; impl MpsSorterFactory for EmptySorterFactory { fn is_sorter(&self, tokens: &VecDeque<&MpsToken>) -> bool { - tokens.len() == 0 + tokens.is_empty() } fn build_sorter( diff --git a/mps-interpreter/src/lang/vocabulary/sql_query.rs b/mps-interpreter/src/lang/vocabulary/sql_query.rs index 251552a..dec1aa2 100644 --- a/mps-interpreter/src/lang/vocabulary/sql_query.rs +++ b/mps-interpreter/src/lang/vocabulary/sql_query.rs @@ -34,7 +34,7 @@ impl SqlStatement { } else { //Some(rows[self.current].clone()) match &rows[self.current] { - Ok(item) => Some(Ok(item.clone().into())), + Ok(item) => Some(Ok(item.clone())), Err(e) => Some(Err(RuntimeError { line: e.line, op: (Box::new(self.clone()) as Box).into(), @@ -46,7 +46,7 @@ impl SqlStatement { Some(Err(RuntimeError { line: 0, op: (Box::new(self.clone()) as Box).into(), - msg: format!("Context error: rows is None").into(), + msg: "Context error: rows is None".to_string(), })) } } @@ -99,7 +99,7 @@ impl Iterator for SqlStatement { }) { Err(e) => { self.rows = Some(Vec::with_capacity(0)); - return Some(Err(e)); + Some(Err(e)) } Ok(rows) => { self.rows = Some(rows); diff --git a/mps-interpreter/src/lang/vocabulary/sql_simple_query.rs b/mps-interpreter/src/lang/vocabulary/sql_simple_query.rs index 5d1bd17..8b5a350 100644 --- a/mps-interpreter/src/lang/vocabulary/sql_simple_query.rs +++ b/mps-interpreter/src/lang/vocabulary/sql_simple_query.rs @@ -83,7 +83,7 @@ impl SimpleSqlStatement { } else { //Some(rows[self.current].clone()) match &rows[self.current] { - Ok(item) => Some(Ok(item.clone().into())), + Ok(item) => Some(Ok(item.clone())), Err(e) => Some(Err(RuntimeError { line: e.line, op: (Box::new(self.clone()) as Box).into(), @@ -95,7 +95,7 @@ impl SimpleSqlStatement { Some(Err(RuntimeError { line: 0, op: (Box::new(self.clone()) as Box).into(), - msg: format!("Context error: rows is None").into(), + msg: "Context error: rows is None".to_string(), })) } } @@ -161,7 +161,7 @@ impl Iterator for SimpleSqlStatement { match query_result { Err(e) => { self.rows = Some(Vec::with_capacity(0)); - return Some(Err(e)); + Some(Err(e)) } Ok(rows) => { self.rows = Some(rows); diff --git a/mps-interpreter/src/music/tag.rs b/mps-interpreter/src/music/tag.rs index 4669c0b..a1e182a 100644 --- a/mps-interpreter/src/music/tag.rs +++ b/mps-interpreter/src/music/tag.rs @@ -33,8 +33,7 @@ impl Tags { self.data .get("TITLE") .unwrap_or(&TagType::Unknown) - .str() - .and_then(|s| Some(s.to_string())) + .str().map(|s| s.to_string()) .unwrap_or_else(|| self.default_title()) } @@ -47,8 +46,7 @@ impl Tags { .unwrap_or(""); self.filename .file_name() - .and_then(|file| file.to_str()) - .and_then(|file| Some(file.replacen(&format!(".{}", extension), "", 1))) + .and_then(|file| file.to_str()).map(|file| file.replacen(&format!(".{}", extension), "", 1)) .unwrap_or("Unknown Title".into()) } @@ -57,8 +55,7 @@ impl Tags { self.data .get("ARTIST") .unwrap_or(&TagType::Unknown) - .str() - .and_then(|s| Some(s.to_string())) + .str().map(|s| s.to_string()) } #[inline] @@ -66,8 +63,7 @@ impl Tags { self.data .get("ALBUM") .unwrap_or(&TagType::Unknown) - .str() - .and_then(|s| Some(s.to_string())) + .str().map(|s| s.to_string()) } #[inline] @@ -75,8 +71,7 @@ impl Tags { self.data .get("GENRE") .unwrap_or(&TagType::Unknown) - .str() - .and_then(|s| Some(s.to_string())) + .str().map(|s| s.to_string()) } #[inline] @@ -233,7 +228,7 @@ impl TagType { fn str(&self) -> Option<&str> { match self { - Self::Str(s) => Some(&s), + Self::Str(s) => Some(s), _ => None, } } diff --git a/mps-interpreter/src/processing/filesystem.rs b/mps-interpreter/src/processing/filesystem.rs index b8389aa..bfc3fb9 100644 --- a/mps-interpreter/src/processing/filesystem.rs +++ b/mps-interpreter/src/processing/filesystem.rs @@ -35,7 +35,7 @@ impl Iterator for SortedReadDir { fn next(&mut self) -> Option { if !self.dir_iter_complete { - while let Some(dir) = self.dir_iter.next() { + for dir in self.dir_iter.by_ref() { match dir { Ok(f) => self.cache.push(f), Err(e) => return Some(Err(e)), @@ -226,15 +226,14 @@ impl FileIter { item: &mut MpsItem, path_str: &str, captures: Option, - mut capture_names: regex::CaptureNames, + capture_names: regex::CaptureNames, ) { // populates fields from named capture groups if let Some(captures) = captures { - while let Some(name_maybe) = capture_names.next() { + for name_maybe in capture_names { if let Some(name) = name_maybe { if let Some(value) = captures - .name(name) - .and_then(|m| Some(m.as_str().to_string())) + .name(name).map(|m| m.as_str().to_string()) { item.set_field(name, MpsTypePrimitive::parse(value)); } @@ -260,62 +259,55 @@ impl Iterator for FileIter { fn next(&mut self) -> Option { if self.is_complete { None - } else { - if self.dir_iters.is_empty() { - if self.root.is_file() { - self.is_complete = true; - match self.build_item(&self.root) { - None => None, - Some(item) => Some(Ok(item)), - } - } else { - self.dir_iters.push(match self.root.read_dir() { - Ok(x) => x.into(), - Err(e) => { - self.is_complete = true; - return Some(Err(format!("Directory read error: {}", e))); - } - }); - return self.next(); - } - } else { - while !self.dir_iters.is_empty() { - let mut dir_iter = self.dir_iters.pop().unwrap(); - 'inner: while let Some(path_result) = dir_iter.next() { - match path_result { - Ok(dir_entry) => { - if dir_entry.path().is_dir() { - if self.recursive { - self.dir_iters.push(dir_iter); - self.dir_iters.push(match dir_entry.path().read_dir() { - Ok(x) => x.into(), - Err(e) => { - return Some(Err(format!( - "Directory read error: {}", - e - ))) - } - }); - //return self.next(); - break 'inner; - } - } else { - if let Some(item) = self.build_item(dir_entry.path()) { - self.dir_iters.push(dir_iter); - return Some(Ok(item)); - } - } - }, - Err(e) => { - self.dir_iters.push(dir_iter); - return Some(Err(format!("Path read error: {}", e))); - } - } - } - } + } else if self.dir_iters.is_empty() { + if self.root.is_file() { self.is_complete = true; - None + self.build_item(&self.root).map(Ok) + } else { + self.dir_iters.push(match self.root.read_dir() { + Ok(x) => x.into(), + Err(e) => { + self.is_complete = true; + return Some(Err(format!("Directory read error: {}", e))); + } + }); + self.next() } + } else { + while !self.dir_iters.is_empty() { + let mut dir_iter = self.dir_iters.pop().unwrap(); + 'inner: while let Some(path_result) = dir_iter.next() { + match path_result { + Ok(dir_entry) => { + if dir_entry.path().is_dir() { + if self.recursive { + self.dir_iters.push(dir_iter); + self.dir_iters.push(match dir_entry.path().read_dir() { + Ok(x) => x.into(), + Err(e) => { + return Some(Err(format!( + "Directory read error: {}", + e + ))) + } + }); + //return self.next(); + break 'inner; + } + } else if let Some(item) = self.build_item(dir_entry.path()) { + self.dir_iters.push(dir_iter); + return Some(Ok(item)); + } + }, + Err(e) => { + self.dir_iters.push(dir_iter); + return Some(Err(format!("Path read error: {}", e))); + } + } + } + } + self.is_complete = true; + None } } } diff --git a/mps-interpreter/src/processing/sql.rs b/mps-interpreter/src/processing/sql.rs index 89d471d..c0714ac 100644 --- a/mps-interpreter/src/processing/sql.rs +++ b/mps-interpreter/src/processing/sql.rs @@ -45,7 +45,7 @@ pub struct MpsSQLiteExecutor { impl MpsSQLiteExecutor { #[inline] fn gen_db_maybe(&mut self, op: &mut QueryOp) -> Result<(), RuntimeError> { - if let None = self.sqlite_connection { + if self.sqlite_connection.is_none() { // connection needs to be created match generate_default_db() { Ok(conn) => { @@ -55,7 +55,7 @@ impl MpsSQLiteExecutor { return Err(RuntimeError { line: 0, op: op(), - msg: format!("SQL connection error: {}", e).into(), + msg: format!("SQL connection error: {}", e), }) } } @@ -78,7 +78,7 @@ impl MpsSQLiteExecutor { item.map_err(|e| RuntimeError { line: 0, op: op(), - msg: format!("SQL item mapping error: {}", e).into(), + msg: format!("SQL item mapping error: {}", e), }) }) .collect()), @@ -103,7 +103,7 @@ impl MpsDatabaseQuerier for MpsSQLiteExecutor { item.map_err(|e| RuntimeError { line: 0, op: op(), - msg: format!("SQL item mapping error: {}", e).into(), + msg: format!("SQL item mapping error: {}", e), }) }) .collect()), @@ -248,8 +248,7 @@ impl std::convert::TryInto for SqliteSettings { fn try_into(self) -> Result { let music_path = self - .music_path - .and_then(|p| Some(std::path::PathBuf::from(p))) + .music_path.map(std::path::PathBuf::from) .unwrap_or_else(crate::lang::utility::music_folder); let sqlite_path = self .db_path diff --git a/mps-interpreter/src/runner.rs b/mps-interpreter/src/runner.rs index fbb9585..280e8ac 100644 --- a/mps-interpreter/src/runner.rs +++ b/mps-interpreter/src/runner.rs @@ -36,7 +36,7 @@ impl MpsRunner { interpretor.context(ctx); } Self { - interpretor: interpretor, + interpretor, new_statement: true, } } diff --git a/mps-interpreter/src/tokens/tokenizer.rs b/mps-interpreter/src/tokens/tokenizer.rs index 0706151..5512d5d 100644 --- a/mps-interpreter/src/tokens/tokenizer.rs +++ b/mps-interpreter/src/tokens/tokenizer.rs @@ -29,7 +29,7 @@ where { pub fn new(reader: R) -> Self { Self { - reader: reader, + reader, fsm: ReaderStateMachine::Start {}, line: 0, column: 0, @@ -73,7 +73,7 @@ where bigger_buf.clear(); } ReaderStateMachine::EndToken {} => { - if bigger_buf.len() != 0 { + if !bigger_buf.is_empty() { // ignore consecutive end tokens let token = String::from_utf8(bigger_buf.clone()) .map_err(|e| self.error(format!("UTF-8 encoding error: {}", e)))?; @@ -86,7 +86,7 @@ where } ReaderStateMachine::SingleCharToken { .. } => { let out = bigger_buf.pop().unwrap(); // bracket or comma token - if bigger_buf.len() != 0 { + if !bigger_buf.is_empty() { // bracket tokens can be beside other tokens, without separator let token = String::from_utf8(bigger_buf.clone()) .map_err(|e| self.error(format!("UTF-8 encoding error: {}", e)))?; @@ -118,7 +118,7 @@ where bigger_buf.clear(); buf.clear(); return match invalid_char { - 0 => Err(self.error(format!("EOF"))), + 0 => Err(self.error("EOF".to_string())), _ => Err(self.error(format!( "character {:?} ({})", invalid_char as char, invalid_char @@ -139,7 +139,7 @@ where self.fsm = self.fsm.next_state(byte_buf[0]); } // handle end statement - if bigger_buf.len() != 0 { + if !bigger_buf.is_empty() { // also end of token // note: never also end of literal, since those have explicit closing characters let token = String::from_utf8(bigger_buf.clone()) @@ -168,7 +168,7 @@ where ParseError { line: self.current_line(), column: self.current_column(), - item: item, + item, } } }