[][src]Struct dia_semver::Semver

pub struct Semver { /* fields omitted */ }

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

A semver instance should always be valid. For this reason, it 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() 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().

Methods

impl Semver[src]

pub const MAX_INPUT_STR_LEN: u16[src]

pub const fn new(major: u128, minor: u128, patch: u128) -> 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().

pub fn parse<S>(s: S) -> Result<Semver, ParseSemverError> where
    S: AsRef<str>, 
[src]

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

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

pub fn parse_os_str<S>(s: S) -> Result<Semver, ParseSemverError> where
    S: AsRef<OsStr>, 
[src]

Parses an &OsStr

This function uses strict parser.

pub fn from_os_str<S>(s: S) -> Result<Semver, ParseSemverError> where
    S: AsRef<OsStr>, 
[src]

Parses an &OsStr

This function uses tolerant parser.

pub fn parse_c_str<S>(s: S) -> Result<Semver, ParseSemverError> where
    S: AsRef<CStr>, 
[src]

Parses a &CStr

This function uses strict parser.

pub fn from_c_str<S>(s: S) -> Result<Semver, ParseSemverError> where
    S: AsRef<CStr>, 
[src]

Parses a &CStr

This function uses tolerant parser.

pub fn major(&self) -> u128[src]

pub fn minor(&self) -> u128[src]

pub fn patch(&self) -> u128[src]

pub fn build_metadata(&self) -> Option<&str>[src]

pub fn pre_release(&self) -> Option<&str>[src]

pub fn is_stable(&self) -> bool[src]

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.

pub fn is_early(&self) -> bool[src]

pub fn parse_pre_release(&self) -> Option<PreRelease>[src]

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

pub fn compatible_with<T>(&self, other: T) -> bool where
    T: AsRef<Semver>, 
[src]

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

pub fn new_major(&self) -> Option<Semver>[src]

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

pub fn new_minor(&self) -> Option<Semver>[src]

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

pub fn new_patch(&self) -> Option<Semver>[src]

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

pub fn new_pre_release<S>(
    &self,
    pre_release: S,
    keep_build_metadata: bool
) -> Result<Semver, ParseSemverError> where
    S: AsRef<str>, 
[src]

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

pub fn new_build_metadata<S>(
    &self,
    build_metadata: S,
    keep_pre_release: bool
) -> Result<Semver, ParseSemverError> where
    S: AsRef<str>, 
[src]

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 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 Eq for Semver[src]

impl Ord for Semver[src]

fn max(self, other: Self) -> Self1.21.0[src]

Compares and returns the maximum of two values. Read more

fn min(self, other: Self) -> Self1.21.0[src]

Compares and returns the minimum of two values. Read more

fn clamp(self, min: Self, max: Self) -> Self[src]

🔬 This is a nightly-only experimental API. (clamp)

Restrict a value to a certain interval. Read more

impl PartialEq<Semver> for Semver[src]

#[must_use]
fn ne(&self, other: &Rhs) -> bool
1.0.0[src]

This method tests for !=.

impl Clone for Semver[src]

fn clone_from(&mut self, source: &Self)1.0.0[src]

Performs copy-assignment from source. Read more

impl PartialOrd<Semver> for Semver[src]

#[must_use]
fn lt(&self, other: &Rhs) -> bool
1.0.0[src]

This method tests less than (for self and other) and is used by the < operator. Read more

#[must_use]
fn le(&self, other: &Rhs) -> bool
1.0.0[src]

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more

#[must_use]
fn gt(&self, other: &Rhs) -> bool
1.0.0[src]

This method tests greater than (for self and other) and is used by the > operator. Read more

#[must_use]
fn ge(&self, other: &Rhs) -> bool
1.0.0[src]

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

impl AsRef<Semver> for Semver[src]

impl Default for Semver[src]

impl Debug for Semver[src]

impl Display for Semver[src]

impl Hash for Semver[src]

fn hash_slice<H>(data: &[Self], state: &mut H) where
    H: Hasher
1.3.0[src]

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

impl FromStr for Semver[src]

type Err = ParseSemverError

The associated error which can be returned from parsing.

fn from_str(s: &str) -> Result<Self, Self::Err>[src]

Parses input string via tolerant parser to make a new semver

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

For strict parser, see parse().

Auto Trait Implementations

impl Send for Semver

impl Sync for Semver

Blanket Implementations

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToString for T where
    T: Display + ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> Any for T where
    T: 'static + ?Sized
[src]