pub struct Version {
pub major: u64,
pub minor: u64,
pub patch: u64,
pub pre_release: PreRelease,
pub build: BuildMetadata,
}Expand description
A parsed semantic version.
Build metadata is stored and included in the version’s string form.
Direct Version comparison ignores build metadata.
Use Version::cmp_build when build metadata should be used as a
tiebreaker.
§Examples
use js_semver::Version;
let version = Version::parse("19.3.0-canary-044d56f3-20260330").unwrap();
assert_eq!(version.major, 19);
assert_eq!(version.minor, 3);
assert_eq!(version.patch, 0);
assert_eq!(version.to_string(), "19.3.0-canary-044d56f3-20260330");Fields§
§major: u64The major version number.
minor: u64The minor version number.
patch: u64The patch version number.
pre_release: PreReleaseThe pre-release identifiers, if any.
build: BuildMetadataThe build metadata identifiers, if any.
Implementations§
Source§impl Version
impl Version
Sourcepub fn new(major: u64, minor: u64, patch: u64) -> Self
pub fn new(major: u64, minor: u64, patch: u64) -> Self
Create a new Version with no pre-release or build metadata.
§Examples
use js_semver::Version;
let version = Version::new(1, 2, 3);
assert_eq!(version.to_string(), "1.2.3");
assert!(version.pre_release.is_empty());
assert!(version.build.is_empty());Sourcepub fn parse(s: &str) -> Result<Self, SemverError>
pub fn parse(s: &str) -> Result<Self, SemverError>
Parse a version string.
§Examples
use js_semver::Version;
let version = Version::parse("1.2.3-alpha.1").unwrap();
assert_eq!(version.major, 1);
assert_eq!(version.minor, 2);
assert_eq!(version.patch, 3);
assert_eq!(version.pre_release.to_string(), "alpha.1");§Errors
Returns SemverError if s is not a valid semver string.
Sourcepub fn cmp_build(&self, other: &Self) -> Ordering
pub fn cmp_build(&self, other: &Self) -> Ordering
Compare semantic version precedence with build metadata as a tiebreaker.
This is equivalent to node-semver’s compareBuild().
§Examples
use core::cmp::Ordering;
use js_semver::Version;
let left: Version = "1.2.3+build.1".parse().unwrap();
let right: Version = "1.2.3+build.2".parse().unwrap();
assert_eq!(left.cmp(&right), Ordering::Equal);
assert_eq!(left.cmp_build(&right), Ordering::Less);Trait Implementations§
impl Eq for Version
Source§impl Ord for Version
impl Ord for Version
1.21.0 (const: unstable) · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Compares and returns the maximum of two values. Read more
1.21.0 (const: unstable) · Source§fn min(self, other: Self) -> Selfwhere
Self: Sized,
fn min(self, other: Self) -> Selfwhere
Self: Sized,
Compares and returns the minimum of two values. Read more
Auto Trait Implementations§
impl Freeze for Version
impl RefUnwindSafe for Version
impl Send for Version
impl Sync for Version
impl Unpin for Version
impl UnsafeUnpin for Version
impl UnwindSafe for Version
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more