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 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
use fst::{self, Automaton, IntoStreamer, Map, MapBuilder};
use std::collections::HashSet;
use std::fmt;
use std::hash::{Hash, Hasher};
use std::iter::FromIterator;
use string_cache::DefaultAtom as Atom;
macro_rules! enum_number {
($name:ident { $($variant:ident | $display:tt | $value:tt, )* }) => {
/// TypeItem represent an item with type,
/// Use `Display` to get the `type dot name` format of the item
///
/// # Example
/// ```
/// assert_eq!("module.vec", TypeItme::Module(vec));
/// assert_eq!("macro.vec", TypeItme::Macro(vec));
///
/// assert_eq!("fn.vec", TypeItme::Function(vec)); // the only two exceptions
/// assert_eq!("type.vec", TypeItme::Typedef(vec)); // the only two exceptions
/// ```
#[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!()
}
}
}
impl AsRef<Atom> for $name {
fn as_ref(&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 | "fn" | 5,
Typedef | "type" | 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,
});
/// DocItem represent a searchable item,
/// Use `Display` to get the relative URI of the item
///
/// eg:
///
/// The `dedup` (name) function of the `Vec`(parent) struct in `std::vec`(path) module.
///
/// The `Vec`(name) struct of `None`(parent) in `std::vec`(path) module.
///
/// The `vec`(name) module of `None`(parent) in `std`(path) module.
///
/// # Example
/// ```
/// println!("{} is the url of {:?}", &docitem, &docitem)
/// ```
#[derive(Debug, Eq)]
pub struct DocItem {
pub name: TypeItem,
pub parent: Option<TypeItem>,
pub path: Atom,
pub desc: Atom,
}
impl DocItem {
pub fn new(name: TypeItem, parent: Option<TypeItem>, path: Atom, desc: Atom) -> DocItem {
DocItem {
name,
parent,
path,
desc,
}
}
/// Return the key of the DocItem for index
pub fn key(&self) -> &[u8] {
self.name.as_ref().as_bytes()
}
}
impl PartialEq for DocItem {
fn eq(&self, other: &DocItem) -> bool {
self.name == other.name && self.parent == other.parent && self.path == other.path
}
}
impl Hash for DocItem {
fn hash<H: Hasher>(&self, state: &mut H) {
self.name.hash(state);
self.parent.hash(state);
self.path.hash(state);
}
}
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 {
match &self.name {
TypeItem::Module(name) => write!(f, "{}/index.html", name),
_ => write!(f, "{}.html", self.name),
}
}
}
}
/// RustDoc contains DocItems, which could be convert to RustDocSeeker
///
/// # Example
///
/// ```
/// let data = fs::read_to_string("search-index.js").unwrap();
/// let rustdoc: RustDoc = data.parse().unwrap();
///
/// // let's combine RustDoc
/// rustdoc_a.extend(rustdoc_b)
/// ```
#[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 }
}
/// Build an index for searching
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, ((start as u64) << 32) + idx as u64)?;
name = items[idx].key();
start = idx;
};
}
builder.insert(name, ((start as u64) << 32) + items.len() as u64)?;
}
let index = Map::from_bytes(builder.into_inner()?)?;
Ok(RustDocSeeker { items, index })
}
}
/// RustDocSeeker contains DocItems and Index for fast searching
///
/// The index is kv-map fro <name, idx: u64 = (start: u32 << 32) + end: u32>
/// where items[start..end] have the same DocItem.name.
///
/// # Example
///
/// ```
/// let seeker = rustdoc.build().unwrap();
/// ```
#[derive(Debug)]
pub struct RustDocSeeker {
items: Vec<DocItem>,
index: Map,
}
impl RustDocSeeker {
/// Search with fst::Automaton, read fst::automaton / fst-levenshtein / fst-regex for details.
///
/// # Example
///
/// ```
/// let aut = fst_regex::Regex::new(".*dedup.*").unwrap();
/// for i in seeker.search(aut) {
/// println!("{:?}", i);
/// }
///
/// let aut = fst_levenshtein::Levenshtein::new("dedXp", 1).unwrap();
/// for i in seeker.search(aut) {
/// println!("{:?}", i);
/// }
///
///
/// let aut = fst::automaton::Subsequence::new("dedup", 1).unwrap();
/// for i in seeker.search(aut) {
/// println!("{:?}", i);
/// }
///
/// ```
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]
})
}
}