pub struct Semver { /* fields omitted */ }
- Does not allow leading/trailing white spaces.
- All 3 version numbers are required: major, minor, patch.
- Ignores leading/trailing white 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()
.
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 white 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 white 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.
::checked_add()
will be used. So if overflow happens, None
is returned.
use std::str::FromStr;
use dia_semver::Semver;
assert_eq!(Semver::from_str("1.2.3-abc+xyz").unwrap().new_major().unwrap().to_string(), "2.0.0");
- Pre-release and build metadata will be dropped.
::checked_add()
will be used. So if overflow happens, None
is returned.
use std::str::FromStr;
use dia_semver::Semver;
assert_eq!(Semver::from_str("1.2.3-abc+xyz").unwrap().new_minor().unwrap().to_string(), "1.3.0");
- Pre-release and build metadata will be dropped.
::checked_add()
will be used. So if overflow happens, None
is returned.
use std::str::FromStr;
use dia_semver::Semver;
assert_eq!(Semver::from_str("1.2.3-abc+xyz").unwrap().new_patch().unwrap().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"
);
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
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
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
Performs copy-assignment from source
. Read more
Returns the "default value" for a type. Read more
Formats the value using the given formatter. Read more
Formats the value using the given formatter. 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 white spaces are ignored.
For strict parser, see ::parse()
.
Creates owned data from borrowed data, usually by cloning. Read more
🔬 This is a nightly-only experimental API. (toowned_clone_into
)
recently added
Uses borrowed data to replace owned data, usually by cloning. Read more
Converts the given value to a String
. Read more
🔬 This is a nightly-only experimental API. (try_from
)
The type returned in the event of a conversion error.
🔬 This is a nightly-only experimental API. (try_from
)
Immutably borrows from an owned value. Read more
🔬 This is a nightly-only experimental API. (get_type_id
)
this method will likely be replaced by an associated static
🔬 This is a nightly-only experimental API. (try_from
)
The type returned in the event of a conversion error.
🔬 This is a nightly-only experimental API. (try_from
)
Mutably borrows from an owned value. Read more