cargo fmt
This commit is contained in:
parent
1dedc29715
commit
5af9311887
7 changed files with 73 additions and 53 deletions
|
@ -41,7 +41,10 @@ impl Display for MpsItem {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl std::hash::Hash 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
|
// hashing is order-dependent, so the pseudo-random sorting of HashMap keys
|
||||||
// prevents it from working correctly without sorting
|
// prevents it from working correctly without sorting
|
||||||
let mut keys: Vec<_> = self.fields.keys().collect();
|
let mut keys: Vec<_> = self.fields.keys().collect();
|
||||||
|
|
|
@ -53,7 +53,10 @@ impl Display for RuntimeError {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl std::hash::Hash 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.line.hash(state);
|
||||||
self.msg.hash(state);
|
self.msg.hash(state);
|
||||||
}
|
}
|
||||||
|
|
|
@ -145,7 +145,10 @@ impl PartialOrd for MpsTypePrimitive {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl std::hash::Hash 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 {
|
match self {
|
||||||
Self::String(s) => s.hash(state),
|
Self::String(s) => s.hash(state),
|
||||||
Self::Int(i) => i.hash(state),
|
Self::Int(i) => i.hash(state),
|
||||||
|
|
|
@ -49,7 +49,8 @@ impl MpsFilterPredicate for FieldRegexFilter {
|
||||||
let pattern = if let Some((_, regex_c)) = &self.regex_cache {
|
let pattern = if let Some((_, regex_c)) = &self.regex_cache {
|
||||||
regex_c
|
regex_c
|
||||||
} else {
|
} 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 = Some((variable.clone(), regex_c));
|
||||||
&self.regex_cache.as_ref().unwrap().1
|
&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 {
|
let regex_c = Regex::new(&literal).map_err(|_| SyntaxError {
|
||||||
line: 0,
|
line: 0,
|
||||||
token: MpsToken::Literal("[valid regex]".to_string()),
|
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 compiled_cache = (literal.clone(), regex_c);
|
||||||
let value = VariableOrValue::Value(MpsTypePrimitive::String(literal));
|
let value = VariableOrValue::Value(MpsTypePrimitive::String(literal));
|
||||||
|
|
|
@ -1,15 +1,15 @@
|
||||||
use std::collections::{VecDeque, HashSet};
|
use std::collections::{HashSet, VecDeque};
|
||||||
use std::fmt::{Debug, Display, Error, Formatter};
|
use std::fmt::{Debug, Display, Error, Formatter};
|
||||||
use std::iter::Iterator;
|
use std::iter::Iterator;
|
||||||
|
|
||||||
use crate::tokens::MpsToken;
|
use crate::tokens::MpsToken;
|
||||||
use crate::MpsContext;
|
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::repeated_tokens;
|
||||||
use crate::lang::vocabulary::union::next_comma;
|
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)]
|
#[derive(Debug)]
|
||||||
pub struct IntersectionStatement {
|
pub struct IntersectionStatement {
|
||||||
|
@ -59,7 +59,8 @@ impl Iterator for IntersectionStatement {
|
||||||
};
|
};
|
||||||
real_op.enter(self.context.take().unwrap());
|
real_op.enter(self.context.take().unwrap());
|
||||||
let original_order: VecDeque<MpsIteratorItem> = real_op.collect();
|
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());
|
self.context = Some(real_op.escape());
|
||||||
if self.ops.len() != 1 && !set.is_empty() {
|
if self.ops.len() != 1 && !set.is_empty() {
|
||||||
for i in 1..self.ops.len() {
|
for i in 1..self.ops.len() {
|
||||||
|
@ -119,7 +120,6 @@ impl MpsOp for IntersectionStatement {
|
||||||
} else {
|
} else {
|
||||||
self.context = Some(real_op.escape());
|
self.context = Some(real_op.escape());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -139,16 +139,20 @@ impl MpsFunctionFactory<IntersectionStatement> for IntersectionFunctionFactory {
|
||||||
dict: &MpsLanguageDictionary,
|
dict: &MpsLanguageDictionary,
|
||||||
) -> Result<IntersectionStatement, SyntaxError> {
|
) -> Result<IntersectionStatement, SyntaxError> {
|
||||||
// intersection(op1, op2, ...)
|
// intersection(op1, op2, ...)
|
||||||
let operations = repeated_tokens(|tokens| {
|
let operations = repeated_tokens(
|
||||||
if let Some(comma_pos) = next_comma(tokens) {
|
|tokens| {
|
||||||
let end_tokens = tokens.split_off(comma_pos);
|
if let Some(comma_pos) = next_comma(tokens) {
|
||||||
let op = dict.try_build_statement(tokens);
|
let end_tokens = tokens.split_off(comma_pos);
|
||||||
tokens.extend(end_tokens);
|
let op = dict.try_build_statement(tokens);
|
||||||
Ok(Some(PseudoOp::from(op?)))
|
tokens.extend(end_tokens);
|
||||||
} else {
|
Ok(Some(PseudoOp::from(op?)))
|
||||||
Ok(Some(PseudoOp::from(dict.try_build_statement(tokens)?)))
|
} else {
|
||||||
}
|
Ok(Some(PseudoOp::from(dict.try_build_statement(tokens)?)))
|
||||||
}, MpsToken::Comma).ingest_all(tokens)?;
|
}
|
||||||
|
},
|
||||||
|
MpsToken::Comma,
|
||||||
|
)
|
||||||
|
.ingest_all(tokens)?;
|
||||||
Ok(IntersectionStatement {
|
Ok(IntersectionStatement {
|
||||||
context: None,
|
context: None,
|
||||||
ops: operations,
|
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)]
|
#[inline(always)]
|
||||||
pub fn intersection_function_factory() -> IntersectionStatementFactory {
|
pub fn intersection_function_factory() -> IntersectionStatementFactory {
|
||||||
|
|
|
@ -5,10 +5,10 @@ use std::iter::Iterator;
|
||||||
use crate::tokens::MpsToken;
|
use crate::tokens::MpsToken;
|
||||||
use crate::MpsContext;
|
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::repeated_tokens;
|
||||||
|
use crate::lang::{MpsFunctionFactory, MpsFunctionStatementFactory, MpsIteratorItem, MpsOp};
|
||||||
|
use crate::lang::{MpsLanguageDictionary, PseudoOp};
|
||||||
|
use crate::lang::{RuntimeError, SyntaxError};
|
||||||
|
|
||||||
#[derive(Debug, Copy, Clone)]
|
#[derive(Debug, Copy, Clone)]
|
||||||
enum UnionStrategy {
|
enum UnionStrategy {
|
||||||
|
@ -52,23 +52,25 @@ impl Iterator for UnionStatement {
|
||||||
type Item = MpsIteratorItem;
|
type Item = MpsIteratorItem;
|
||||||
|
|
||||||
fn next(&mut self) -> Option<Self::Item> {
|
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 {
|
match self.strategy {
|
||||||
UnionStrategy::Sequential => {
|
UnionStrategy::Sequential => loop {
|
||||||
loop {
|
if self.index == self.ops.len() {
|
||||||
if self.index == self.ops.len() {return None;}
|
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;
|
|
||||||
}
|
}
|
||||||
|
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 => {
|
UnionStrategy::Interleave => {
|
||||||
let mut none_count = 0;
|
let mut none_count = 0;
|
||||||
|
@ -128,7 +130,6 @@ impl MpsOp for UnionStatement {
|
||||||
} else {
|
} else {
|
||||||
self.context = Some(real_op.escape());
|
self.context = Some(real_op.escape());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -148,16 +149,20 @@ impl MpsFunctionFactory<UnionStatement> for UnionFunctionFactory {
|
||||||
dict: &MpsLanguageDictionary,
|
dict: &MpsLanguageDictionary,
|
||||||
) -> Result<UnionStatement, SyntaxError> {
|
) -> Result<UnionStatement, SyntaxError> {
|
||||||
// union(op1, op2, ...)
|
// union(op1, op2, ...)
|
||||||
let operations = repeated_tokens(|tokens| {
|
let operations = repeated_tokens(
|
||||||
if let Some(comma_pos) = next_comma(tokens) {
|
|tokens| {
|
||||||
let end_tokens = tokens.split_off(comma_pos);
|
if let Some(comma_pos) = next_comma(tokens) {
|
||||||
let op = dict.try_build_statement(tokens);
|
let end_tokens = tokens.split_off(comma_pos);
|
||||||
tokens.extend(end_tokens);
|
let op = dict.try_build_statement(tokens);
|
||||||
Ok(Some(PseudoOp::from(op?)))
|
tokens.extend(end_tokens);
|
||||||
} else {
|
Ok(Some(PseudoOp::from(op?)))
|
||||||
Ok(Some(PseudoOp::from(dict.try_build_statement(tokens)?)))
|
} else {
|
||||||
}
|
Ok(Some(PseudoOp::from(dict.try_build_statement(tokens)?)))
|
||||||
}, MpsToken::Comma).ingest_all(tokens)?;
|
}
|
||||||
|
},
|
||||||
|
MpsToken::Comma,
|
||||||
|
)
|
||||||
|
.ingest_all(tokens)?;
|
||||||
let combine_strategy = if name == "u" || name == "union" {
|
let combine_strategy = if name == "u" || name == "union" {
|
||||||
UnionStrategy::Sequential
|
UnionStrategy::Sequential
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -422,7 +422,7 @@ fn execute_unionfn_line() -> Result<(), Box<dyn MpsLanguageError>> {
|
||||||
execute_single_line(
|
execute_single_line(
|
||||||
"interlace(empty(), files(`~/Music/MusicFlac/Bruno Mars/24K Magic/`))",
|
"interlace(empty(), files(`~/Music/MusicFlac/Bruno Mars/24K Magic/`))",
|
||||||
false,
|
false,
|
||||||
true
|
true,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -460,6 +460,6 @@ fn execute_intersectionfn_line() -> Result<(), Box<dyn MpsLanguageError>> {
|
||||||
execute_single_line(
|
execute_single_line(
|
||||||
"n(empty(), files(`~/Music/MusicFlac/Bruno Mars/24K Magic/`))",
|
"n(empty(), files(`~/Music/MusicFlac/Bruno Mars/24K Magic/`))",
|
||||||
true,
|
true,
|
||||||
true
|
true,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue