Skip to content

Commit 5648014

Browse files
more clippy cleanups (RedisJSON#905)
* clippt cleanups * replace match with ok_or_else * clean more warnings * clean more code * fix typo errors * fix typo error * fix typo error * remove uneeded deref * more cleanups * format code * fix code example * two more small cleanups * more cleanup * remove uneeded clousre --------- Co-authored-by: filipe oliveira <filipecosta.90@gmail.com>
1 parent 74c5a8b commit 5648014

15 files changed

+187
-230
lines changed

.github/wordlist.txt

+3-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ JRedisJSON
99
JS
1010
JSONPath
1111
JSONTestSuite
12+
jedis
1213
Jedis
1314
KeyRef
1415
KeyRefs
@@ -90,6 +91,7 @@ repo
9091
rubygems
9192
rueian
9293
rueidis
94+
rustis
9395
schulzf
9496
sdk
9597
seriot
@@ -100,4 +102,4 @@ trie
100102
uint
101103
vachhanihpavan
102104
www
103-
zstd
105+
zstd

Cargo.toml

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
cargo-features = ["edition2021"]
2-
31
[package]
42
name = "redisjson"
53
version = "99.99.99"

build.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
/*
2-
* Copyright Redis Ltd. 2016 - present
3-
* Licensed under your choice of the Redis Source Available License 2.0 (RSALv2) or
4-
* the Server Side Public License v1 (SSPLv1).
5-
*/
6-
1+
/*
2+
* Copyright Redis Ltd. 2016 - present
3+
* Licensed under your choice of the Redis Source Available License 2.0 (RSALv2) or
4+
* the Server Side Public License v1 (SSPLv1).
5+
*/
6+
77
use std::process::Command;
88

99
fn main() {
@@ -13,14 +13,14 @@ fn main() {
1313
.output();
1414
if let Ok(sha) = git_sha {
1515
let sha = String::from_utf8(sha.stdout).unwrap();
16-
println!("cargo:rustc-env=GIT_SHA={}", sha);
16+
println!("cargo:rustc-env=GIT_SHA={sha}");
1717
}
1818
// Expose GIT_BRANCH env var
1919
let git_branch = Command::new("git")
2020
.args(["rev-parse", "--abbrev-ref", "HEAD"])
2121
.output();
2222
if let Ok(branch) = git_branch {
2323
let branch = String::from_utf8(branch.stdout).unwrap();
24-
println!("cargo:rustc-env=GIT_BRANCH={}", branch);
24+
println!("cargo:rustc-env=GIT_BRANCH={branch}");
2525
}
2626
}

src/c_api.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,9 @@ pub fn json_api_open_key_internal<M: Manager>(
8080
pub fn json_api_get_at<M: Manager>(_: M, json: *const c_void, index: size_t) -> *const c_void {
8181
let json = unsafe { &*(json.cast::<M::V>()) };
8282
match json.get_type() {
83-
SelectValueType::Array => match json.get_index(index) {
84-
Some(v) => (v as *const M::V).cast::<c_void>(),
85-
_ => null(),
86-
},
83+
SelectValueType::Array => json
84+
.get_index(index)
85+
.map_or_else(null, |v| (v as *const M::V).cast::<c_void>()),
8786
_ => null(),
8887
}
8988
}

src/commands.rs

+27-30
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ pub struct KeyValue<'a, V: SelectValue> {
8888
}
8989

9090
impl<'a, V: SelectValue + 'a> KeyValue<'a, V> {
91-
pub fn new(v: &'a V) -> KeyValue<'a, V> {
91+
pub const fn new(v: &'a V) -> KeyValue<'a, V> {
9292
KeyValue { val: v }
9393
}
9494

@@ -424,7 +424,7 @@ impl<'a, V: SelectValue + 'a> KeyValue<'a, V> {
424424
if a.len().unwrap() != b.len().unwrap() {
425425
false
426426
} else {
427-
for (i, e) in a.values().unwrap().into_iter().enumerate() {
427+
for (i, e) in a.values().unwrap().enumerate() {
428428
if !Self::is_equal(e, b.get_index(i).unwrap()) {
429429
return false;
430430
}
@@ -465,10 +465,7 @@ impl<'a, V: SelectValue + 'a> KeyValue<'a, V> {
465465
let res = self
466466
.get_values(path)?
467467
.iter()
468-
.map(|value| {
469-
self.arr_first_index_single(value, &json_value, start, end)
470-
.into()
471-
})
468+
.map(|value| Self::arr_first_index_single(value, &json_value, start, end).into())
472469
.collect::<Vec<RedisValue>>();
473470
Ok(res.into())
474471
}
@@ -481,7 +478,7 @@ impl<'a, V: SelectValue + 'a> KeyValue<'a, V> {
481478
end: i64,
482479
) -> Result<RedisValue, Error> {
483480
let arr = self.get_first(path)?;
484-
match self.arr_first_index_single(arr, &json_value, start, end) {
481+
match Self::arr_first_index_single(arr, &json_value, start, end) {
485482
FoundIndex::NotArray => Err(Error::from(err_msg_json_expected(
486483
"array",
487484
self.get_type(path).unwrap().as_str(),
@@ -490,8 +487,8 @@ impl<'a, V: SelectValue + 'a> KeyValue<'a, V> {
490487
}
491488
}
492489

493-
/// Returns first array index of `v` in `arr`, or NotFound if not found in `arr`, or NotArray if `arr` is not an array
494-
fn arr_first_index_single(&self, arr: &V, v: &Value, start: i64, end: i64) -> FoundIndex {
490+
/// Returns first array index of `v` in `arr`, or `NotFound` if not found in `arr`, or `NotArray` if `arr` is not an array
491+
fn arr_first_index_single(arr: &V, v: &Value, start: i64, end: i64) -> FoundIndex {
495492
if !arr.is_array() {
496493
return FoundIndex::NotArray;
497494
}
@@ -609,17 +606,17 @@ pub fn json_set<M: Manager>(manager: M, ctx: &Context, args: Vec<RedisString>) -
609606
let val = manager.from_str(value, format, true)?;
610607

611608
match (current, set_option) {
612-
(Some(ref mut doc), ref op) => {
609+
(Some(doc), op) => {
613610
if path.get_path() == JSON_ROOT_PATH {
614-
if *op != SetOptions::NotExists {
611+
if op != SetOptions::NotExists {
615612
redis_key.set_value(Vec::new(), val)?;
616613
redis_key.apply_changes(ctx, "json.set")?;
617614
REDIS_OK
618615
} else {
619616
Ok(RedisValue::Null)
620617
}
621618
} else {
622-
let mut update_info = KeyValue::new(*doc).find_paths(path.get_path(), op)?;
619+
let mut update_info = KeyValue::new(doc).find_paths(path.get_path(), &op)?;
623620
if !update_info.is_empty() {
624621
let mut res = false;
625622
if update_info.len() == 1 {
@@ -726,7 +723,7 @@ where
726723
.collect::<Vec<Option<&T>>>()
727724
}
728725

729-
fn find_all_paths<T: SelectValue, F: FnMut(&T) -> bool>(
726+
fn find_all_paths<T: SelectValue, F>(
730727
path: &str,
731728
doc: &T,
732729
f: F,
@@ -741,7 +738,7 @@ where
741738
}
742739
}
743740

744-
fn find_all_values<'a, T: SelectValue, F: FnMut(&T) -> bool>(
741+
fn find_all_values<'a, T: SelectValue, F>(
745742
path: &str,
746743
doc: &'a T,
747744
f: F,
@@ -1777,17 +1774,17 @@ where
17771774
.ok_or_else(RedisError::nonexistent_key)?;
17781775

17791776
let paths = find_paths(path, root, |v| v.get_type() == SelectValueType::Array)?;
1780-
if !paths.is_empty() {
1777+
if paths.is_empty() {
1778+
Err(RedisError::String(
1779+
err_msg_json_path_doesnt_exist_with_param_or(path, "not an array"),
1780+
))
1781+
} else {
17811782
let mut res = None;
17821783
for p in paths {
17831784
res = Some(redis_key.arr_trim(p, start, stop)?);
17841785
}
17851786
redis_key.apply_changes(ctx, "json.arrtrim")?;
17861787
Ok(res.unwrap().into())
1787-
} else {
1788-
Err(RedisError::String(
1789-
err_msg_json_path_doesnt_exist_with_param_or(path, "not an array"),
1790-
))
17911788
}
17921789
}
17931790

@@ -1818,10 +1815,9 @@ where
18181815
let values = find_all_values(path, root, |v| v.get_type() == SelectValueType::Object)?;
18191816
let mut res: Vec<RedisValue> = vec![];
18201817
for v in values {
1821-
res.push(match v {
1822-
Some(v) => v.keys().unwrap().collect::<Vec<&str>>().into(),
1823-
_ => RedisValue::Null,
1824-
});
1818+
res.push(v.map_or(RedisValue::Null, |v| {
1819+
v.keys().unwrap().collect::<Vec<&str>>().into()
1820+
}));
18251821
}
18261822
res.into()
18271823
};
@@ -1874,9 +1870,10 @@ where
18741870
let res = match root {
18751871
Some(root) => find_all_values(path, root, |v| v.get_type() == SelectValueType::Object)?
18761872
.iter()
1877-
.map(|v| match *v {
1878-
Some(v) => RedisValue::Integer(v.len().unwrap() as i64),
1879-
None => RedisValue::Null,
1873+
.map(|v| {
1874+
v.map_or(RedisValue::Null, |v| {
1875+
RedisValue::Integer(v.len().unwrap() as i64)
1876+
})
18801877
})
18811878
.collect::<Vec<RedisValue>>()
18821879
.into(),
@@ -2010,8 +2007,8 @@ pub fn json_resp<M: Manager>(manager: M, ctx: &Context, args: Vec<RedisString>)
20102007
};
20112008

20122009
let key = manager.open_key_read(ctx, &key)?;
2013-
match key.get_value()? {
2014-
Some(doc) => KeyValue::new(doc).resp_serialize(path),
2015-
None => Ok(RedisValue::Null),
2016-
}
2010+
key.get_value()?.map_or_else(
2011+
|| Ok(RedisValue::Null),
2012+
|doc| KeyValue::new(doc).resp_serialize(path),
2013+
)
20172014
}

src/error.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
/*
2-
* Copyright Redis Ltd. 2016 - present
3-
* Licensed under your choice of the Redis Source Available License 2.0 (RSALv2) or
4-
* the Server Side Public License v1 (SSPLv1).
5-
*/
6-
1+
/*
2+
* Copyright Redis Ltd. 2016 - present
3+
* Licensed under your choice of the Redis Source Available License 2.0 (RSALv2) or
4+
* the Server Side Public License v1 (SSPLv1).
5+
*/
6+
77
use crate::jsonpath::json_path::QueryCompilationError;
88
use redis_module::RedisError;
99
use std::num::ParseIntError;
@@ -55,7 +55,7 @@ impl From<redis_module::error::Error> for Error {
5555

5656
impl From<RedisError> for Error {
5757
fn from(e: RedisError) -> Self {
58-
Self::from(format!("ERR {}", e))
58+
Self::from(format!("ERR {e}"))
5959
}
6060
}
6161

src/formatter.rs

+31-27
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
/*
2-
* Copyright Redis Ltd. 2016 - present
3-
* Licensed under your choice of the Redis Source Available License 2.0 (RSALv2) or
4-
* the Server Side Public License v1 (SSPLv1).
5-
*/
6-
1+
/*
2+
* Copyright Redis Ltd. 2016 - present
3+
* Licensed under your choice of the Redis Source Available License 2.0 (RSALv2) or
4+
* the Server Side Public License v1 (SSPLv1).
5+
*/
6+
77
// Custom serde_json formatter supporting ReJSON formatting options.
88
// Based on serde_json::ser::PrettyFormatter
99
/*
@@ -44,7 +44,11 @@ pub struct RedisJsonFormatter<'a> {
4444
}
4545

4646
impl<'a> RedisJsonFormatter<'a> {
47-
pub fn new(indent: Option<&'a str>, space: Option<&'a str>, newline: Option<&'a str>) -> Self {
47+
pub const fn new(
48+
indent: Option<&'a str>,
49+
space: Option<&'a str>,
50+
newline: Option<&'a str>,
51+
) -> Self {
4852
RedisJsonFormatter {
4953
current_indent: 0,
5054
has_value: false,
@@ -54,9 +58,9 @@ impl<'a> RedisJsonFormatter<'a> {
5458
}
5559
}
5660

57-
fn new_line<W: ?Sized>(&self, wr: &mut W) -> io::Result<()>
61+
fn new_line<W>(&self, wr: &mut W) -> io::Result<()>
5862
where
59-
W: io::Write,
63+
W: io::Write + ?Sized,
6064
{
6165
// Write new line if defined
6266
if let Some(n) = self.newline {
@@ -76,18 +80,18 @@ impl<'a> RedisJsonFormatter<'a> {
7680
}
7781

7882
impl Formatter for RedisJsonFormatter<'_> {
79-
fn begin_array<W: ?Sized>(&mut self, writer: &mut W) -> io::Result<()>
83+
fn begin_array<W>(&mut self, writer: &mut W) -> io::Result<()>
8084
where
81-
W: io::Write,
85+
W: io::Write + ?Sized,
8286
{
8387
self.current_indent += 1;
8488
self.has_value = false;
8589
writer.write_all(b"[")
8690
}
8791

88-
fn end_array<W: ?Sized>(&mut self, writer: &mut W) -> io::Result<()>
92+
fn end_array<W>(&mut self, writer: &mut W) -> io::Result<()>
8993
where
90-
W: io::Write,
94+
W: io::Write + ?Sized,
9195
{
9296
self.current_indent -= 1;
9397

@@ -98,36 +102,36 @@ impl Formatter for RedisJsonFormatter<'_> {
98102
writer.write_all(b"]")
99103
}
100104

101-
fn begin_array_value<W: ?Sized>(&mut self, writer: &mut W, first: bool) -> io::Result<()>
105+
fn begin_array_value<W>(&mut self, writer: &mut W, first: bool) -> io::Result<()>
102106
where
103-
W: io::Write,
107+
W: io::Write + ?Sized,
104108
{
105109
if !first {
106110
writer.write_all(b",")?;
107111
}
108112
self.new_line(writer)
109113
}
110114

111-
fn end_array_value<W: ?Sized>(&mut self, _writer: &mut W) -> io::Result<()>
115+
fn end_array_value<W>(&mut self, _writer: &mut W) -> io::Result<()>
112116
where
113-
W: io::Write,
117+
W: io::Write + ?Sized,
114118
{
115119
self.has_value = true;
116120
Ok(())
117121
}
118122

119-
fn begin_object<W: ?Sized>(&mut self, writer: &mut W) -> io::Result<()>
123+
fn begin_object<W>(&mut self, writer: &mut W) -> io::Result<()>
120124
where
121-
W: io::Write,
125+
W: io::Write + ?Sized,
122126
{
123127
self.current_indent += 1;
124128
self.has_value = false;
125129
writer.write_all(b"{")
126130
}
127131

128-
fn end_object<W: ?Sized>(&mut self, writer: &mut W) -> io::Result<()>
132+
fn end_object<W>(&mut self, writer: &mut W) -> io::Result<()>
129133
where
130-
W: io::Write,
134+
W: io::Write + ?Sized,
131135
{
132136
self.current_indent -= 1;
133137

@@ -138,28 +142,28 @@ impl Formatter for RedisJsonFormatter<'_> {
138142
writer.write_all(b"}")
139143
}
140144

141-
fn begin_object_key<W: ?Sized>(&mut self, writer: &mut W, first: bool) -> io::Result<()>
145+
fn begin_object_key<W>(&mut self, writer: &mut W, first: bool) -> io::Result<()>
142146
where
143-
W: io::Write,
147+
W: io::Write + ?Sized,
144148
{
145149
if !first {
146150
writer.write_all(b",")?;
147151
}
148152
self.new_line(writer)
149153
}
150154

151-
fn begin_object_value<W: ?Sized>(&mut self, writer: &mut W) -> io::Result<()>
155+
fn begin_object_value<W>(&mut self, writer: &mut W) -> io::Result<()>
152156
where
153-
W: io::Write,
157+
W: io::Write + ?Sized,
154158
{
155159
writer.write_all(b":")?;
156160
self.space
157161
.map_or(Ok(()), |s| writer.write_all(s.as_bytes()))
158162
}
159163

160-
fn end_object_value<W: ?Sized>(&mut self, _writer: &mut W) -> io::Result<()>
164+
fn end_object_value<W>(&mut self, _writer: &mut W) -> io::Result<()>
161165
where
162-
W: io::Write,
166+
W: io::Write + ?Sized,
163167
{
164168
self.has_value = true;
165169
Ok(())

0 commit comments

Comments
 (0)