Struct dia_semver::Semver
source · pub struct Semver { /* private fields */ }
Expand description
Semver.
Concepts
Strict parser
- Does not allow leading/trailing white spaces.
- All 3 version numbers are required: major, minor, patch.
Tolerant parser
- 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.
Usage
This struct does not expose internal fields, where the user can make new instance directly. Instead, it provides helper functions:
new()
makes new instance from raw input version numbers.parse()
andparse_*()
use strict parser, whilefrom_*()
are more tolerant.
For protection against flood attack, max length of the string (to be parsed) is one of:
255
bytes (on 8-bit machines)2048
bytes (on larger machines)
Since Rust encourages the use of immutable variables, a semver instance should not be changed. Indeed, there are no
mutable functions. 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()
…
Implementations§
source§impl Semver
impl Semver
sourcepub const fn new(major: u64, minor: u64, patch: u64) -> Self
pub const fn new(major: u64, minor: u64, patch: u64) -> Self
Makes new semver from raw input version numbers
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>
. If you need pre-release and/or build metadata, you can use parse()
.
If you only have a single major version number, there’s a simpler call:
use dia_semver::Semver;
assert_eq!(Semver::new(2, 0, 0), Semver::from(2_u8));
sourcepub fn parse<S>(s: S) -> Result<Self>where
S: AsRef<str>,
pub fn parse<S>(s: S) -> Result<Self>where
S: AsRef<str>,
Parses input string to make a new semver
- Minor and patch version numbers are required.
- Leading/trailing white spaces are not allowed.
For convenience, you can use implementation of FromStr
trait. It uses tolerant parser, where:
- Minor and patch version numbers are optional.
- Leading/trailing white spaces are ignored.
Examples
use core::str::FromStr;
use dia_semver::Semver;
assert_eq!(Semver::parse("1.2.3")?.to_string(), "1.2.3");
assert_eq!(
Semver::from_str("\t 1-a.b.c \r\n")?.to_string(),
"1.0.0-a.b.c",
);
sourcepub fn parse_os_str<S>(s: S) -> Result<Self>where
S: AsRef<OsStr>,
pub fn parse_os_str<S>(s: S) -> Result<Self>where
S: AsRef<OsStr>,
Parses an &OsStr
This function uses strict parser.
sourcepub fn from_os_str<S>(s: S) -> Result<Self>where
S: AsRef<OsStr>,
pub fn from_os_str<S>(s: S) -> Result<Self>where
S: AsRef<OsStr>,
Parses an &OsStr
This function uses tolerant parser.
sourcepub fn parse_c_str<S>(s: S) -> Result<Self>where
S: AsRef<CStr>,
pub fn parse_c_str<S>(s: S) -> Result<Self>where
S: AsRef<CStr>,
Parses a &CStr
This function uses strict parser.
sourcepub fn from_c_str<S>(s: S) -> Result<Self>where
S: AsRef<CStr>,
pub fn from_c_str<S>(s: S) -> Result<Self>where
S: AsRef<CStr>,
Parses a &CStr
This function uses tolerant parser.
sourcepub fn pre_release(&self) -> Option<&str>
pub fn pre_release(&self) -> Option<&str>
Pre-release
This does not contain the leading character -
. However calling to_string()
will always have that character displayed.
sourcepub fn build_metadata(&self) -> Option<&str>
pub fn build_metadata(&self) -> Option<&str>
Build metadata
This does not contain the leading character +
. However calling to_string()
will always have that character displayed.
sourcepub fn is_stable(&self) -> bool
pub fn is_stable(&self) -> bool
Checks to see if the semver is stable
A semver is stable if:
- It’s not a pre-release.
- Its major version is larger than zero.
sourcepub fn parse_pre_release(&self) -> Option<PreRelease>
pub fn parse_pre_release(&self) -> Option<PreRelease>
Parses pre-release to see if it starts with some common phrases
Notes:
- Case is ignored.
- If it returns
None
, it means the semver doesn’t have a pre-release.
Examples
use core::str::FromStr;
use dia_semver::{Semver, PreRelease};
match Semver::from_str("1.2.3-hola")?.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"),
};
sourcepub fn compatible_with(&self, other: &Self) -> bool
pub fn compatible_with(&self, other: &Self) -> bool
Checks to see if this semver is compatible with one other
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.
Examples
use core::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")?) == false);
assert!(Semver::new(0, 0, 1).compatible_with(&Semver::from_str("0.0.1+abc")?));
sourcepub fn new_major(&self) -> Option<Self>
pub fn new_major(&self) -> Option<Self>
Makes new semver with major version incremented by 1
Notes
- Pre-release and build metadata will be dropped.
checked_add()
will be used. So if overflow happens,None
is returned.
Examples
use core::str::FromStr;
use dia_semver::Semver;
assert_eq!(
Semver::from_str("1.2.3-abc+xyz")?.new_major().unwrap().to_string(),
"2.0.0",
);
sourcepub fn new_major_with_last_details(&self) -> Option<Self>
pub fn new_major_with_last_details(&self) -> Option<Self>
Makes new semver with major version incremented by 1
Notes
- Pre-release and build metadata will be kept.
checked_add()
will be used. So if overflow happens,None
is returned.
Examples
use core::str::FromStr;
use dia_semver::Semver;
assert_eq!(
Semver::from_str("1.2.3-abc+xyz")?
.new_major_with_last_details().unwrap().to_string(),
"2.0.0-abc+xyz",
);
sourcepub fn new_minor(&self) -> Option<Self>
pub fn new_minor(&self) -> Option<Self>
Makes new semver with minor version incremented by 1
Notes
- Pre-release and build metadata will be dropped.
checked_add()
will be used. So if overflow happens,None
is returned.
Examples
use core::str::FromStr;
use dia_semver::Semver;
assert_eq!(
Semver::from_str("1.2.3-abc+xyz")?.new_minor().unwrap().to_string(),
"1.3.0",
);
sourcepub fn new_minor_with_last_details(&self) -> Option<Self>
pub fn new_minor_with_last_details(&self) -> Option<Self>
Makes new semver with minor version incremented by 1
Notes
- Pre-release and build metadata will be kept.
checked_add()
will be used. So if overflow happens,None
is returned.
Examples
use core::str::FromStr;
use dia_semver::Semver;
assert_eq!(
Semver::from_str("1.2.3-abc+xyz")?
.new_minor_with_last_details().unwrap().to_string(),
"1.3.0-abc+xyz",
);
sourcepub fn new_patch(&self) -> Option<Self>
pub fn new_patch(&self) -> Option<Self>
Makes new semver with patch version incremented by 1
Notes
- Pre-release and build metadata will be dropped.
checked_add()
will be used. So if overflow happens,None
is returned.
Examples
use core::str::FromStr;
use dia_semver::Semver;
assert_eq!(
Semver::from_str("1.2.3-abc+xyz")?.new_patch().unwrap().to_string(),
"1.2.4",
);
sourcepub fn new_patch_with_last_details(&self) -> Option<Self>
pub fn new_patch_with_last_details(&self) -> Option<Self>
Makes new semver with patch version incremented by 1
Notes
- Pre-release and build metadata will be kept.
checked_add()
will be used. So if overflow happens,None
is returned.
Examples
use core::str::FromStr;
use dia_semver::Semver;
assert_eq!(
Semver::from_str("1.2.3-abc+xyz")?
.new_patch_with_last_details().unwrap().to_string(),
"1.2.4-abc+xyz",
);
sourcepub fn new_pre_release<S>(
&self,
pre_release: S,
keep_build_metadata: bool
) -> Result<Self>where
S: AsRef<str>,
pub fn new_pre_release<S>(
&self,
pre_release: S,
keep_build_metadata: bool
) -> Result<Self>where
S: AsRef<str>,
Makes new semver with same version numbers, but new pre-release and/or same build metadata
pre_release
should not include leading character -
; it will be added automatically.
Examples
use core::str::FromStr;
use dia_semver::Semver;
assert_eq!(
Semver::from_str("1.2.3-alpha.1+abc")?
.new_pre_release("alpha.2", false)?
.to_string(),
"1.2.3-alpha.2"
);
sourcepub fn drop_pre_release(&self) -> Self
pub fn drop_pre_release(&self) -> Self
Drops pre-release
Examples
use {
core::str::FromStr,
dia_semver::Semver,
};
assert_eq!(
Semver::from_str("1.2.3-alpha")?.drop_pre_release(),
Semver::new(1, 2, 3),
);
assert_eq!(
Semver::from_str("1.2.3-some+rigel")?.drop_pre_release().to_string(),
Semver::from_str("1.2.3+rigel")?.to_string(),
);
sourcepub fn new_build_metadata<S>(
&self,
build_metadata: S,
keep_pre_release: bool
) -> Result<Self>where
S: AsRef<str>,
pub fn new_build_metadata<S>(
&self,
build_metadata: S,
keep_pre_release: bool
) -> Result<Self>where
S: AsRef<str>,
Makes new semver with same version numbers, but new build metadata and/or same pre-release
build_metadata
should not include leading character +
; it will be added automatically.
Examples
use core::str::FromStr;
use dia_semver::Semver;
assert_eq!(
Semver::from_str("1.2.3-alpha.1+abc")?
.new_build_metadata("xyz", false)?
.to_string(),
"1.2.3+xyz"
);
sourcepub fn drop_build_metadata(&self) -> Self
pub fn drop_build_metadata(&self) -> Self
Drops build metadata
Examples
use {
core::str::FromStr,
dia_semver::Semver,
};
assert_eq!(
Semver::from_str("1.2.3+bsd")?.drop_build_metadata().to_string(),
Semver::new(1, 2, 3).to_string(),
);
assert_eq!(
Semver::from_str("1.2.3-beta+artix")?.drop_build_metadata().to_string(),
Semver::from_str("1.2.3-beta")?.to_string(),
);
sourcepub fn to_short_format(&self) -> String
pub fn to_short_format(&self) -> String
Makes this version in short format
Short format means the last version number(s) will be left out, if they’re zeros.
Examples
use core::str::FromStr;
use dia_semver::Semver;
for (sample, expected) in &[
("1.0.0-alpha+bsd", "1-alpha+bsd"),
("1.1.0", "1.1"),
("2.0.1-beta", "2.0.1-beta"),
] {
assert_eq!(Semver::from_str(sample)?.to_short_format(), *expected);
}
Trait Implementations§
source§impl FromStr for Semver
impl FromStr for Semver
source§impl Ord for Semver
impl Ord for Semver
source§impl PartialOrd<Semver> for Semver
impl PartialOrd<Semver> for Semver
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read more