Skip to content

Commit

Permalink
fix clippy
Browse files Browse the repository at this point in the history
  • Loading branch information
freestrings committed Sep 7, 2021
1 parent 1d7bada commit 1a84c5a
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 36 deletions.
4 changes: 2 additions & 2 deletions src/selector/cmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ impl Cmp for CmpEq {
fn cmp_json<'a>(&self, v1: &[&'a Value], v2: &[&'a Value]) -> Vec<&'a Value> {
v1.iter().fold(Vec::new(), |acc, a| {
v2.iter().fold(acc, |mut acc, b| {
if *a as *const Value == *b as *const Value {
if std::ptr::eq(*a, *b) {
acc.push(*a);
}
acc
Expand Down Expand Up @@ -60,7 +60,7 @@ impl Cmp for CmpNe {
let mut ret = v1.to_vec();
for v in v2 {
for i in 0..ret.len() {
if *v as *const Value == &*ret[i] as *const Value {
if std::ptr::eq(*v, &*ret[i]) {
ret.remove(i);
break;
}
Expand Down
24 changes: 13 additions & 11 deletions src/selector/terms.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ impl<'a> ExprTerm<'a> {
C: Cmp,
{
match other {
ExprTerm::Number(n2) => ExprTerm::Bool(cmp_fn.cmp_f64(utils::to_f64(n1), utils::to_f64(&n2))),
ExprTerm::Number(n2) => ExprTerm::Bool(cmp_fn.cmp_f64(utils::to_f64(n1), utils::to_f64(n2))),
ExprTerm::Json(_, _, _) => unreachable!(),
_ => ExprTerm::Bool(cmp_fn.default()),
}
Expand All @@ -54,7 +54,7 @@ impl<'a> ExprTerm<'a> {

fn cmp_json_string<C>(s2: &str,
fk1: &Option<FilterKey>,
vec1: &Vec<&'a Value>,
vec1: &[&'a Value],
cmp_fn: &C) -> Vec<&'a Value>
where
C: Cmp
Expand All @@ -78,7 +78,7 @@ impl<'a> ExprTerm<'a> {

fn cmp_json_number<C>(n2: &Number,
fk1: &Option<FilterKey>,
vec1: &Vec<&'a Value>,
vec1: &[&'a Value],
cmp_fn: &C) -> Vec<&'a Value>
where
C: Cmp
Expand All @@ -100,7 +100,7 @@ impl<'a> ExprTerm<'a> {

fn cmp_json_bool<C1>(b2: &bool,
fk1: &Option<FilterKey>,
vec1: &Vec<&'a Value>,
vec1: &[&'a Value],
cmp_fn: &C1) -> Vec<&'a Value>
where
C1: Cmp
Expand All @@ -121,8 +121,8 @@ impl<'a> ExprTerm<'a> {

fn cmp_json_json<C1>(rel: &Option<Vec<&'a Value>>,
parent: &Option<Vec<&'a Value>>,
vec1: &Vec<&'a Value>,
vec2: &Vec<&'a Value>,
vec1: &[&'a Value],
vec2: &[&'a Value],
cmp_fn: &C1) -> Vec<&'a Value>
where
C1: Cmp
Expand Down Expand Up @@ -150,10 +150,10 @@ impl<'a> ExprTerm<'a> {
{
let ret: Vec<&Value> = match other {
ExprTerm::String(s2) => Self::cmp_json_string(s2, &fk1, vec1, cmp_fn),
ExprTerm::Number(n2) => Self::cmp_json_number(&n2, &fk1, vec1, cmp_fn),
ExprTerm::Bool(b2) => Self::cmp_json_bool(&b2, &fk1, vec1, cmp_fn),
ExprTerm::Number(n2) => Self::cmp_json_number(n2, &fk1, vec1, cmp_fn),
ExprTerm::Bool(b2) => Self::cmp_json_bool(b2, &fk1, vec1, cmp_fn),
ExprTerm::Json(parent, _, vec2) => {
Self::cmp_json_json(&rel, &parent, vec1, &vec2, cmp_fn)
Self::cmp_json_json(&rel, parent, vec1, vec2, cmp_fn)
}
};

Expand All @@ -167,7 +167,9 @@ impl<'a> ExprTerm<'a> {

if rel.is_some() {
if let ExprTerm::Json(_, _, _) = &other {
return ExprTerm::Json(Some(rel.unwrap()), None, ret);
if let Some(rel) = rel {
return ExprTerm::Json(Some(rel), None, ret);
}
}
}

Expand Down Expand Up @@ -418,7 +420,7 @@ impl<'a> FilterTerms<'a> {
let mut visited = HashSet::new();
let mut acc = Vec::new();

let ref path_key = utils::to_path_str(key);
let path_key = &utils::to_path_str(key);

ValueWalker::walk_dedup_all(vec,
path_key.get_key(),
Expand Down
2 changes: 1 addition & 1 deletion src/selector/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ impl<'a: 'b, 'b> PathKey<'a> {
}
}

pub fn to_path_str<'a>(key: &'a str) -> PathKey<'a> {
pub fn to_path_str(key: &str) -> PathKey {
let mut path_key = PathKey {
key,
special_key: None
Expand Down
39 changes: 17 additions & 22 deletions src/selector/value_walker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use selector::utils::PathKey;
pub(super) struct ValueWalker;

impl<'a> ValueWalker {
pub fn next_all(vec: &Vec<&'a Value>) -> Vec<&'a Value> {
pub fn next_all(vec: &[&'a Value]) -> Vec<&'a Value> {
vec.iter().fold(Vec::new(), |mut acc, v| {
match v {
Value::Object(map) => acc.extend(map.values()),
Expand All @@ -18,7 +18,7 @@ impl<'a> ValueWalker {
})
}

pub fn next_with_str(vec: &Vec<&'a Value>, key: &'a str) -> Vec<&'a Value> {
pub fn next_with_str(vec: &[&'a Value], key: &'a str) -> Vec<&'a Value> {
vec.iter().fold(Vec::new(), |mut acc, v| {
if let Value::Object(map) = v {
if let Some(v) = map.get(key) {
Expand All @@ -29,21 +29,18 @@ impl<'a> ValueWalker {
})
}

pub fn next_with_num(vec: &Vec<&'a Value>, index: f64) -> Vec<&'a Value> {
pub fn next_with_num(vec: &[&'a Value], index: f64) -> Vec<&'a Value> {
vec.iter().fold(Vec::new(), |mut acc, v| {
match v {
Value::Array(vec) => {
if let Some(v) = vec.get(utils::abs_index(index as isize, vec.len())) {
acc.push(v);
}
if let Value::Array(vec) = v {
if let Some(v) = vec.get(utils::abs_index(index as isize, vec.len())) {
acc.push(v);
}
_ => {}
}
acc
})
}

pub fn all_with_num(vec: &Vec<&'a Value>, index: f64) -> Vec<&'a Value> {
pub fn all_with_num(vec: &[&'a Value], index: f64) -> Vec<&'a Value> {
Self::walk(vec, &|v, acc| {
if v.is_array() {
if let Some(v) = v.get(index as usize) {
Expand All @@ -53,7 +50,7 @@ impl<'a> ValueWalker {
})
}

pub fn all_with_str(vec: &Vec<&'a Value>, key: &'a str) -> Vec<&'a Value> {
pub fn all_with_str(vec: &[&'a Value], key: &'a str) -> Vec<&'a Value> {
let path_key = utils::to_path_str(key);
Self::walk(vec, &|v, acc| if let Value::Object(map) = v {
if let Some(v) = map.get(path_key.get_key()) {
Expand All @@ -62,8 +59,8 @@ impl<'a> ValueWalker {
})
}

pub fn all_with_strs(vec: &Vec<&'a Value>, keys: &[&'a str]) -> Vec<&'a Value> {
let ref path_keys: Vec<PathKey> = keys.iter().map(|key| { utils::to_path_str(key) }).collect();
pub fn all_with_strs(vec: &[&'a Value], keys: &[&'a str]) -> Vec<&'a Value> {
let path_keys: &Vec<PathKey> = &keys.iter().map(|key| { utils::to_path_str(key) }).collect();
vec.iter().fold(Vec::new(), |mut acc, v| {
if let Value::Object(map) = v {
path_keys.iter().for_each(|pk| if let Some(v) = map.get(pk.get_key()) {
Expand All @@ -74,7 +71,7 @@ impl<'a> ValueWalker {
})
}

pub fn all(vec: &Vec<&'a Value>) -> Vec<&'a Value> {
pub fn all(vec: &[&'a Value]) -> Vec<&'a Value> {
Self::walk(vec, &|v, acc| {
match v {
Value::Array(ay) => acc.extend(ay),
Expand All @@ -86,7 +83,7 @@ impl<'a> ValueWalker {
})
}

fn walk<F>(vec: &Vec<&'a Value>, fun: &F) -> Vec<&'a Value>
fn walk<F>(vec: &[&'a Value], fun: &F) -> Vec<&'a Value>
where
F: Fn(&'a Value, &mut Vec<&'a Value>),
{
Expand All @@ -113,7 +110,7 @@ impl<'a> ValueWalker {
}
}

pub fn walk_dedup_all<F1, F2>(vec: &Vec<&'a Value>,
pub fn walk_dedup_all<F1, F2>(vec: &[&'a Value],
key: &str,
visited: &mut HashSet<*const Value>,
is_contain: &mut F1,
Expand Down Expand Up @@ -150,24 +147,22 @@ impl<'a> ValueWalker {

match v {
Value::Object(map) => {
if let Some(_) = map.get(key) {
if map.get(key).is_some() {
let ptr = v as *const Value;
if !visited.contains(&ptr) {
visited.insert(ptr);
is_contain(v);
}
} else {
if depth == 0 {
is_not_contain(index);
}
} else if depth == 0 {
is_not_contain(index);
}
}
Value::Array(vec) => {
if depth == 0 {
is_not_contain(index);
}
vec.iter().for_each(|v| {
Self::walk_dedup(&v, key, visited, index, is_contain, is_not_contain, depth + 1);
Self::walk_dedup(v, key, visited, index, is_contain, is_not_contain, depth + 1);
})
}
_ => {
Expand Down

0 comments on commit 1a84c5a

Please sign in to comment.