Skip to content

Commit

Permalink
chore: applied clippy suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
leontoeides committed Jan 12, 2025
1 parent de09fd9 commit e4aea47
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 37 deletions.
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "indicium"
version = "0.6.4"
version = "0.6.5"
authors = ["Dylan Bowker <dylan.bowker@arkiteq.io>"]
edition = "2021"
categories = ["database-implementations"]
Expand All @@ -14,9 +14,9 @@ repository = "https://github.com/leontoeides/indicium"
rust-version = "1.73.0"

[features]
default = ["simple", "rustc-hash"]
default = ["simple"]

simple = ["rapidfuzz"]
simple = ["rapidfuzz", "rustc-hash"]
select2 = ["simple", "dep:serde"]

fuzzy = ["dep:strsim"]
Expand Down
19 changes: 7 additions & 12 deletions src/select2/flat/results.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ impl Request {
name = "build flat results",
skip(self, search_results_keys, search_results_values)
)]
#[allow(clippy::option_if_let_else)]
pub fn flat_response<K: Clone + Ord + ToString, S: Selectable>(
&self,
items_per_page: &Option<usize>,
Expand Down Expand Up @@ -60,7 +61,7 @@ impl Request {
// records dumped from a key-value store:
let paginated_results: Vec<Record> = search_results_keys
// Iterate over each passed record:
.into_iter()
.iter()
// Track the number of keys we've iterated over, so we can
// look-up the corresponding values from the
// `search_results_values` slice:
Expand All @@ -81,11 +82,8 @@ impl Request {
// ...was set. Update record with comparison result and
// return record:
record.selected = record.id == *selected_record;
record
} else {
// ...wasn't set, return record as-is:
record
} // if
} // ...wasn't set, return record as-is:
record
}) // map
// Collect all Select2 records into a `Vec<Record>`:
.collect();
Expand All @@ -104,7 +102,7 @@ impl Request {
// records dumped from a key-value store:
let unpaginated_results = search_results_keys
// Iterate over each passed record:
.into_iter()
.iter()
// Track the number of keys we've iterated over, so we can
// look-up the corresponding values from the
// `search_results_values` slice:
Expand All @@ -121,11 +119,8 @@ impl Request {
// ...was set. Update record with comparison result and
// return record:
record.selected = record.id == *selected_record;
record
} else {
// ...wasn't set, return record as-is:
record
} // if
} // ...wasn't set, return record as-is:
record
}) // map
// Collect all select2 records into a `Vec<Record>`:
.collect();
Expand Down
11 changes: 6 additions & 5 deletions src/select2/grouped/results.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ impl Request {
name = "build grouped results",
skip(self, search_results_keys, search_results_values)
)]
#[allow(clippy::option_if_let_else)]
pub fn grouped_response<
K: Clone + Debug + Display + Eq + Hash + PartialEq + ToString,
G: Groupable,
Expand Down Expand Up @@ -69,7 +70,7 @@ impl Request {
// records dumped from a key-value store:
let paginated_results: Vec<(&K, &G)> = search_results_keys
// Iterate over each key:
.into_iter()
.iter()
// Track the number of keys we've iterated over, so we can
// look-up the corresponding values from the
// `search_results_values` slice:
Expand All @@ -95,7 +96,7 @@ impl Request {
// records dumped from a key-value store:
let unpaginated_results: Vec<(&K, &G)> = search_results_keys
// Iterate over each key:
.into_iter()
.iter()
// Track the number of keys we've iterated over, so we can
// look-up the corresponding values from the
// `search_results_values` slice:
Expand Down Expand Up @@ -140,7 +141,7 @@ impl Request {
Some(group) => group.push(record),
// If group does not exist, initialize with this record:
None => {
grouped_results.insert(groupable_record.group.to_owned(), vec![record]);
grouped_results.insert(groupable_record.group.clone(), vec![record]);
} // None
} // match
}); // for_each
Expand All @@ -151,8 +152,8 @@ impl Request {
let grouped_results: Vec<Group> = grouped_results
.into_iter()
.map(|(group, records)| Group {
text: group.to_string(),
children: records.to_owned(),
text: group,
children: records,
}) // map
.collect();

Expand Down
34 changes: 17 additions & 17 deletions src/select2/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,23 @@
//! Steps for processing a Select2 request:
//!
//! 1. Setup a route to your Select2 server in your Rust web framework. In your
//! route function, convert the query-string received from the Select2 plug-in
//! into a [`Request`] struct. Your chosen web framework should provide some
//! facilities for processing a query-string into a `struct`.
//! route function, convert the query-string received from the Select2
//! plug-in into a [`Request`] struct. Your chosen web framework should
//! provide some facilities for processing a query-string into a `struct`.
//!
//! 2. Your search index must already be initialized. Search the index using the
//! [`search_select2`] method, supplying it with the `Request` struct you've
//! built from the query-string. This search function will return keys as the
//! search results.
//! [`search_select2`] method, supplying it with the `Request` struct you've
//! built from the query-string. This search function will return keys as the
//! search results.
//!
//! 3. If desired, filter (and further process) the search results.
//!
//! 4. Using the keys returned from the `search_select2` search, get the values
//! from your collection. This crate does not know how to look up values from
//! your collection, so you must get them.
//! from your collection. This crate does not know how to look up values from
//! your collection, so you must get them.
//!
//! 5. Use either the [`flat_response`] or [`grouped_response`] method to
//! produce a response for the Select2 plug-in, providing:
//! produce a response for the Select2 plug-in, providing:
//! * the `Request` struct _in step #1_,
//! * the keys from `search_select2` _in step #2_,
//! * and the values you got from your collection _in step #4_,
Expand All @@ -47,8 +47,8 @@
//! [`grouped_response`]: struct.Request.html#method.grouped_response
//!
//! 6. Depending on whether flat or grouped output was selected, convert the
//! [`FlatResults`] or [`GroupedResults`] struct into `JSON` and return it to
//! the client.
//! [`FlatResults`] or [`GroupedResults`] struct into `JSON` and return it to
//! the client.
//!
//! [`FlatResults`]: flat/struct.FlatResults.html
//! [`GroupedResults`]: grouped/struct.GroupedResults.html
Expand Down Expand Up @@ -144,14 +144,13 @@ impl<'a> Request {
/// For some reason, `Select2` can send the user's search term in either
/// the `term` field or in the `q` field. This convenience method checks
/// both fields and returns the user's query term, if available.
#[must_use]
#[allow(clippy::option_if_let_else)]
pub fn query_term(&'a self, dump_keyword: Option<&'a str>) -> Option<&'a str> {
// Get query (search term) if any:
match &self.q {
Some(q) => Some(q.as_str()),
None => match &self.term {
Some(term) => Some(term.as_str()),
None => dump_keyword,
}, // None
None => self.term.as_ref().map_or(dump_keyword, |term| Some(term.as_str())), // None
} // match
} // fn
} // impl
Expand All @@ -161,13 +160,14 @@ impl<'a> Request {
impl Request {
/// This convenience method will return the appropriate page number for
/// pagination.
#[must_use]
#[allow(clippy::missing_panics_doc)] // `None` is handled, `unwrap` is safe
pub fn page_number(&self) -> usize {
// Ensure that the `page` number is set correctly before processing:
match self.page {
// If no page number specified, assume page 1:
None => 1,
// There is no page 0. Assume caller meant page 1:
Some(0) => 1,
Some(0) | None => 1,
// Otherwise continue with caller's page number:
_ => self.page.unwrap(),
} // match
Expand Down
1 change: 1 addition & 0 deletions src/select2/search_select2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ impl<'a, K: 'a + Debug + Hash + Ord> SearchIndex<K> {
/// method will return all search results for the client's query.
#[tracing::instrument(level = "trace", name = "select2 search", skip(self))]
#[allow(clippy::option_if_let_else)]
pub fn search_select2(&'a self, request: &'a Request) -> Vec<&'a K> {
// Get query (or "search term"), if any:
let query_term: Option<&str> = request.query_term(self.dump_keyword());
Expand Down

0 comments on commit e4aea47

Please sign in to comment.