#semver #diff #cargo #rustdoc-json

public-api

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

59 releases (33 breaking)

0.42.0 Nov 19, 2024
0.40.0 Oct 21, 2024
0.37.0 Jul 26, 2024
0.33.1 Nov 29, 2023
0.12.4 Jul 24, 2022

#49 in Development tools

Download history 2462/week @ 2024-08-24 1932/week @ 2024-08-31 2214/week @ 2024-09-07 2051/week @ 2024-09-14 2589/week @ 2024-09-21 4128/week @ 2024-09-28 5123/week @ 2024-10-05 6974/week @ 2024-10-12 6862/week @ 2024-10-19 7608/week @ 2024-10-26 8344/week @ 2024-11-02 7940/week @ 2024-11-09 8359/week @ 2024-11-16 8036/week @ 2024-11-23 9144/week @ 2024-11-30 8397/week @ 2024-12-07

35,297 downloads per month
Used in 15 crates (9 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 \
    expect-test

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
    expect_test::expect_file!["public-api.txt"].assert_eq(&public_api.to_string());
}

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

UPDATE_EXPECT=1 cargo test public_api

This creates a tests/public-api.txt file in your project that you git add together with your other project files. Whenever you change the public API, you need to bless it again with the above command. If you forget to bless, the test will fail, together with instructions on how to bless.

Changelog

See CHANGELOG.md.

Maintainers

See here.

Dependencies

~0.8–1.7MB
~36K SLoC