Skip to content

Commit

Permalink
clippy cleanup (#181)
Browse files Browse the repository at this point in the history
  • Loading branch information
rcoh authored Apr 5, 2023
1 parent 67c56cc commit d6ccbd1
Show file tree
Hide file tree
Showing 10 changed files with 127 additions and 155 deletions.
2 changes: 1 addition & 1 deletion src/alias.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ lazy_static! {
LOADED_ALIASES.iter().map(|a| a.keyword.as_str()).collect();
}

#[derive(Debug, Deserialize, PartialEq)]
#[derive(Debug, Deserialize, PartialEq, Eq)]
pub struct AliasConfig {
keyword: String,
template: String,
Expand Down
40 changes: 18 additions & 22 deletions src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ impl serde::Serialize for WrappedAggregateRow<'_> {
}
}

#[derive(Debug, PartialEq, Clone)]
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct Aggregate {
pub columns: Vec<String>,
pub data: Vec<VMap>,
Expand All @@ -59,14 +59,14 @@ impl serde::Serialize for Aggregate {
for row in &self.data {
seq.serialize_element(&WrappedAggregateRow {
columns: &self.columns,
data: &row,
data: row,
})?
}
seq.end()
}
}

#[derive(Debug, PartialEq, Clone)]
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct Record {
pub data: VMap,
pub raw: String,
Expand Down Expand Up @@ -128,20 +128,19 @@ impl Ord for Value {
fn cmp(&self, other: &Self) -> Ordering {
match (self, other) {
// Ints and floats are converted to floats
(&Value::Int(ref int_val), &Value::Float(ref float_val)) => {
(Value::Int(int_val), Value::Float(float_val)) => {
(OrderedFloat::from(*int_val as f64)).cmp(float_val)
}
(&Value::Float(ref float_val), &Value::Int(ref int_val)) => {
(Value::Float(float_val), Value::Int(int_val)) => {
float_val.cmp(&OrderedFloat::from(*int_val as f64))
}

(&Value::Float(ref l), &Value::Float(ref r)) => l.cmp(r),
(&Value::Int(ref l), &Value::Int(ref r)) => l.cmp(r),
(&Value::Str(ref l), &Value::Str(ref r)) => l.cmp(r),
(&Value::Bool(l), &Value::Bool(r)) => l.cmp(&r),
(&Value::DateTime(l), &Value::DateTime(r)) => l.cmp(&r),
(&Value::Duration(l), &Value::Duration(r)) => l.cmp(&r),
(&Value::Obj(ref l), &Value::Obj(ref r)) => l.cmp(r),
(Value::Float(l), Value::Float(r)) => l.cmp(r),
(Value::Int(l), Value::Int(r)) => l.cmp(r),
(Value::Str(l), Value::Str(r)) => l.cmp(r),
(Value::Bool(l), Value::Bool(r)) => l.cmp(r),
(Value::DateTime(l), Value::DateTime(r)) => l.cmp(r),
(Value::Duration(l), Value::Duration(r)) => l.cmp(r),
(Value::Obj(l), Value::Obj(r)) => l.cmp(r),
// All these remaining cases aren't directly comparable
(unrelated_l, unrelated_r) => unrelated_l.rank().cmp(&unrelated_r.rank()),
}
Expand Down Expand Up @@ -318,7 +317,7 @@ impl Value {

pub fn from_float(f: f64) -> Value {
let rounded = f as i64;
if (f - f.floor()).abs() < std::f64::EPSILON {
if (f - f.floor()).abs() < f64::EPSILON {
Value::Int(rounded)
} else {
Value::Float(OrderedFloat(f))
Expand Down Expand Up @@ -429,7 +428,7 @@ impl Aggregate {
agg_column: String,
data: &[(HashMap<String, String>, Value)],
) -> Aggregate {
data.iter().for_each(|&(ref row, ref _value)| {
data.iter().for_each(|(row, _value)| {
if row.len() != key_columns.len() {
panic!("Invalid number of key columns")
}
Expand All @@ -440,10 +439,10 @@ impl Aggregate {
});
});
let raw_data: Vec<HashMap<String, Value>> = data
.into_iter()
.iter()
.map(|(keycols, value)| {
let mut new_map: HashMap<String, Value> = keycols
.into_iter()
.iter()
.map(|(keycol, val)| (keycol.clone(), Value::Str(val.clone())))
.collect();
new_map.insert(agg_column.clone(), value.clone());
Expand Down Expand Up @@ -717,13 +716,10 @@ mod tests {
Value::from_string("1,000,000"),
Value::Str("1,000,000".to_owned())
);
assert_eq!(
Value::aggressively_to_num("1,000,000"),
Ok(1_000_000 as f64)
);
assert_eq!(Value::aggressively_to_num("1,000,000"), Ok(1_000_000_f64));
assert_eq!(
Value::aggressively_to_num("1,000,000.1"),
Ok(1_000_000.1 as f64)
Ok(1_000_000.1_f64)
);
}

Expand Down
5 changes: 3 additions & 2 deletions src/errors.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::lang::{query, Positioned, Query};
use crate::pipeline::CompileError;
use annotate_snippets::snippet::{Annotation, AnnotationType, Slice, Snippet, SourceAnnotation};
use atty::Stream;
use std::env;
Expand Down Expand Up @@ -31,8 +32,8 @@ impl QueryContainer {
}

/// Parse the contained query string.
pub fn parse(&self) -> Result<Query, ()> {
query(self)
pub fn parse(&self) -> Result<Query, CompileError> {
query(self).map_err(|_| CompileError::Parse)
}
}

Expand Down
26 changes: 13 additions & 13 deletions src/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,41 +29,41 @@ mod tests {
#[test]
fn filter_and() {
let filt = Filter::And(vec![from_str("abc"), from_str("cde")]);
assert_eq!(filt.matches("abc cde"), true);
assert_eq!(filt.matches("abc"), false);
assert!(filt.matches("abc cde"));
assert!(!filt.matches("abc"));
}

#[test]
fn filter_or() {
let filt = Filter::Or(vec![from_str("abc"), from_str("cde")]);
assert_eq!(filt.matches("abc cde"), true);
assert_eq!(filt.matches("abc"), true);
assert_eq!(filt.matches("cde"), true);
assert_eq!(filt.matches("def"), false);
assert!(filt.matches("abc cde"));
assert!(filt.matches("abc"));
assert!(filt.matches("cde"));
assert!(!filt.matches("def"));
}

#[test]
fn filter_wildcard() {
let filt = Filter::And(vec![]);
assert_eq!(filt.matches("abc"), true);
assert!(filt.matches("abc"));
}

#[test]
fn filter_not() {
let filt = Filter::And(vec![from_str("abc"), from_str("cde")]);
let filt = Filter::Not(Box::new(filt));
assert_eq!(filt.matches("abc cde"), false);
assert_eq!(filt.matches("abc"), true);
assert!(!filt.matches("abc cde"));
assert!(filt.matches("abc"));
}

#[test]
fn filter_complex() {
let filt_letters = Filter::And(vec![from_str("abc"), from_str("cde")]);
let filt_numbers = Filter::And(vec![from_str("123"), from_str("456")]);
let filt = Filter::Or(vec![filt_letters, filt_numbers]);
assert_eq!(filt.matches("abc cde"), true);
assert_eq!(filt.matches("abc"), false);
assert_eq!(filt.matches("123 456"), true);
assert_eq!(filt.matches("123 cde"), false);
assert!(filt.matches("abc cde"));
assert!(!filt.matches("abc"));
assert!(filt.matches("123 456"));
assert!(!filt.matches("123 cde"));
}
}
59 changes: 22 additions & 37 deletions src/funcs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -332,38 +332,32 @@ mod tests {
fn unicode_length() {
assert_eq!(
data::Value::Int(1),
length(&vec!(data::Value::Str("\u{2603}".to_string()))).unwrap()
length(&[data::Value::Str("\u{2603}".to_string())]).unwrap()
);
}

#[test]
fn array_length() {
assert_eq!(
Ok(data::Value::Int(3)),
length(&vec!(data::Value::Array(vec!(
length(&[data::Value::Array(vec!(
data::Value::Int(0),
data::Value::Int(1),
data::Value::Int(2)
))))
))])
);
}

#[test]
fn int_length() {
assert_eq!(
Ok(data::Value::Int(3)),
length(&vec!(data::Value::Int(123)))
);
assert_eq!(Ok(data::Value::Int(3)), length(&[data::Value::Int(123)]));
}

#[test]
fn object_length() {
let mut map = im::HashMap::new();
map.insert("abc".to_string(), data::Value::from_bool(true));
assert_eq!(
Ok(data::Value::Int(1)),
length(&vec!(data::Value::Obj(map)))
);
assert_eq!(Ok(data::Value::Int(1)), length(&[data::Value::Obj(map)]));
}

#[test]
Expand Down Expand Up @@ -417,11 +411,11 @@ mod tests {
fn substring_of_num() {
assert_eq!(
Ok(data::Value::Str("12".to_string())),
substring(&vec!(
substring(&[
data::Value::Int(123),
data::Value::Int(0),
data::Value::Int(2)
))
])
);
}

Expand All @@ -432,70 +426,61 @@ mod tests {
name: "substring",
msg: "end offset (0) is less than the start offset (2)".to_string()
}),
substring(&vec!(
substring(&[
data::Value::Int(123),
data::Value::Str("2".to_string()),
data::Value::Int(0)
))
])
);
}

#[test]
fn value_predicates() {
assert_eq!(
Ok(data::Value::Bool(true)),
is_null(&vec![data::Value::None])
);
assert_eq!(Ok(data::Value::Bool(true)), is_null(&[data::Value::None]));
assert_eq!(
Ok(data::Value::Bool(false)),
is_null(&vec![data::Value::Str("".to_string())])
is_null(&[data::Value::Str("".to_string())])
);

assert_eq!(Ok(data::Value::Bool(true)), is_empty(&[data::Value::None]));
assert_eq!(
Ok(data::Value::Bool(true)),
is_empty(&vec![data::Value::None])
);
assert_eq!(
Ok(data::Value::Bool(true)),
is_empty(&vec![data::Value::Str("".to_string())])
is_empty(&[data::Value::Str("".to_string())])
);
assert_eq!(
Ok(data::Value::Bool(false)),
is_empty(&vec![data::Value::Str(" ".to_string())])
is_empty(&[data::Value::Str(" ".to_string())])
);

assert_eq!(Ok(data::Value::Bool(true)), is_blank(&[data::Value::None]));
assert_eq!(
Ok(data::Value::Bool(true)),
is_blank(&vec![data::Value::None])
);
assert_eq!(
Ok(data::Value::Bool(true)),
is_blank(&vec![data::Value::Str("".to_string())])
is_blank(&[data::Value::Str("".to_string())])
);
assert_eq!(
Ok(data::Value::Bool(true)),
is_blank(&vec![data::Value::Str(" ".to_string())])
is_blank(&[data::Value::Str(" ".to_string())])
);
assert_eq!(
Ok(data::Value::Bool(false)),
is_blank(&vec![data::Value::Str("abc".to_string())])
is_blank(&[data::Value::Str("abc".to_string())])
);

assert_eq!(
Ok(data::Value::Bool(true)),
is_numeric(&vec![data::Value::Str("123".to_string())])
is_numeric(&[data::Value::Str("123".to_string())])
);
assert_eq!(
Ok(data::Value::Bool(true)),
is_numeric(&vec![data::Value::Str("1.23".to_string())])
is_numeric(&[data::Value::Str("1.23".to_string())])
);
assert_eq!(
Ok(data::Value::Bool(true)),
is_numeric(&vec![data::Value::Str("1e3".to_string())])
is_numeric(&[data::Value::Str("1e3".to_string())])
);
assert_eq!(
Ok(data::Value::Bool(false)),
is_numeric(&vec![data::Value::Str("abc".to_string())])
is_numeric(&[data::Value::Str("abc".to_string())])
);
}
}
Loading

0 comments on commit d6ccbd1

Please sign in to comment.