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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
use fst::{self, Automaton, IntoStreamer, Map, MapBuilder};
use std::collections::HashSet;
use std::fmt;
use std::iter::FromIterator;
use string_cache::DefaultAtom as Atom;
macro_rules! enum_number {
($name:ident { $($variant:ident | $display:tt | $value:tt, )* }) => {
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub enum $name {
$($variant(Atom),)*
}
impl $name {
pub fn new(tag: usize, data: Atom) -> $name {
match tag {
$( $value => $name::$variant(data), )*
_ => unreachable!()
}
}
pub fn plain(&self) -> &Atom {
match self {
$( $name::$variant(data) => data, )*
}
}
}
impl fmt::Display for $name {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
$( $name::$variant(data) => write!(f, "{}.{}", $display, data), )*
}
}
}
}
}
enum_number!(TypeItem {
Module | "module" | 0,
ExternCrate | "externcrate" | 1,
Import | "import" | 2,
Struct | "struct" | 3,
Enum | "enum" | 4,
Function | "function" | 5,
Typedef | "typedef" | 6,
Static | "static" | 7,
Trait | "trait" | 8,
Impl | "impl" | 9,
TyMethod | "tymethod" | 10,
Method | "method" | 11,
StructField | "structfield" | 12,
Variant | "variant" | 13,
Macro | "macro" | 14,
Primitive | "primitive" | 15,
AssociatedType | "associatedtype" | 16,
Constant | "constant" | 17,
AssociatedConst | "associatedconst" | 18,
Union | "union" | 19,
ForeignType | "foreigntype" | 20,
Keyword | "keyword" | 21,
Existential | "existential" | 22,
});
#[derive(Debug, Eq, Hash, PartialEq)]
pub struct DocItem {
pub name: TypeItem,
pub parent: Option<TypeItem>,
pub path: Atom,
}
impl DocItem {
pub fn new(name: TypeItem, parent: Option<TypeItem>, path: Atom) -> DocItem {
DocItem { name, parent, path }
}
pub fn key(&self) -> &Atom {
self.name.plain()
}
}
impl fmt::Display for DocItem {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
for part in self.path.split("::") {
write!(f, "{}/", part)?;
}
if let Some(ref parent) = self.parent {
write!(f, "{}.html#{}", parent, self.name)
} else {
write!(f, "{}.html", self.name)
}
}
}
#[derive(Debug)]
pub struct RustDoc {
items: HashSet<DocItem>,
}
impl Extend<DocItem> for RustDoc {
fn extend<T: IntoIterator<Item = DocItem>>(&mut self, iter: T) {
for elem in iter {
self.items.insert(elem);
}
}
}
impl FromIterator<DocItem> for RustDoc {
fn from_iter<I: IntoIterator<Item = DocItem>>(iter: I) -> Self {
RustDoc {
items: iter.into_iter().collect(),
}
}
}
impl IntoIterator for RustDoc {
type Item = DocItem;
type IntoIter = ::std::collections::hash_set::IntoIter<DocItem>;
fn into_iter(self) -> Self::IntoIter {
self.items.into_iter()
}
}
impl RustDoc {
pub fn new(items: HashSet<DocItem>) -> RustDoc {
RustDoc { items }
}
pub fn build(self) -> Result<RustDocSeeker, fst::Error> {
let mut builder = MapBuilder::memory();
let mut items: Vec<_> = self.items.into_iter().collect();
if items.len() > 0 {
items.sort_unstable_by(|a, b| a.key().cmp(b.key()));
let mut name = items[0].key();
let mut start = 0;
for idx in 1..items.len() {
if name != items[idx].key() {
builder.insert(name.as_bytes(), ((start as u64) << 32) + idx as u64)?;
name = items[idx].key();
start = idx;
};
}
builder.insert(name.as_bytes(), ((start as u64) << 32) + items.len() as u64)?;
}
let index = Map::from_bytes(builder.into_inner()?)?;
Ok(RustDocSeeker { items, index })
}
}
#[derive(Debug)]
pub struct RustDocSeeker {
items: Vec<DocItem>,
index: Map,
}
impl RustDocSeeker {
pub fn search<A: Automaton>(&self, aut: &A) -> impl Iterator<Item = &DocItem> {
let result = self.index.search(aut).into_stream().into_values();
result.into_iter().flat_map(move |idx| {
let start = (idx >> 32) as usize;
let end = (idx & 0xffffffff) as usize;
&self.items[start..end]
})
}
}