cargo fmt

This commit is contained in:
NGnius (Graham) 2022-02-03 16:56:42 -05:00
parent 1dedc29715
commit 5af9311887
7 changed files with 73 additions and 53 deletions

View file

@ -41,7 +41,10 @@ impl Display for MpsItem {
}
impl std::hash::Hash for MpsItem {
fn hash<H>(&self, state: &mut H) where H: std::hash::Hasher {
fn hash<H>(&self, state: &mut H)
where
H: std::hash::Hasher,
{
// hashing is order-dependent, so the pseudo-random sorting of HashMap keys
// prevents it from working correctly without sorting
let mut keys: Vec<_> = self.fields.keys().collect();

View file

@ -53,7 +53,10 @@ impl Display for RuntimeError {
}
impl std::hash::Hash for RuntimeError {
fn hash<H>(&self, state: &mut H) where H: std::hash::Hasher {
fn hash<H>(&self, state: &mut H)
where
H: std::hash::Hasher,
{
self.line.hash(state);
self.msg.hash(state);
}

View file

@ -145,7 +145,10 @@ impl PartialOrd for MpsTypePrimitive {
}
impl std::hash::Hash for MpsTypePrimitive {
fn hash<H>(&self, state: &mut H) where H: std::hash::Hasher {
fn hash<H>(&self, state: &mut H)
where
H: std::hash::Hasher,
{
match self {
Self::String(s) => s.hash(state),
Self::Int(i) => i.hash(state),

View file

@ -49,7 +49,8 @@ impl MpsFilterPredicate for FieldRegexFilter {
let pattern = if let Some((_, regex_c)) = &self.regex_cache {
regex_c
} else {
let regex_c = Regex::new(variable).map_err(|e| RuntimeMsg(format!("Regex compile error: {}", e)))?;
let regex_c = Regex::new(variable)
.map_err(|e| RuntimeMsg(format!("Regex compile error: {}", e)))?;
self.regex_cache = Some((variable.clone(), regex_c));
&self.regex_cache.as_ref().unwrap().1
};
@ -128,7 +129,7 @@ impl MpsFilterFactory<FieldRegexFilter> for FieldRegexFilterFactory {
let regex_c = Regex::new(&literal).map_err(|_| SyntaxError {
line: 0,
token: MpsToken::Literal("[valid regex]".to_string()),
got: Some(MpsToken::Literal(literal.clone()))
got: Some(MpsToken::Literal(literal.clone())),
})?;
let compiled_cache = (literal.clone(), regex_c);
let value = VariableOrValue::Value(MpsTypePrimitive::String(literal));

View file

@ -1,15 +1,15 @@
use std::collections::{VecDeque, HashSet};
use std::collections::{HashSet, VecDeque};
use std::fmt::{Debug, Display, Error, Formatter};
use std::iter::Iterator;
use crate::tokens::MpsToken;
use crate::MpsContext;
use crate::lang::{MpsLanguageDictionary, PseudoOp};
use crate::lang::{MpsFunctionFactory, MpsFunctionStatementFactory, MpsIteratorItem, MpsOp};
use crate::lang::{RuntimeError, SyntaxError};
use crate::lang::repeated_tokens;
use crate::lang::vocabulary::union::next_comma;
use crate::lang::{MpsFunctionFactory, MpsFunctionStatementFactory, MpsIteratorItem, MpsOp};
use crate::lang::{MpsLanguageDictionary, PseudoOp};
use crate::lang::{RuntimeError, SyntaxError};
#[derive(Debug)]
pub struct IntersectionStatement {
@ -59,7 +59,8 @@ impl Iterator for IntersectionStatement {
};
real_op.enter(self.context.take().unwrap());
let original_order: VecDeque<MpsIteratorItem> = real_op.collect();
let mut set: HashSet<MpsIteratorItem> = original_order.iter().map(|x| x.to_owned()).collect();
let mut set: HashSet<MpsIteratorItem> =
original_order.iter().map(|x| x.to_owned()).collect();
self.context = Some(real_op.escape());
if self.ops.len() != 1 && !set.is_empty() {
for i in 1..self.ops.len() {
@ -119,7 +120,6 @@ impl MpsOp for IntersectionStatement {
} else {
self.context = Some(real_op.escape());
}
}
Ok(())
}
@ -139,16 +139,20 @@ impl MpsFunctionFactory<IntersectionStatement> for IntersectionFunctionFactory {
dict: &MpsLanguageDictionary,
) -> Result<IntersectionStatement, SyntaxError> {
// intersection(op1, op2, ...)
let operations = repeated_tokens(|tokens| {
if let Some(comma_pos) = next_comma(tokens) {
let end_tokens = tokens.split_off(comma_pos);
let op = dict.try_build_statement(tokens);
tokens.extend(end_tokens);
Ok(Some(PseudoOp::from(op?)))
} else {
Ok(Some(PseudoOp::from(dict.try_build_statement(tokens)?)))
}
}, MpsToken::Comma).ingest_all(tokens)?;
let operations = repeated_tokens(
|tokens| {
if let Some(comma_pos) = next_comma(tokens) {
let end_tokens = tokens.split_off(comma_pos);
let op = dict.try_build_statement(tokens);
tokens.extend(end_tokens);
Ok(Some(PseudoOp::from(op?)))
} else {
Ok(Some(PseudoOp::from(dict.try_build_statement(tokens)?)))
}
},
MpsToken::Comma,
)
.ingest_all(tokens)?;
Ok(IntersectionStatement {
context: None,
ops: operations,
@ -159,7 +163,8 @@ impl MpsFunctionFactory<IntersectionStatement> for IntersectionFunctionFactory {
}
}
pub type IntersectionStatementFactory = MpsFunctionStatementFactory<IntersectionStatement, IntersectionFunctionFactory>;
pub type IntersectionStatementFactory =
MpsFunctionStatementFactory<IntersectionStatement, IntersectionFunctionFactory>;
#[inline(always)]
pub fn intersection_function_factory() -> IntersectionStatementFactory {

View file

@ -5,10 +5,10 @@ use std::iter::Iterator;
use crate::tokens::MpsToken;
use crate::MpsContext;
use crate::lang::{MpsLanguageDictionary, PseudoOp};
use crate::lang::{MpsFunctionFactory, MpsFunctionStatementFactory, MpsIteratorItem, MpsOp};
use crate::lang::{RuntimeError, SyntaxError};
use crate::lang::repeated_tokens;
use crate::lang::{MpsFunctionFactory, MpsFunctionStatementFactory, MpsIteratorItem, MpsOp};
use crate::lang::{MpsLanguageDictionary, PseudoOp};
use crate::lang::{RuntimeError, SyntaxError};
#[derive(Debug, Copy, Clone)]
enum UnionStrategy {
@ -52,23 +52,25 @@ impl Iterator for UnionStatement {
type Item = MpsIteratorItem;
fn next(&mut self) -> Option<Self::Item> {
if self.index == self.ops.len() {return None;}
if self.index == self.ops.len() {
return None;
}
match self.strategy {
UnionStrategy::Sequential => {
loop {
if self.index == self.ops.len() {return None;}
let real_op = match self.ops[self.index].try_real() {
Ok(x) => x,
Err(e) => return Some(Err(e)),
};
real_op.enter(self.context.take().unwrap());
while let Some(item) = real_op.next() {
self.context = Some(real_op.escape());
return Some(item);
}
self.context = Some(real_op.escape());
self.index += 1;
UnionStrategy::Sequential => loop {
if self.index == self.ops.len() {
return None;
}
let real_op = match self.ops[self.index].try_real() {
Ok(x) => x,
Err(e) => return Some(Err(e)),
};
real_op.enter(self.context.take().unwrap());
while let Some(item) = real_op.next() {
self.context = Some(real_op.escape());
return Some(item);
}
self.context = Some(real_op.escape());
self.index += 1;
},
UnionStrategy::Interleave => {
let mut none_count = 0;
@ -128,7 +130,6 @@ impl MpsOp for UnionStatement {
} else {
self.context = Some(real_op.escape());
}
}
Ok(())
}
@ -148,16 +149,20 @@ impl MpsFunctionFactory<UnionStatement> for UnionFunctionFactory {
dict: &MpsLanguageDictionary,
) -> Result<UnionStatement, SyntaxError> {
// union(op1, op2, ...)
let operations = repeated_tokens(|tokens| {
if let Some(comma_pos) = next_comma(tokens) {
let end_tokens = tokens.split_off(comma_pos);
let op = dict.try_build_statement(tokens);
tokens.extend(end_tokens);
Ok(Some(PseudoOp::from(op?)))
} else {
Ok(Some(PseudoOp::from(dict.try_build_statement(tokens)?)))
}
}, MpsToken::Comma).ingest_all(tokens)?;
let operations = repeated_tokens(
|tokens| {
if let Some(comma_pos) = next_comma(tokens) {
let end_tokens = tokens.split_off(comma_pos);
let op = dict.try_build_statement(tokens);
tokens.extend(end_tokens);
Ok(Some(PseudoOp::from(op?)))
} else {
Ok(Some(PseudoOp::from(dict.try_build_statement(tokens)?)))
}
},
MpsToken::Comma,
)
.ingest_all(tokens)?;
let combine_strategy = if name == "u" || name == "union" {
UnionStrategy::Sequential
} else {

View file

@ -422,7 +422,7 @@ fn execute_unionfn_line() -> Result<(), Box<dyn MpsLanguageError>> {
execute_single_line(
"interlace(empty(), files(`~/Music/MusicFlac/Bruno Mars/24K Magic/`))",
false,
true
true,
)
}
@ -460,6 +460,6 @@ fn execute_intersectionfn_line() -> Result<(), Box<dyn MpsLanguageError>> {
execute_single_line(
"n(empty(), files(`~/Music/MusicFlac/Bruno Mars/24K Magic/`))",
true,
true
true,
)
}