Skip to content

Commit

Permalink
search: fix regression (#7)
Browse files Browse the repository at this point in the history
* search: fix regression

* Add two more test cases

---------

Co-authored-by: Sandro Jäckel <[email protected]>
  • Loading branch information
MarcelCoding and SuperSandro2000 authored Oct 13, 2024
1 parent bf67342 commit fb89d2c
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 71,636 deletions.
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();

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();
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

0 comments on commit fb89d2c

Please sign in to comment.