Check parent for search index items

For members, the path listed in the search index does not point to the
parent item but to the path of the parent item.  To get the parent item,
we have to access the parent field and lookup the index in the crate’s
path list.
This commit is contained in:
Robin Krahl 2020-07-20 00:36:54 +02:00
parent 1bd5cf8fbe
commit e1ef283159

View File

@ -52,6 +52,8 @@ struct Data {
struct CrateData { struct CrateData {
#[serde(rename = "i")] #[serde(rename = "i")]
items: Vec<ItemData>, items: Vec<ItemData>,
#[serde(rename = "p")]
paths: Vec<(usize, String)>,
} }
#[derive(Debug, Default, PartialEq, serde_tuple::Deserialize_tuple)] #[derive(Debug, Default, PartialEq, serde_tuple::Deserialize_tuple)]
@ -119,11 +121,18 @@ impl Index {
} else { } else {
&item.path &item.path
}; };
let full_name = format!("{}::{}", path, &item.name); let full_path = match item.parent {
Some(idx) => {
let parent = &data.paths[idx].1;
format!("{}::{}", path, parent)
}
None => path.to_owned(),
};
let full_name = format!("{}::{}", &full_path, &item.name);
if full_name.ends_with(&keyword) { if full_name.ends_with(&keyword) {
matches.push(IndexItem { matches.push(IndexItem {
name: item.name.clone(), name: item.name.clone(),
path: path.to_owned(), path: full_path,
description: item.desc.clone(), description: item.desc.clone(),
}); });
} }
@ -152,7 +161,7 @@ mod tests {
expected expected
.crates .crates
.insert("test".to_owned(), Default::default()); .insert("test".to_owned(), Default::default());
let actual: Data = serde_json::from_str("{\"test\": {\"i\": []}}").unwrap(); let actual: Data = serde_json::from_str("{\"test\": {\"i\": [], \"p\": []}}").unwrap();
assert_eq!(expected, actual); assert_eq!(expected, actual);
} }
@ -167,7 +176,7 @@ mod tests {
krate.items.push(item); krate.items.push(item);
expected.crates.insert("test".to_owned(), krate); expected.crates.insert("test".to_owned(), krate);
let actual: Data = serde_json::from_str( let actual: Data = serde_json::from_str(
"{\"test\": {\"i\": [[0, \"name\", \"path\", \"desc\", null, null]]}}", "{\"test\": {\"i\": [[0, \"name\", \"path\", \"desc\", null, null]], \"p\": []}}",
) )
.unwrap(); .unwrap();
assert_eq!(expected, actual); assert_eq!(expected, actual);