Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

search: fix regression #7

Merged
merged 2 commits into from
Oct 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion libixx/src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,10 @@ impl Index {
.map(|segment| segment.to_lowercase())
.collect::<Vec<_>>();

if search.is_empty() {
return Ok(vec![]);
}

let mut results = Vec::new();

for (idx, option) in self.0.iter().enumerate() {
Expand All @@ -139,11 +143,13 @@ impl Index {
// remove last dot...
option_name.pop();

let lower_option_name = option_name.to_lowercase();
SuperSandro2000 marked this conversation as resolved.
Show resolved Hide resolved

let mut start = 0;

'outer: {
for segment in &search {
match option_name[start..].find(segment) {
match lower_option_name[start..].find(segment) {
Some(idx) => start = idx + segment.len(),
None => break 'outer,
}
Expand Down
56 changes: 44 additions & 12 deletions libixx/src/test/mod.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,53 @@
use std::{collections::HashMap, fs::File};

use crate::Index;

#[test]
fn test() {
let options: HashMap<String, crate::option::Option> =
serde_json::from_str(include_str!("./options.json")).unwrap();
MarcelCoding marked this conversation as resolved.
Show resolved Hide resolved
let mut index = Index::default();

let options = options.keys().collect::<Vec<_>>();
index.push("home.enableDebugInfo");
index.push("home.enableNixpkgsReleaseCheck");
index.push("home.file.<name>.enable");
index.push("home.language.measurement");
index.push("home.pointerCursor.gtk.enable");
index.push("home.pointerCursor.x11.enable");
index.push("programs.home-manager.enable");
index.push("services.home-manager.autoUpgrade.enable");
index.push("services.home-manager.autoUpgrade.frequency");

let mut index = Index::default();
for option in &options {
index.push(option);
}
assert_eq!(
index.search("ho*auto", 10).unwrap(),
vec![
(
7usize,
"services.home-manager.autoUpgrade.enable".to_string()
),
(
8usize,
"services.home-manager.autoUpgrade.frequency".to_string()
)
]
);

assert_eq!(
index.search("ho*auto*ena", 10).unwrap(),
vec![(
7usize,
"services.home-manager.autoUpgrade.enable".to_string()
)]
);

assert_eq!(
index.search("ho*en*Nix", 10).unwrap(),
vec![(1usize, "home.enableNixpkgsReleaseCheck".to_string())]
);

println!("{:?}", index.search("ho*exta", 10).unwrap());
assert_eq!(
index.search("ho*en*Nix*Rel*Che", 10).unwrap(),
vec![(1usize, "home.enableNixpkgsReleaseCheck".to_string())]
);

let mut file = File::create("index.nuscht").unwrap();
index.write_into(&mut file).unwrap();
assert_eq!(
index.search("enablenixpkgsreleasecheck", 10).unwrap(),
vec![(1usize, "home.enableNixpkgsReleaseCheck".to_string())]
);
}
Loading
Loading