docsearch 0.3.5

Resolve crate items to rustdoc URLs.
Documentation
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
//! Handling of the index data and its transformation in a more usable format as well as a mapping
//! of simple paths to rustdoc URL.

use std::{
    collections::{BTreeMap, HashMap},
    fmt,
};

use serde::{
    de::{SeqAccess, Visitor},
    Deserialize, Deserializer,
};
use serde_repr::Deserialize_repr;

use crate::error::{Error, Result};

#[cfg(feature = "index-v1")]
mod v1;
#[cfg(feature = "index-v2")]
mod v2;

#[cfg_attr(test, derive(Clone, Copy, Eq, PartialEq, serde::Serialize))]
enum Version {
    #[cfg(feature = "index-v1")]
    V1,
    #[cfg(feature = "index-v2")]
    V2,
    V3,
}

impl Version {
    fn detect(index: &str) -> Option<Self> {
        #[cfg(feature = "index-v1")]
        if index.starts_with(r#"var N=null,E="",T="t",U="u",searchIndex={};"#) {
            return Some(Self::V1);
        }

        #[cfg(feature = "index-v2")]
        if index.ends_with(r#"addSearchOptions(searchIndex);initSearch(searchIndex);"#) {
            return Some(Self::V2);
        }

        if index.ends_with(r#"if (window.initSearch) {window.initSearch(searchIndex)};"#)
            || index.trim_end().ends_with(
                r#"if (typeof exports !== 'undefined') {exports.searchIndex = searchIndex};"#,
            )
        {
            Some(Self::V3)
        } else {
            None
        }
    }
}

/// Whole index data after transformation.
#[cfg_attr(test, derive(PartialEq, Eq, serde::Serialize))]
struct IndexData {
    /// Mapping from crate name to data.
    crates: HashMap<String, CrateData>,
}

/// Crate data after transformation.
#[cfg_attr(test, derive(PartialEq, Eq, serde::Serialize))]
struct CrateData {
    /// Doc string of the crate.
    #[allow(dead_code)]
    doc: String,
    /// Data for each individual item of the crate.
    items: Vec<IndexItem>,
    /// Parent paths that help to construct full paths and URLs from item information.
    paths: Vec<(ItemType, String)>,
    // aliases
}

/// Index data for a single item after transformation.
///
/// Taken from: <https://github.com/rust-lang/rust/blob/eba3228b2a9875d268ff3990903d04e19f6cdb0c/src/librustdoc/html/render/mod.rs#L84>.
#[cfg_attr(test, derive(PartialEq, Eq, serde::Serialize))]
struct IndexItem {
    /// The type of item.
    ty: ItemType,
    /// Simple name without path.
    name: String,
    /// Resolved, full path.
    path: String,
    /// Short, one line description. Can contain HTML tags and is likely truncated with the `…`
    /// character.
    #[allow(dead_code)]
    desc: String,
    /// Index to the parent item, if it belongs to another item.
    parent_idx: Option<usize>,
    // search_type
}

/// Different item types that can appear in the rust docs to identify the kind of item.
///
/// Taken from: <https://github.com/rust-lang/rust/blob/eba3228b2a9875d268ff3990903d04e19f6cdb0c/src/librustdoc/formats/item_type.rs>.
#[derive(Clone, Copy, Debug, Deserialize_repr)]
#[cfg_attr(test, derive(PartialEq, Eq, serde::Serialize))]
#[repr(u8)]
enum ItemType {
    Module = 0,
    ExternCrate = 1,
    Import = 2,
    Struct = 3,
    Enum = 4,
    Function = 5,
    Typedef = 6,
    Static = 7,
    Trait = 8,
    Impl = 9,
    TyMethod = 10,
    Method = 11,
    StructField = 12,
    Variant = 13,
    Macro = 14,
    Primitive = 15,
    AssocType = 16,
    Constant = 17,
    AssocConst = 18,
    Union = 19,
    ForeignType = 20,
    Keyword = 21,
    OpaqueTy = 22,
    ProcAttribute = 23,
    ProcDerive = 24,
    TraitAlias = 25,
}

impl ItemType {
    const fn as_str(self) -> &'static str {
        match self {
            Self::Module => "mod",
            Self::ExternCrate => "externcrate",
            Self::Import => "import",
            Self::Struct => "struct",
            Self::Union => "union",
            Self::Enum => "enum",
            Self::Function => "fn",
            Self::Typedef => "type",
            Self::Static => "static",
            Self::Trait => "trait",
            Self::Impl => "impl",
            Self::TyMethod => "tymethod",
            Self::Method => "method",
            Self::StructField => "structfield",
            Self::Variant => "variant",
            Self::Macro => "macro",
            Self::Primitive => "primitive",
            Self::AssocType => "associatedtype",
            Self::Constant => "constant",
            Self::AssocConst => "associatedconstant",
            Self::ForeignType => "foreigntype",
            Self::Keyword => "keyword",
            Self::OpaqueTy => "opaque",
            Self::ProcAttribute => "attr",
            Self::ProcDerive => "derive",
            Self::TraitAlias => "traitalias",
        }
    }

    const fn from_raw(value: u8) -> Option<Self> {
        Some(match value {
            0 => Self::Module,
            1 => Self::ExternCrate,
            2 => Self::Import,
            3 => Self::Struct,
            4 => Self::Enum,
            5 => Self::Function,
            6 => Self::Typedef,
            7 => Self::Static,
            8 => Self::Trait,
            9 => Self::Impl,
            10 => Self::TyMethod,
            11 => Self::Method,
            12 => Self::StructField,
            13 => Self::Variant,
            14 => Self::Macro,
            15 => Self::Primitive,
            16 => Self::AssocType,
            17 => Self::Constant,
            18 => Self::AssocConst,
            19 => Self::Union,
            20 => Self::ForeignType,
            21 => Self::Keyword,
            22 => Self::OpaqueTy,
            23 => Self::ProcAttribute,
            24 => Self::ProcDerive,
            25 => Self::TraitAlias,
            _ => return None,
        })
    }
}

/// The whole index data for a crate. It usually contains only one entry for the crate it was
/// generated for. The stdlib index is a special case where multiple crates like `std` and `alloc`
/// are included.
#[derive(Debug, Deserialize)]
#[cfg_attr(test, derive(PartialEq, Eq, serde::Serialize))]
struct RawIndexData {
    /// Mapping from crate name to raw index data.
    #[serde(flatten)]
    crates: HashMap<String, RawCrateData>,
}

/// Crate index data in its raw form. All elements are vectors and the same index over all of them
/// contain the information for a single item.
///
/// Taken from: <https://github.com/rust-lang/rust/blob/eba3228b2a9875d268ff3990903d04e19f6cdb0c/src/librustdoc/html/render/cache.rs#L121>.
#[derive(Debug, Deserialize)]
#[cfg_attr(test, derive(PartialEq, Eq, serde::Serialize))]
struct RawCrateData {
    /// Doc string for the crate. Seems to always be `github\u{2002}crates-io\u{2002}docs-rs`.
    doc: String,
    /// Type of item.
    #[serde(deserialize_with = "t")]
    t: Vec<ItemType>,
    /// Simple name without the path.
    n: Vec<String>,
    /// Module path of the item. This uses previous items as reference and an empty value means to
    /// use the value of the previous item. Similar to being still in the same _directory_.
    #[serde(deserialize_with = "q")]
    q: BTreeMap<usize, String>,
    /// Short, one line description of the item. Maybe contain HTML tags and is likely truncated.
    d: Vec<String>,
    /// Index of the parent item. For example if the item is a method, it references the index of
    /// the struct/enum/... it belongs to.
    ///
    /// A value of `0` means that no parent exists. Therefore, indexes start at `1` and need to be
    /// adjusted to access the right item in the other vectors.
    i: Vec<usize>,
    // f: search type
    /// Further information about the parent item that helps in constructing the full path of an
    /// item with parent.
    ///
    /// For example a method `baz` as part of the struct `Bar` in the module `foo` will only have
    /// the basic path `foo` as the [`Self::q`] value only describes module paths. This field
    /// contains the parent name `Bar` (and its item type) so that the full path `foo::Bar::baz` can
    /// be constructed.
    p: Vec<(ItemType, String)>,
    // a: aliases
}

/// Parse and transform a raw index file and convert it into mappings from paths to URLs that can be
/// used to generate permalinks to the items' docs page.
///
/// This is the combination of the internal functions [`load_raw`], [`transform`] and
/// [`generate_mapping`].
pub fn load(index: &str) -> Result<HashMap<String, BTreeMap<String, String>>> {
    let raw = match Version::detect(index) {
        Some(Version::V3) => load_raw(index)?,
        #[cfg(feature = "index-v2")]
        Some(Version::V2) => v2::load_raw(index)?,
        #[cfg(feature = "index-v1")]
        Some(Version::V1) => v1::load_raw(index)?,
        None => return Err(Error::UnsupportedIndexVersion),
    };

    Ok(generate_mapping(transform(raw)))
}

/// Extract the JSON content from the index data and run it through [`serde`] to transform it into
/// usable data structures.
///
/// The index data looks basically as follows:
///
/// ```js
/// var searchIndex = JSON.parse('{\
/// "cratename":{"doc":"...","t":[1],"n":["Name"],"q":["path"],"d":[""],"i":[0],"f":[null],"p":[]}\
/// }');
/// if (window.initSearch) {window.initSearch(searchIndex)};
/// ```
///
/// After the initial JavaScript line, the file contains one line of JSON data for each crate
/// contained in the index. These are extracted and the surrounding `{` and `}` delimiters added
/// again to create a valid JSON object.
///
/// For further explanation of the individual fields of a single crate entry, looks at the docs of
/// [`RawIndexData`] and [`RawCrateData`].
fn load_raw(index: &str) -> Result<RawIndexData> {
    let json = {
        let mut json = index
            .lines()
            .filter_map(|l| {
                if l.starts_with('"') {
                    l.strip_suffix('\\')
                } else {
                    None
                }
            })
            .fold(String::from("{"), |mut json, l| {
                json.push_str(l);
                json
            });
        json.push('}');

        // Inverse operation of:
        // <https://github.com/rust-lang/rust/blob/eba3228b2a9875d268ff3990903d04e19f6cdb0c/src/librustdoc/html/render/cache.rs#L175-L190>.
        json.replace("\\\\\"", "\\\"")
            .replace(r"\'", "'")
            .replace(r"\\", r"\")
    };

    serde_json::from_str(&json).map_err(Into::into)
}

/// Convert from the index data into a more usable data structure that contains one full data set
/// for each item of the crate.
///
/// The raw structure contains a data driven layout (likely to reduce size of the JSON format) what
/// means that the data for a single entry is not contained in a single object but instead spread
/// over vectors of data, each one representing one field.
///
/// Data for a single entry can be retrieved by index and this transformation does exactly that to
/// get back a whole structure of information for each item.
///
/// ## Implementation
///
/// The separate elements of each item are combined back together with the [`Iterator::zip`] method.
/// A nice side effect is that we don't have to cope for differences in vector sizes (which should
/// not exist but can theoretically) as it stops as soon as one of the iterators returns [`None`].
///
/// The path field is only present if it changes compared to the previous item to reduce index size.
/// The previous path is kept around thanks to the [`Iterator::fold`] method and only updated if the
/// current path is present. Otherwise the old value is used. This increases data size but makes
/// usage much more convenient and less error prone as the path doesn't need to be searched every
/// time it is accessed.
///
/// Parent indexes are transformed from a `usize` into an `Option<usize>` to erase the special
/// handling of the `0` value and indexes are reduced by `1` to allow proper indexing.
fn transform(raw: RawIndexData) -> IndexData {
    IndexData {
        crates: raw
            .crates
            .into_iter()
            .map(|(name, mut raw_data)| {
                let length = raw_data.t.len();
                let (items, _) = raw_data
                    .t
                    .into_iter()
                    .enumerate()
                    .zip(raw_data.n.into_iter())
                    .zip(raw_data.d.into_iter())
                    .zip(raw_data.i.into_iter())
                    .fold(
                        (Vec::with_capacity(length), String::new()),
                        |(mut items, path), ((((pos, t), n), d), i)| {
                            let path = raw_data.q.remove(&pos).unwrap_or(path);
                            items.push(IndexItem {
                                ty: t,
                                name: n,
                                path: path.clone(),
                                desc: d,
                                parent_idx: if i > 0 { Some(i - 1) } else { None },
                            });
                            (items, path)
                        },
                    );

                (
                    name,
                    CrateData {
                        doc: raw_data.doc,
                        items,
                        paths: raw_data.p,
                    },
                )
            })
            .collect(),
    }
}

/// Generate a mapping from the transformed index data. This simply calls [`generate_crate_mapping`]
/// for each crate in the index to do the actual transformation of item data.
fn generate_mapping(data: IndexData) -> HashMap<String, BTreeMap<String, String>> {
    data.crates
        .into_iter()
        .map(|(name, data)| (name, generate_crate_mapping(data)))
        .collect()
}

/// Generate the simple path for each item in the crate data and its URL variant as used by
/// `rustdoc`. This allows to get a direct mapping from simple path to URL path, which can further
/// be used to create a permalink to the rustdoc page.
///
/// ## Implementation
///
/// The path is usually in the form of `<module>::<item>` where the module path can contain further
/// `::`. If the item has a parent its form is `<module::<parent_item>::<item>`.
///
/// The URL path is slightly different, with additional information about the type. The basic form
/// is `<module>/<type>.<item>.html` where the module can contain further slashes `/` and the type
/// defines the item type like `Struct`, `Enum` and others.
///
/// If the item has a parent its form is `<module>/<parent_type>.<parent_item>.html#<type>.<item>`.
/// The original type/item combination is replaced with the parent information and the actual item
/// part is moved into a path fragment to become an anchor. That is, because an item with parent
/// doesn't have its own page but is a part of the parents page.
fn generate_crate_mapping(data: CrateData) -> BTreeMap<String, String> {
    let paths = data.paths;

    data.items
        .into_iter()
        .map(|item| {
            let full_path = if let Some(idx) = item.parent_idx {
                format!("{}::{}::{}", item.path, paths[idx].1, item.name)
            } else {
                format!("{}::{}", item.path, item.name)
            };

            let url = if let Some(parent) = item.parent_idx.map(|i| &paths[i]) {
                format!(
                    "{}/{}.{}.html#{}.{}",
                    item.path.replace("::", "/"),
                    parent.0.as_str(),
                    parent.1,
                    item.ty.as_str(),
                    item.name
                )
            } else {
                format!(
                    "{}/{}.{}.html",
                    item.path.replace("::", "/"),
                    item.ty.as_str(),
                    item.name
                )
            };

            (full_path, url)
        })
        .collect()
}

fn t<'de, D>(deserializer: D) -> Result<Vec<ItemType>, D::Error>
where
    D: Deserializer<'de>,
{
    deserializer.deserialize_any(VecItemTypeVisitor)
}

struct VecItemTypeVisitor;

impl<'de> Visitor<'de> for VecItemTypeVisitor {
    type Value = Vec<ItemType>;

    fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str("item types either as an array of IDs or a string of ASCII chars")
    }

    fn visit_str<E>(self, v: &str) -> std::result::Result<Self::Value, E>
    where
        E: serde::de::Error,
    {
        v.bytes()
            .map(|ascii| {
                ascii
                    .is_ascii_uppercase()
                    .then(|| ItemType::from_raw(ascii - b'A'))
                    .flatten()
                    .ok_or_else(|| {
                        E::custom(format!("invalid ASCII character `{}`", ascii as char))
                    })
            })
            .collect()
    }

    fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
    where
        A: SeqAccess<'de>,
    {
        let mut list = Vec::with_capacity(seq.size_hint().unwrap_or(0));

        while let Some(element) = seq.next_element()? {
            list.push(element);
        }

        Ok(list)
    }
}

fn q<'de, D>(deserializer: D) -> Result<BTreeMap<usize, String>, D::Error>
where
    D: Deserializer<'de>,
{
    deserializer.deserialize_seq(VecPathVisitor)
}

struct VecPathVisitor;

impl<'de> Visitor<'de> for VecPathVisitor {
    type Value = BTreeMap<usize, String>;

    fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str("item types either as an array of IDs or a string of ASCII chars")
    }

    fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
    where
        A: SeqAccess<'de>,
    {
        #[derive(Deserialize)]
        #[serde(untagged)]
        enum Value {
            String(String),
            Tuple((usize, String)),
        }

        let mut map = BTreeMap::new();
        let mut position = 0;

        while let Some(element) = seq.next_element::<Value>()? {
            let (key, value) = match element {
                Value::String(name) => {
                    if name.is_empty() {
                        position += 1;
                        continue;
                    }
                    (position, name)
                }
                Value::Tuple((position, name)) => (position, name),
            };

            map.insert(key, value);
            position += 1;
        }

        Ok(map)
    }
}

#[cfg(test)]
mod tests {
    use std::fs;

    use insta::glob;
    use serde_test::Token;

    use super::*;

    #[test]
    fn test_version_detect() {
        glob!("fixtures/*.js", |path| {
            let input = fs::read_to_string(path).unwrap();
            let data = Version::detect(&input);
            insta::assert_yaml_snapshot!(data);
        });
    }

    #[allow(clippy::bind_instead_of_map)]
    #[test]
    fn test_load_raw() {
        glob!("fixtures/*.js", |path| {
            let input = fs::read_to_string(path).unwrap();
            let data = Version::detect(&input).and_then(|v| match v {
                #[cfg(feature = "index-v1")]
                Version::V1 => Some(v1::load_raw(&input).unwrap()),
                #[cfg(feature = "index-v2")]
                Version::V2 => Some(v2::load_raw(&input).unwrap()),
                Version::V3 => Some(load_raw(&input).unwrap()),
            });
            insta::assert_yaml_snapshot!(data);
        });
    }

    #[allow(clippy::bind_instead_of_map)]
    #[test]
    fn test_transform() {
        glob!("fixtures/*.js", |path| {
            let input = fs::read_to_string(path).unwrap();
            let data = Version::detect(&input)
                .and_then(|v| match v {
                    #[cfg(feature = "index-v1")]
                    Version::V1 => Some(v1::load_raw(&input).unwrap()),
                    #[cfg(feature = "index-v2")]
                    Version::V2 => Some(v2::load_raw(&input).unwrap()),
                    Version::V3 => Some(load_raw(&input).unwrap()),
                })
                .map(transform);
            insta::assert_yaml_snapshot!(data);
        });
    }

    #[allow(clippy::bind_instead_of_map)]
    #[test]
    fn test_generate_mapping() {
        glob!("fixtures/*.js", |path| {
            let input = fs::read_to_string(path).unwrap();
            let data = Version::detect(&input)
                .and_then(|v| match v {
                    #[cfg(feature = "index-v1")]
                    Version::V1 => Some(v1::load_raw(&input).unwrap()),
                    #[cfg(feature = "index-v2")]
                    Version::V2 => Some(v2::load_raw(&input).unwrap()),
                    Version::V3 => Some(load_raw(&input).unwrap()),
                })
                .map(transform)
                .map(generate_mapping);
            insta::assert_yaml_snapshot!(data);
        });
    }

    #[test]
    fn test_t() {
        #[derive(Debug, PartialEq, Deserialize)]
        struct Wrapper {
            #[serde(deserialize_with = "t")]
            value: Vec<ItemType>,
        }

        let wrapper = Wrapper {
            value: vec![ItemType::Module],
        };

        serde_test::assert_de_tokens(
            &wrapper,
            &[
                Token::Struct {
                    name: "Wrapper",
                    len: 1,
                },
                Token::Str("value"),
                Token::Str("A"),
                Token::StructEnd,
            ],
        );
        serde_test::assert_de_tokens(
            &wrapper,
            &[
                Token::Struct {
                    name: "Wrapper",
                    len: 1,
                },
                Token::Str("value"),
                Token::Seq { len: Some(1) },
                Token::I64(0),
                Token::SeqEnd,
                Token::StructEnd,
            ],
        );
    }

    #[test]
    fn test_q() {
        #[derive(Debug, PartialEq, Deserialize)]
        struct Wrapper {
            #[serde(deserialize_with = "q")]
            value: BTreeMap<usize, String>,
        }

        let wrapper = Wrapper {
            value: [(0, "test".to_owned()), (2, "test::two".to_owned())].into(),
        };

        serde_test::assert_de_tokens(
            &wrapper,
            &[
                Token::Struct {
                    name: "Wrapper",
                    len: 1,
                },
                Token::Str("value"),
                Token::Seq { len: Some(2) },
                Token::Seq { len: Some(2) },
                Token::I64(0),
                Token::Str("test"),
                Token::SeqEnd,
                Token::Seq { len: Some(2) },
                Token::I64(2),
                Token::Str("test::two"),
                Token::SeqEnd,
                Token::SeqEnd,
                Token::StructEnd,
            ],
        );
        serde_test::assert_de_tokens(
            &wrapper,
            &[
                Token::Struct {
                    name: "Wrapper",
                    len: 1,
                },
                Token::Str("value"),
                Token::Seq { len: Some(3) },
                Token::Str("test"),
                Token::Str(""),
                Token::Str("test::two"),
                Token::SeqEnd,
                Token::StructEnd,
            ],
        );
    }
}