Refactor format_keywords function

This commit is contained in:
Azalea Colburn 2025-03-14 19:27:25 -07:00
parent fb8d23a22a
commit a352c88b8f
No known key found for this signature in database
1 changed files with 8 additions and 12 deletions

View File

@ -126,18 +126,14 @@ impl Query {
/// ## WARNING
/// Clones `self.keywords`
fn format_keywords(&self) -> String {
if self.keywords.is_empty() {
return String::new();
let mut keywords = self.keywords.clone();
match keywords.pop() {
None => String::new(),
Some(last) => {
let head = keywords.join("', '");
format!("'{}', and '{}'", head, last)
}
}
let result = self
.keywords
.iter()
.take(self.keywords.len() - 1)
.map(|s| s.to_string())
.collect::<Vec<String>>()
.join("', '");
format!("'{}', and '{}'", result, self.keywords.last().unwrap())
}
}