From e1ef283159e01b09a6d35788b359844d188a34dc Mon Sep 17 00:00:00 2001 From: Robin Krahl Date: Mon, 20 Jul 2020 00:36:54 +0200 Subject: [PATCH] Check parent for search index items MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- src/index.rs | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/index.rs b/src/index.rs index db1568e..643d41b 100644 --- a/src/index.rs +++ b/src/index.rs @@ -52,6 +52,8 @@ struct Data { struct CrateData { #[serde(rename = "i")] items: Vec, + #[serde(rename = "p")] + paths: Vec<(usize, String)>, } #[derive(Debug, Default, PartialEq, serde_tuple::Deserialize_tuple)] @@ -119,11 +121,18 @@ impl Index { } else { &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) { matches.push(IndexItem { name: item.name.clone(), - path: path.to_owned(), + path: full_path, description: item.desc.clone(), }); } @@ -152,7 +161,7 @@ mod tests { expected .crates .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); } @@ -167,7 +176,7 @@ mod tests { krate.items.push(item); expected.crates.insert("test".to_owned(), krate); 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(); assert_eq!(expected, actual);