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
//! This library gives you a the public API of a library crate, in the form of a
//! list of public items in the crate. Public items are items that other crates
//! can use.
//!
//! As input to the library, a special output format from `cargo doc` is used,
//! which goes by the name **rustdoc JSON**. Currently, only `cargo doc` from
//! the Nightly toolchain can produce **rustdoc JSON** for a library. You build
//! **rustdoc JSON** like this:
//!
//! ```bash
//! RUSTDOCFLAGS='-Z unstable-options --output-format json' cargo +nightly doc --lib --no-deps
//! ```
//!
//! The main entry point to the library is [`public_api_from_rustdoc_json_str`],
//! so please read its documentation.
//!
//! # Examples
//!
//! The two main use cases are listing the public API and diffing different
//! versions of the same public APIs.
//!
//! ## List all public items of a crate (the public API)
//! ```
#![doc = include_str!("../examples/list_public_api.rs")]
//! ```
//!
//! ## Diff two versions of a public API
//! ```
#![doc = include_str!("../examples/diff_public_api.rs")]
//! ```
//!
//! The most comprehensive example code on how to use the library can be found
//! in the thin binary wrapper around the library, see
//! <https://github.com/Enselic/public-api/blob/main/src/main.rs>.
#![deny(missing_docs, dead_code)]
#![deny(clippy::all, clippy::pedantic)]
mod error;
mod intermediate_public_item;
mod item_iterator;
mod render;
pub mod tokens;
pub mod diff;
// Documented at the definition site so cargo doc picks it up
pub use error::{Error, Result};
// Documented at the definition site so cargo doc picks it up
pub use item_iterator::PublicItem;
/// This constant defines the minimum version of nightly that is required in
/// order for the rustdoc JSON output to be parsable by this library. Note that
/// this library is implemented with stable Rust. But the rustdoc JSON that this
/// library parses can currently only be produced by nightly.
///
/// The rustdoc JSON format is still changing, so every now and then we update
/// this library to support the latest format. If you use this version of
/// nightly or later, you should be fine.
pub const MINIMUM_RUSTDOC_JSON_VERSION: &str = "nightly-2022-05-19";
/// Contains various options that you can pass to [`public_api_from_rustdoc_json_str`].
#[derive(Copy, Clone, Debug)]
#[non_exhaustive] // More options are likely to be added in the future
pub struct Options {
/// If `true`, items part of blanket implementations such as `impl<T> Any
/// for T`, `impl<T> Borrow<T> for T`, and `impl<T, U> Into<U> for T where
/// U: From<T>` are included in the list of public items of a crate.
///
/// The default value is `false` since the the vast majority of users will
/// find the presence of these items to just constitute noise, even if they
/// formally are part of the public API of a crate.
pub with_blanket_implementations: bool,
/// If `true`, items will be sorted before being returned. If you will pass
/// on the return value to [`diff::PublicItemsDiff::between`], it is
/// currently unnecessary to sort first, because the sorting will be
/// performed/ensured inside of that function.
///
/// The default value is `true`, because usually the performance impact is
/// negligible, and is is generally more practical to work with sorted data.
pub sorted: bool,
}
/// Enables options to be set up like this (note that `Options` is marked
/// `#[non_exhaustive]`):
///
/// ```
/// # use public_api::Options;
/// let mut options = Options::default();
/// options.sorted = true;
/// // ...
/// ```
impl Default for Options {
fn default() -> Self {
Self {
with_blanket_implementations: false,
sorted: true,
}
}
}
/// Takes rustdoc JSON and returns a [`Vec`] of [`PublicItem`]s where each
/// [`PublicItem`] is one public item of the crate, i.e. part of the crate's
/// public API.
///
/// There exists a convenient `cargo public-api` subcommand wrapper for this
/// function found at <https://github.com/Enselic/cargo-public-api> that
/// builds the rustdoc JSON for you and then invokes this function. If you don't
/// want to use that wrapper, use
/// ```bash
/// RUSTDOCFLAGS='-Z unstable-options --output-format json' cargo +nightly doc --lib --no-deps
/// ```
/// to generate the rustdoc JSON that this function takes as input. The output
/// is put in `./target/doc/your_library.json`.
///
/// For reference, the rustdoc JSON format is documented at
/// <https://rust-lang.github.io/rfcs/2963-rustdoc-json.html>. But the format is
/// still a moving target. Open PRs and issues for rustdoc JSON itself can be
/// found at <https://github.com/rust-lang/rust/labels/A-rustdoc-json>.
///
/// # Errors
///
/// E.g. if the JSON is invalid.
pub fn public_api_from_rustdoc_json_str(
rustdoc_json_str: &str,
options: Options,
) -> Result<Vec<PublicItem>> {
let crate_ = deserialize_without_recursion_limit(rustdoc_json_str)?;
let mut public_api: Vec<_> = item_iterator::public_api_in_crate(&crate_, options).collect();
if options.sorted {
public_api.sort();
}
Ok(public_api)
}
/// Helper to deserialize the JSON with `serde_json`, but with the recursion
/// limit disabled. Otherwise we hit the recursion limit on crates such as
/// `diesel`.
fn deserialize_without_recursion_limit(rustdoc_json_str: &str) -> Result<rustdoc_types::Crate> {
let mut deserializer = serde_json::Deserializer::from_str(rustdoc_json_str);
deserializer.disable_recursion_limit();
return Ok(serde::de::Deserialize::deserialize(&mut deserializer)?);
}