crate-api 0.0.1

Interact with the crate's API
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
use std::collections::HashMap;
use std::collections::VecDeque;

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct RustDocBuilder {
    deps: bool,
    target_directory: Option<std::path::PathBuf>,
    silence: bool,
    color: Option<bool>,
}

impl RustDocBuilder {
    pub fn new() -> Self {
        Self {
            deps: false,
            target_directory: None,
            silence: false,
            color: None,
        }
    }

    /// Include dependencies
    ///
    /// Reasons to have this disabled:
    /// - Faster API extraction
    /// - Less likely to hit bugs in rustdoc, like
    ///   - rust-lang/rust#89097
    ///   - rust-lang/rust#83718
    ///
    /// Reasons to have this enabled:
    /// - Check for accidental inclusion of dependencies in your API
    /// - Detect breaking changes from dependencies in your API
    pub fn deps(mut self, yes: bool) -> Self {
        self.deps = yes;
        self
    }

    pub fn target_directory(mut self, path: impl Into<std::path::PathBuf>) -> Self {
        self.target_directory = Some(path.into());
        self
    }

    /// Don't write progress to stderr
    pub fn silence(mut self, yes: bool) -> Self {
        self.silence = yes;
        self
    }

    /// Whether stderr can be colored
    pub fn color(mut self, yes: impl Into<Option<bool>>) -> Self {
        self.color = yes.into();
        self
    }

    pub fn dump_raw(self, manifest_path: &std::path::Path) -> Result<String, crate::Error> {
        let manifest = std::fs::read_to_string(manifest_path).map_err(|e| {
            crate::Error::new(
                crate::ErrorKind::ApiParse,
                format!("Failed when reading {}: {}", manifest_path.display(), e),
            )
        })?;
        let manifest: toml_edit::Document = manifest.parse().map_err(|e| {
            crate::Error::new(
                crate::ErrorKind::ApiParse,
                format!("Failed to parse {}: {}", manifest_path.display(), e),
            )
        })?;
        let crate_name = manifest["package"]["name"].as_str().ok_or_else(|| {
            crate::Error::new(
                crate::ErrorKind::ApiParse,
                format!(
                    "Failed to parse {}: invalid package.name",
                    manifest_path.display()
                ),
            )
        })?;

        let manifest_target_directory;
        let target_dir = if let Some(target_dir) = self.target_directory.as_deref() {
            target_dir
        } else {
            let metadata = cargo_metadata::MetadataCommand::new()
                .manifest_path(manifest_path)
                .no_deps()
                .exec()
                .map_err(|e| crate::Error::new(crate::ErrorKind::ApiParse, e))?;
            manifest_target_directory = metadata
                .target_directory
                .as_path()
                .as_std_path()
                // HACK: Avoid potential errors when mixing toolchains
                .join("crate-api/target");
            manifest_target_directory.as_path()
        };

        let stderr = if self.silence {
            std::process::Stdio::piped()
        } else {
            // Print cargo doc progress
            std::process::Stdio::inherit()
        };

        let mut cmd = std::process::Command::new("cargo");
        cmd.env(
            "RUSTDOCFLAGS",
            "-Z unstable-options --document-hidden-items --output-format=json",
        )
        .stdout(std::process::Stdio::null()) // Don't pollute cargo api output
        .stderr(stderr)
        .args(["+nightly", "doc", "--all-features"])
        .arg("--manifest-path")
        .arg(manifest_path)
        .arg("--target-dir")
        .arg(target_dir);
        if !self.deps {
            cmd.arg("--no-deps");
        }
        if let Some(color) = self.color {
            if color {
                cmd.arg("--color=always");
            } else {
                cmd.arg("--color=never");
            }
        }

        let output = cmd
            .output()
            .map_err(|e| crate::Error::new(crate::ErrorKind::ApiParse, e))?;
        if !output.status.success() {
            let message = if self.silence {
                format!(
                    "Failed when running cargo-doc on {}: {}",
                    manifest_path.display(),
                    String::from_utf8_lossy(&output.stderr)
                )
            } else {
                format!(
                    "Failed when running cargo-doc on {}. See stderr.",
                    manifest_path.display(),
                )
            };
            return Err(crate::Error::new(crate::ErrorKind::ApiParse, message));
        }

        let json_path = target_dir.join(format!("doc/{}.json", crate_name));
        std::fs::read_to_string(&json_path).map_err(|e| {
            crate::Error::new(
                crate::ErrorKind::ApiParse,
                format!("Failed when loading {}: {}", json_path.display(), e),
            )
        })
    }

    pub fn into_api(self, manifest_path: &std::path::Path) -> Result<crate::Api, crate::Error> {
        let raw = self.dump_raw(manifest_path)?;
        parse_raw(&raw, manifest_path)
    }
}

impl Default for RustDocBuilder {
    fn default() -> Self {
        Self::new()
    }
}

pub fn parse_raw(raw: &str, manifest_path: &std::path::Path) -> Result<crate::Api, crate::Error> {
    RustDocParser::new().parse(raw, manifest_path)
}

#[derive(Default)]
struct RustDocParser {
    unprocessed: VecDeque<(Option<crate::PathId>, rustdoc_json_types_fork::Id)>,
    deferred_imports: Vec<(crate::PathId, String, rustdoc_json_types_fork::Id)>,

    api: crate::Api,
    crate_ids: HashMap<u32, Option<crate::CrateId>>,
    path_ids: HashMap<rustdoc_json_types_fork::Id, Option<crate::PathId>>,
    item_ids: HashMap<rustdoc_json_types_fork::Id, Option<crate::ItemId>>,
}

impl RustDocParser {
    fn new() -> Self {
        Self::default()
    }

    fn parse(
        mut self,
        raw: &str,
        manifest_path: &std::path::Path,
    ) -> Result<crate::Api, crate::Error> {
        let raw: rustdoc_json_types_fork::Crate = serde_json::from_str(raw).map_err(|e| {
            crate::Error::new(
                crate::ErrorKind::ApiParse,
                format!(
                    "Failed when parsing json for {}: {}",
                    manifest_path.display(),
                    e
                ),
            )
        })?;

        self.unprocessed.push_back((None, raw.root.clone()));
        while let Some((parent_path_id, raw_item_id)) = self.unprocessed.pop_front() {
            let raw_item = raw
                .index
                .get(&raw_item_id)
                .expect("all item ids are in `index`");

            let crate_id = self._parse_crate(&raw, raw_item.crate_id);

            let path_id = self
                ._parse_path(&raw, parent_path_id, &raw_item_id, crate_id)
                .or(parent_path_id);

            self._parse_item(&raw, &raw_item_id, path_id, crate_id);
        }

        for (parent_path_id, name, raw_target_id) in self.deferred_imports {
            let target_path_id = self.path_ids.get(&raw_target_id).unwrap().unwrap();
            let target_path = self
                .api
                .paths
                .get(target_path_id)
                .expect("path_id to always be valid")
                .clone();

            let parent_path = self
                .api
                .paths
                .get(parent_path_id)
                .expect("all ids are valid");
            let name = format!("{}::{}", parent_path.path, name);

            let kind = crate::PathKind::Import;

            let mut path = crate::Path::new(kind, name);
            path.crate_id = parent_path.crate_id;
            path.item_id = target_path.item_id;
            path.children = target_path.children.clone();
            let path_id = self.api.paths.push(path);

            self.api
                .paths
                .get_mut(parent_path_id)
                .expect("parent_path_id to always be valid")
                .children
                .push(path_id);
        }

        Ok(self.api)
    }

    fn _parse_crate(
        &mut self,
        raw: &rustdoc_json_types_fork::Crate,
        raw_crate_id: u32,
    ) -> Option<crate::CrateId> {
        if let Some(crate_id) = self.crate_ids.get(&raw_crate_id) {
            return *crate_id;
        }

        let crate_id = (raw_crate_id != 0).then(|| {
            let raw_crate = raw
                .external_crates
                .get(&raw_crate_id)
                .expect("all crate ids are in `external_crates`");
            let crate_ = crate::Crate::new(&raw_crate.name);
            self.api.crates.push(crate_)
        });
        self.crate_ids.insert(raw_crate_id, crate_id);
        crate_id
    }

    fn _parse_path(
        &mut self,
        raw: &rustdoc_json_types_fork::Crate,
        parent_path_id: Option<crate::PathId>,
        raw_item_id: &rustdoc_json_types_fork::Id,
        crate_id: Option<crate::CrateId>,
    ) -> Option<crate::PathId> {
        if let Some(path_id) = self.path_ids.get(raw_item_id) {
            return *path_id;
        }

        let path_id = raw.paths.get(raw_item_id).map(|raw_path| {
            let raw_item = raw
                .index
                .get(raw_item_id)
                .expect("all item ids are in `index`");

            let kind = _convert_path_kind(raw_path.kind.clone());

            let mut path = crate::Path::new(kind, raw_path.path.join("::"));
            path.crate_id = crate_id;
            path.span = raw_item.span.clone().map(|raw_span| crate::Span {
                filename: raw_span.filename,
                begin: raw_span.begin,
                end: raw_span.end,
            });
            let path_id = self.api.paths.push(path);

            if let Some(parent_path_id) = parent_path_id {
                self.api
                    .paths
                    .get_mut(parent_path_id)
                    .expect("parent_path_id to always be valid")
                    .children
                    .push(path_id);
            }
            self.api.root_id.get_or_insert(path_id);
            path_id
        });
        self.path_ids.insert(raw_item_id.clone(), path_id);
        path_id
    }

    fn _parse_item(
        &mut self,
        raw: &rustdoc_json_types_fork::Crate,
        raw_item_id: &rustdoc_json_types_fork::Id,
        path_id: Option<crate::PathId>,
        crate_id: Option<crate::CrateId>,
    ) -> Option<crate::ItemId> {
        if let Some(item_id) = self.item_ids.get(raw_item_id) {
            return *item_id;
        }

        let raw_item = raw
            .index
            .get(raw_item_id)
            .expect("all item ids are in `index`");

        let item_id = match &raw_item.inner {
            rustdoc_json_types_fork::ItemEnum::Module(module) => {
                self.unprocessed
                    .extend(module.items.iter().map(move |i| (path_id, i.clone())));
                None
            }
            rustdoc_json_types_fork::ItemEnum::Import(import) => {
                let raw_target_id = import.id.as_ref().unwrap();
                self.unprocessed.push_back((path_id, raw_target_id.clone()));
                self.deferred_imports.push((
                    path_id.unwrap(),
                    import.name.clone(),
                    raw_target_id.clone(),
                ));
                None
            }
            rustdoc_json_types_fork::ItemEnum::Trait(trait_) => {
                self.unprocessed
                    .extend(trait_.items.iter().map(move |i| (path_id, i.clone())));
                None
            }
            rustdoc_json_types_fork::ItemEnum::Impl(impl_) => {
                self.unprocessed
                    .extend(impl_.items.iter().map(move |i| (path_id, i.clone())));
                None
            }
            rustdoc_json_types_fork::ItemEnum::Enum(enum_) => {
                self.unprocessed
                    .extend(enum_.variants.iter().map(move |i| (path_id, i.clone())));
                None
            }
            _ => {
                assert_ne!(self.api.root_id, None, "Module should be root");
                let mut item = crate::Item::new();
                item.crate_id = crate_id;
                item.name = raw_item.name.clone();
                item.span = raw_item.span.clone().map(|raw_span| crate::Span {
                    filename: raw_span.filename,
                    begin: raw_span.begin,
                    end: raw_span.end,
                });
                let item_id = self.api.items.push(item);

                if let Some(path_id) = path_id {
                    self.api
                        .paths
                        .get_mut(path_id)
                        .expect("path_id to always be valid")
                        .item_id = Some(item_id);
                }
                Some(item_id)
            }
        };
        self.item_ids.insert(raw_item_id.clone(), item_id);
        item_id
    }
}

fn _convert_path_kind(kind: rustdoc_json_types_fork::ItemKind) -> crate::PathKind {
    match kind {
        rustdoc_json_types_fork::ItemKind::Module => crate::PathKind::Module,
        rustdoc_json_types_fork::ItemKind::ExternCrate => crate::PathKind::ExternCrate,
        rustdoc_json_types_fork::ItemKind::Import => crate::PathKind::Import,
        rustdoc_json_types_fork::ItemKind::Struct => crate::PathKind::Struct,
        rustdoc_json_types_fork::ItemKind::Union => crate::PathKind::Union,
        rustdoc_json_types_fork::ItemKind::Enum => crate::PathKind::Enum,
        rustdoc_json_types_fork::ItemKind::Variant => crate::PathKind::Variant,
        rustdoc_json_types_fork::ItemKind::Function => crate::PathKind::Function,
        rustdoc_json_types_fork::ItemKind::Typedef => crate::PathKind::Typedef,
        rustdoc_json_types_fork::ItemKind::OpaqueTy => crate::PathKind::OpaqueTy,
        rustdoc_json_types_fork::ItemKind::Constant => crate::PathKind::Constant,
        rustdoc_json_types_fork::ItemKind::Trait => crate::PathKind::Trait,
        rustdoc_json_types_fork::ItemKind::TraitAlias => crate::PathKind::TraitAlias,
        rustdoc_json_types_fork::ItemKind::Method => crate::PathKind::Method,
        rustdoc_json_types_fork::ItemKind::Impl => crate::PathKind::Impl,
        rustdoc_json_types_fork::ItemKind::Static => crate::PathKind::Static,
        rustdoc_json_types_fork::ItemKind::ForeignType => crate::PathKind::ForeignType,
        rustdoc_json_types_fork::ItemKind::Macro => crate::PathKind::Macro,
        rustdoc_json_types_fork::ItemKind::ProcAttribute => crate::PathKind::ProcAttribute,
        rustdoc_json_types_fork::ItemKind::ProcDerive => crate::PathKind::ProcDerive,
        rustdoc_json_types_fork::ItemKind::AssocConst => crate::PathKind::AssocConst,
        rustdoc_json_types_fork::ItemKind::AssocType => crate::PathKind::AssocType,
        rustdoc_json_types_fork::ItemKind::Primitive => crate::PathKind::Primitive,
        rustdoc_json_types_fork::ItemKind::Keyword => crate::PathKind::Keyword,
        rustdoc_json_types_fork::ItemKind::StructField => {
            unreachable!("These are handled by the Item")
        }
    }
}