1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#[macro_use]
extern crate serde;
#[macro_use]
extern crate thiserror;
pub const RUSTDOC_VERSION: &str = "rustdoc 1.53.0-nightly (132b4e5d1 2021-04-13)";
pub mod doc;
pub mod location;
pub mod search_index;
use rayon::prelude::*;
use std::{
fs::File,
io::{stdout, BufRead, BufReader, BufWriter, Write},
path::Path
};
#[derive(Debug, Error)]
pub enum Error {
#[error("Not supported format of search-index.json")]
InvalidFormat(String),
#[error(transparent)]
Io(#[from] std::io::Error),
#[error(transparent)]
SerdeJson(#[from] serde_json::error::Error),
#[error(transparent)]
Metadata(#[from] cargo_metadata::Error),
#[error(transparent)]
Join(#[from] tokio::task::JoinError),
#[error(transparent)]
Location(#[from] location::LocationError)
}
pub fn read_search_index_and_show<P: AsRef<Path>>(src: P) -> Result<(), Error> {
let doc = {
let file = File::open(src.as_ref())?;
let reader = BufReader::new(file);
let mut lines = reader.lines();
lines.next();
lines
.par_bridge()
.map(|l| l.map_err(Error::from))
.filter(|l| {
if let Ok(l) = &l {
l != "}');"
&& l != "initSearch(searchIndex);"
&& l != "if (window.initSearch) {window.initSearch(searchIndex)};"
} else {
true
}
})
.map(|l: Result<String, Error>| l.and_then(parse_line))
};
{
doc.try_for_each(|r: Result<(String, doc::Crate), Error>| -> Result<(), _> {
let out = stdout();
let mut out = BufWriter::new(out.lock());
r.and_then(|(_name, krate)| -> Result<_, _> {
for path in krate.items() {
writeln!(out, "{}", path)?;
}
Ok(())
})
})
.unwrap();
}
Ok(())
}
pub fn parse_line(line: String) -> Result<(String, doc::Crate), Error> {
let mut line = {
let mut line = line;
line.pop();
if &line[(line.len() - 1)..] == "," {
line.pop();
}
line
};
let colon_idx = line
.find(':')
.ok_or_else(|| Error::InvalidFormat(line.clone()))?;
let (mut name_colon, body) = {
let body = line.split_off(colon_idx + 1);
(line, body)
};
let mut quoted_name = {
let _colon = name_colon.split_off(colon_idx);
name_colon
};
let name = {
quoted_name.pop();
quoted_name.split_off(1)
};
let body = unescape::unescape(&body).ok_or_else(|| Error::InvalidFormat(body.clone()))?;
let krate: doc::Crate = serde_json::from_str(&body)?;
Ok((name, krate))
}