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));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"| 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 |
- 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-rc→1.2.0) reports the release it became (Minorhere), matching node-semver. - The result is symmetric (
diff(a, b) == diff(b, a)) and ignores build metadata, just like SemVer precedence.
This project follows the all-contributors specification. Contributions of any kind are welcome — code, docs, bug reports, ideas, reviews! See the emoji key for how each contribution is recognized, and open a PR or issue to get involved.
Thanks goes to these wonderful people:
Tung Tran 💻 🚧 |
Licensed under either of Apache-2.0 or MIT at your option.