Skip to content

Commit c732674

Browse files
committed
refactor(cli/topical-doc): clean up some funtions
1 parent b429f94 commit c732674

File tree

1 file changed

+12
-19
lines changed

1 file changed

+12
-19
lines changed

src/cli/topical_doc.rs

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::ffi::OsString;
22
use std::fs;
33
use std::path::{Path, PathBuf};
44

5-
use anyhow::{anyhow, bail, Context, Result};
5+
use anyhow::{anyhow, Context, Result};
66

77
struct DocData<'a> {
88
topic: &'a str,
@@ -19,13 +19,10 @@ fn index_html(doc: &DocData<'_>, wpath: &Path) -> Option<PathBuf> {
1919
}
2020

2121
fn dir_into_vec(dir: &Path) -> Result<Vec<OsString>> {
22-
let entries = fs::read_dir(dir).with_context(|| format!("Failed to read_dir {dir:?}"))?;
23-
let mut v = Vec::new();
24-
for entry in entries {
25-
let entry = entry?;
26-
v.push(entry.file_name());
27-
}
28-
Ok(v)
22+
fs::read_dir(dir)
23+
.with_context(|| format!("Failed to read_dir {dir:?}"))?
24+
.map(|f| Ok(f?.file_name()))
25+
.collect()
2926
}
3027

3128
fn search_path(doc: &DocData<'_>, wpath: &Path, keywords: &[&str]) -> Result<PathBuf> {
@@ -118,18 +115,14 @@ pub(crate) fn local_path(root: &Path, topic: &str) -> Result<PathBuf> {
118115
// topic.split.count cannot be 0
119116
let subpath_os_path = match topic_vec.len() {
120117
1 => match topic {
121-
"std" | "core" | "alloc" => match index_html(&doc, &work_path) {
122-
Some(f) => f,
123-
None => bail!(format!("No document for '{}'", doc.topic)),
124-
},
125-
_ => {
126-
let std = PathBuf::from("std");
127-
let search_keywords = match forced_keyword {
128-
Some(k) => k,
129-
None => keywords_top,
130-
};
131-
search_path(&doc, &std, &search_keywords)?
118+
"std" | "core" | "alloc" => {
119+
index_html(&doc, &work_path).context(anyhow!("No document for '{}'", doc.topic))?
132120
}
121+
_ => search_path(
122+
&doc,
123+
Path::new("std"),
124+
&forced_keyword.unwrap_or(keywords_top),
125+
)?,
133126
},
134127
2 => match index_html(&doc, &work_path) {
135128
Some(f) => f,

0 commit comments

Comments
 (0)