Struct dia_semver::Semver[][src]

pub struct Semver {
    pub major: u128,
    pub minor: u128,
    pub patch: u128,
    pub pre_release: Option<String>,
    pub build_metadata: Option<String>,
    // some fields omitted
}

Semver.

Concepts

Strict parser

  • Does not allow leading/trailing while spaces.
  • All 3 version numbers are required: major, minor, patch.

Tolerant parser

  • 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.

Usage

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().

Full specification

https://semver.org/spec/v2.0.0.html

Fields

Pre-release.

This does not contain the leading character -. However calling Semver.to_string() will always have that character displayed.

Build metadata.

This does not contain the leading character +. However calling Semver.to_string() will always have that character displayed.

Methods

impl Semver
[src]

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, ParseSemverError>. If you need pre-release and/or build metadata, you can use ::parse().

Parses input string to make a new semver.

  • 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.

Examples

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");

Parses an &OsStr.

This function uses strict parser.

Parses an &OsStr.

This function uses tolerant parser.

Parses a &CStr.

This function uses strict parser.

Parses a &CStr.

This function uses tolerant parser.

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.

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 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"),
};

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 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()));

Makes new semver with major version incremented by 1.

Pre-release and build metadata will be dropped.

Examples

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");

Makes new semver with minor version incremented by 1.

Pre-release and build metadata will be dropped.

Examples

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");

Makes new semver with patch version incremented by 1.

Pre-release and build metadata will be dropped.

Examples

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");

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 (details).

Examples

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"
);

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 (details).

Examples

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"
);

Trait Implementations

impl Debug for Semver
[src]

Formats the value using the given formatter. Read more

impl Clone for Semver
[src]

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

impl Eq for Semver
[src]

impl PartialEq for Semver
[src]

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

impl Ord for Semver
[src]

This method returns an Ordering between self and other. Read more

Compares and returns the maximum of two values. Read more

Compares and returns the minimum of two values. Read more

impl PartialOrd for Semver
[src]

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

impl Hash for Semver
[src]

Feeds this value into the given [Hasher]. Read more

Feeds a slice of this type into the given [Hasher]. Read more

impl FromStr for Semver
[src]

The associated error which can be returned from parsing.

Parses input string via tolerant parser to make a new semver.

  • Minor and patch version numbers are optional.
  • Leading/trailing while spaces are ignored.

For strict parser, see ::parse().

impl Display for Semver
[src]

Formats the value using the given formatter. Read more

impl AsRef<Semver> for Semver
[src]

Performs the conversion.

Auto Trait Implementations

impl Send for Semver

impl Sync for Semver