Load module documentation

Previously, we could only print documentation for the items listed in
all.html.  With this patch, we also load the documentation for modules
(including crates) from the <module>/index.html file.  Note that items
are preferred over modules if there is documentation for both (e. g.
std::u8 is both a module and a primitive).
This commit is contained in:
Robin Krahl 2020-07-17 08:50:05 +02:00
parent 5d37789837
commit ab1d655d39
2 changed files with 27 additions and 0 deletions

View File

@ -34,6 +34,18 @@ impl Crate {
parser::find_item(self.path.join("all.html"), &name)
.map(|o| o.map(|s| Item::new(name, self.path.join(path::PathBuf::from(s)))))
}
pub fn find_module(&self, item: &[&str]) -> Option<Item> {
let path = self
.path
.join(path::PathBuf::from(item.join("/")))
.join("index.html");
if path.is_file() {
Some(Item::new(item.join("::"), path))
} else {
None
}
}
}
impl Item {

View File

@ -82,6 +82,7 @@ fn find_doc(sources: &[Box<dyn source::Source>], keyword: &str) -> anyhow::Resul
let crate_ = find_crate(sources, parts[0])?;
let item = crate_
.find_item(&parts[1..])?
.or_else(|| crate_.find_module(&parts[1..]))
.with_context(|| format!("Could not find the item {}", keyword))?;
item.load_doc()
}
@ -100,6 +101,8 @@ fn find_crate(sources: &[Box<dyn source::Source>], name: &str) -> anyhow::Result
mod tests {
use std::path;
use crate::source;
pub fn ensure_docs() -> path::PathBuf {
let doc = path::PathBuf::from("./target/doc");
assert!(
@ -108,4 +111,16 @@ mod tests {
);
doc
}
#[test]
fn test_find_doc() {
let path = ensure_docs();
let sources = vec![source::get_source(path).unwrap()];
super::find_doc(&sources, "kuchiki").unwrap();
super::find_doc(&sources, "kuchiki::NodeRef").unwrap();
super::find_doc(&sources, "kuchiki::traits").unwrap();
super::find_doc(&sources, "kachiki").unwrap_err();
super::find_doc(&sources, "kuchiki::NodeDataRef::as_node").unwrap_err();
}
}