iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🚧

[Rust] cargo-semver-checks is handy for compatibility checking

に公開
2024/01/07

Cargo uses SemVer for version management. Furthermore, the rules for maintaining library compatibility are documented in SemVer Compatibility.

SemVer Compatibility categorizes changes as follows:

  • Major change
    Large-scale, breaking changes that increment the MAJOR version in SemVer. Such as deleting functions or structs.
  • Minor change
    Compatibility-maintaining changes that increment MINOR or PATCH versions. Such as adding functions or structs.
  • Possibly-breaking change
    Changes where the classification as a Major or Minor change varies by project. Such as changing the Rust version.

For example, making a breaking change like deleting an existing function and then bumping the version from 1.0.1 to 1.0.2 is not allowed.

While these serve as a reference when updating libraries, checking each one manually is tedious. This is where cargo-semver-checks, which automatically checks compatibility rules, comes in handy.

Let's consider the following case.

In version 1.0.0 of a certain crate, there was a struct named Error.

[1.0.0] lib.rs
pub struct Error {}

Suppose you want to rename Error to ConfigError in version 1.1.0. To maintain compatibility, you attempted to add a type alias, but accidentally named it Err instead of Error. This breaks compatibility.

[1.1.0 mistaken] lib.rs
pub struct ConfigError {}

#[deprecated(note = "Use `ConfigError` instead.")]
pub type Err = ConfigError;

This is something a person might overlook. However, cargo-semver-checks will catch it perfectly.

error
--- failure enum_missing: pub enum removed or renamed ---

Description:
A publicly-visible enum cannot be imported by its prior path. A `pub use` may have been removed, or the enum itself may have been renamed or removed entirely.
        ref: https://doc.rust-lang.org/cargo/reference/semver.html#item-remove
       impl: https://github.com/obi1kenobi/cargo-semver-checks/tree/v0.23.0/src/lints/enum_missing.ron

Here is the corrected version:

[1.1.0 correct] lib.rs
pub struct ConfigError {}

#[deprecated(note = "Use `ConfigError` instead.")]
pub type Error = ConfigError;

There is also a GitHub Action, so it's a good idea to introduce it for now.

GitHubで編集を提案

Discussion