semver-diff 0.1.0

Find the release-type difference between two semantic versions (major/minor/patch and their prerelease variants). A faithful Rust port of node-semver's diff.
Documentation
  • Coverage
  • 100%
    13 out of 13 items documented2 out of 6 items with examples
  • Size
  • Source code size: 30.13 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 362.15 kB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 2s Average build duration of successful builds.
  • all releases: 2s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • Homepage
  • trananhtung/semver-diff
    0 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • trananhtung

semver-diff

Crates.io Documentation CI License

Find the release-type difference between two semantic versions — was it a major, minor, or patch change (or one of their prerelease variants)? A faithful Rust port of node-semver's diff and the semver-diff npm package, built on the semver crate.

use semver::Version;
use semver_diff::{diff, Difference};

assert_eq!(diff(&Version::new(1, 0, 0), &Version::new(1, 0, 1)), Some(Difference::Patch));
assert_eq!(diff(&Version::new(1, 0, 0), &Version::new(2, 0, 0)), Some(Difference::Major));
assert_eq!(diff(&Version::new(1, 2, 3), &Version::new(1, 2, 3)), None);

// Or straight from strings:
assert_eq!(semver_diff::diff_str("1.0.0", "1.1.0-rc.1").unwrap(), Some(Difference::PreMinor));

Why semver-diff?

The semver crate parses and compares versions, but doesn't tell you what kind of bump separates two of them — the thing update notifiers, changelog generators, and dependency dashboards actually want to show ("this is a minor update"). The prerelease logic is subtle, so this is a small, well-tested port that matches node-semver exactly.

[dependencies]
semver-diff = "0.1"

API

Item Purpose
diff(a, b) Option<Difference> between two Versions (None if equal)
diff_str(a, b) Parse two &str versions and diff them
Difference Major / PreMajor / Minor / PreMinor / Patch / PrePatch / PreRelease
Difference::as_str() The node-semver name ("major", "premajor", …)
Difference::is_prerelease() Whether it's one of the Pre* variants

Behavior

  • The most significant changed component decides the result; a Pre* variant is returned when the higher version is itself a prerelease.
  • Stabilizing a prerelease (1.2.0-rc1.2.0) reports the release it became (Minor here), matching node-semver.
  • The result is symmetric (diff(a, b) == diff(b, a)) and ignores build metadata, just like SemVer precedence.

License

Licensed under either of Apache-2.0 or MIT at your option.