#api-documentation #rustdoc-json #semver #diff

public-api

List and diff the public API of Rust library crates. Relies on rustdoc JSON output from the nightly toolchain.

63 releases (35 breaking)

0.44.2 Feb 26, 2025
0.43.0 Jan 25, 2025
0.42.0 Nov 19, 2024
0.37.0 Jul 26, 2024
0.12.4 Jul 24, 2022

#10 in #diff

Download history 9115/week @ 2024-11-30 9732/week @ 2024-12-07 7124/week @ 2024-12-14 1063/week @ 2024-12-21 1205/week @ 2024-12-28 4140/week @ 2025-01-04 2606/week @ 2025-01-11 2005/week @ 2025-01-18 2294/week @ 2025-01-25 2719/week @ 2025-02-01 2444/week @ 2025-02-08 1673/week @ 2025-02-15 1582/week @ 2025-02-22 2065/week @ 2025-03-01 1125/week @ 2025-03-08 1736/week @ 2025-03-15

6,812 downloads per month
Used in 18 crates (12 directly)

MIT license

100KB
2K SLoC

public-api

List and diff the public API of Rust library crates by analyzing rustdoc JSON output files from rustdoc +nightly.

Usage

… as a Rust library

See docs.rs for library documentation and example code.

… as a CLI

Use cargo public-api for CLI use cases.

… as a CI Check

With a regular cargo test that you run in CI you will be able to

  • prevent accidental changes to your public API
  • review the public API diff of deliberate changes

First add the latest versions of the recommended libraries to your [dev-dependencies]:

cargo add --dev \
    rustup-toolchain \
    rustdoc-json \
    public-api \
    insta

Then add the following test to your project. As the author of the below test code, I hereby associate it with CC0 and to the extent possible under law waive all copyright and related or neighboring rights to it:

#[test]
fn public_api() {
    // Install a compatible nightly toolchain if it is missing
    rustup_toolchain::install(public_api::MINIMUM_NIGHTLY_RUST_VERSION).unwrap();

    // Build rustdoc JSON
    let rustdoc_json = rustdoc_json::Builder::default()
        .toolchain(public_api::MINIMUM_NIGHTLY_RUST_VERSION)
        .build()
        .unwrap();

    // Derive the public API from the rustdoc JSON
    let public_api = public_api::Builder::from_rustdoc_json(rustdoc_json)
        .build()
        .unwrap();

    // Assert that the public API looks correct
    insta::assert_snapshot!(public_api);
}

Before you run the test the first time you need to bless the current public API:

INSTA_UPDATE=always cargo test

This creates a tests/snapshots/<module>_public_api.snap file in your project that you git add together with your other project files. Then a regular

cargo test

will fail if your public API is accidentally or deliberately changed. Run

INSTA_UPDATE=always cargo test

again to review and accept public API changes.

Maintainers

See here.

Dependencies

~0.8–1.7MB
~35K SLoC