- Does not allow leading/trailing while spaces.
- All 3 version numbers are required: major, minor, patch.
- Ignores leading/trailing while spaces.
- Minor and patch version numbers are optional.
This mode is added by the crate author, it's not described in official specification.
A semver instance should always be valid. For this reason, it does not support making new instance directly. Instead, it provides helper
functions:
::new()
makes new instance from raw input version numbers.
::parse()
and ::parse_*()
use strict parser, while ::from_*()
are more tolerant.
Since Rust encourages the use of immutable variables, a semver instance should not be changed. Once
made, there are no functions which help you change its fields. So let mut
is discouraged. However there are useful functions to help you
make new semver from an existing one: new_major()
, new_minor()
, new_patch()
...
Others might also come in handy: is_stable()
, is_early()
.
https://semver.org/spec/v2.0.0.html
This does not contain the leading character -
. However calling Semver.to_string()
will always have that
character displayed.
This does not contain the leading character +
. However calling Semver.to_string()
will always have that
character displayed.
This function does not support pre-release and build metadata -- which are required to be parsed. For convenience, it returns a direct
instance, not a Result<Semver, ParseSemverError>
. If you need pre-release and/or build metadata, you can use ::parse()
.
- Minor and patch version numbers are required.
- Leading/trailing while spaces are not allowed.
For convenience, you can use ::from_str()
implemented from FromStr
trait. It uses tolerant parser, in
which:
- Minor and patch version numbers are optional.
- Leading/trailing while spaces are ignored.
use std::str::FromStr;
use dia_semver::Semver;
assert_eq!(Semver::parse("1.2.3").unwrap().to_string(), "1.2.3");
assert_eq!(Semver::from_str("\t 1-a.b.c \r\n").unwrap().to_string(), "1.0.0-a.b.c");
This function uses strict parser.
This function uses tolerant parser.
This function uses strict parser.
This function uses tolerant parser.
A semver is stable if:
- It's not a pre-release.
- Its major version is larger than zero.
Notes:
- Case is ignored.
- If it returns
None
, it means the semver doesn't have a pre-release.
use std::str::FromStr;
use dia_semver::{Semver, PreRelease};
match Semver::from_str("1.2.3-hola").unwrap().parse_pre_release() {
Some(PreRelease::Alpha) => println!("Alpha"),
Some(PreRelease::Beta) => println!("Beta"),
Some(PreRelease::RC) => println!("RC"),
Some(PreRelease::Other) => println!("Other"),
None => println!("Not available"),
};
Two semvers are compatible with each other if one of these conditions is true:
- They have same major version, which must be larger than
0
.
- If both major versions are
0
, they must have exactly these same components: major, minor, patch, pre-release.
use std::str::FromStr;
use dia_semver::Semver;
assert!(Semver::new(1, 0, 1).compatible_with(&Semver::new(1, 2, 3)));
assert!(Semver::new(1, 0, 1).compatible_with(&Semver::new(1, 99, 9)));
assert!(Semver::new(1, 0, 1).compatible_with(&Semver::new(2, 0, 0)) == false);
assert!(Semver::new(0, 0, 1).compatible_with(Semver::new(0, 2, 0)) == false);
assert!(Semver::new(0, 0, 1).compatible_with(Semver::from_str("0.0.1-abc").unwrap()) == false);
assert!(Semver::new(0, 0, 1).compatible_with(Semver::from_str("0.0.1+abc").unwrap()));
Pre-release and build metadata will be dropped.
use std::str::FromStr;
use dia_semver::Semver;
assert_eq!(Semver::from_str("1.2.3-abc+xyz").unwrap().new_major().to_string(), "2.0.0");
Pre-release and build metadata will be dropped.
use std::str::FromStr;
use dia_semver::Semver;
assert_eq!(Semver::from_str("1.2.3-abc+xyz").unwrap().new_minor().to_string(), "1.3.0");
Pre-release and build metadata will be dropped.
use std::str::FromStr;
use dia_semver::Semver;
assert_eq!(Semver::from_str("1.2.3-abc+xyz").unwrap().new_patch().to_string(), "1.2.4");
pre_release
should not include leading character -
; it will be added automatically (details).
use std::str::FromStr;
use dia_semver::Semver;
assert_eq!(
Semver::from_str("1.2.3-alpha.1+abc").unwrap()
.new_pre_release("alpha.2", false).unwrap()
.to_string(),
"1.2.3-alpha.2"
);
build_metadata
should not include leading character +
; it will be added automatically (details).
use std::str::FromStr;
use dia_semver::Semver;
assert_eq!(
Semver::from_str("1.2.3-alpha.1+abc").unwrap()
.new_build_metadata("xyz", false).unwrap()
.to_string(),
"1.2.3+xyz"
);
Formats the value using the given formatter. Read more
Performs copy-assignment from source
. Read more
This method tests for self
and other
values to be equal, and is used by ==
. Read more
This method tests for !=
.
This method returns an Ordering
between self
and other
. Read more
fn max(self, other: Self) -> Self | 1.21.0 [src] |
Compares and returns the maximum of two values. Read more
fn min(self, other: Self) -> Self | 1.21.0 [src] |
Compares and returns the minimum of two values. Read more
This method returns an ordering between self
and other
values if one exists. Read more
This method tests less than (for self
and other
) and is used by the <
operator. Read more
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
This method tests greater than (for self
and other
) and is used by the >
operator. Read more
This method tests greater than or equal to (for self
and other
) and is used by the >=
operator. Read more
Feeds this value into the given [Hasher
]. Read more
Feeds a slice of this type into the given [Hasher
]. Read more
The associated error which can be returned from parsing.
- Minor and patch version numbers are optional.
- Leading/trailing while spaces are ignored.
For strict parser, see ::parse()
.
Formats the value using the given formatter. Read more