Struct VersionMap

Source
pub struct VersionMap<T> { /* private fields */ }
Expand description

A map that stores values indexed by semantic versions with support for alternate lookups.

The VersionMap maintains a primary mapping from versions to values, and a secondary mapping that groups versions by their “alternate” keys for fallback lookups.

§Alternate Lookup Logic

  • For major versions > 0: alternate is major.*.*
  • For minor versions > 0 (when major is 0): alternate is 0.minor.*
  • Otherwise: alternate is 0.0.patch
  • Pre-release versions have no alternates

§Example

use semver::Version;

let mut map = VersionMap::new();
map.insert(Version::new(1, 0, 1), "v1.0.1");
map.insert(Version::new(1, 2, 0), "v1.2.0");

// Exact lookups
assert_eq!(map.get_exact(&Version::new(1, 0, 1)), Some(&"v1.0.1"));

// Alternate lookups (finds latest patch for major version 1)
assert_eq!(map.get(&Version::new(1, 0, 0)), Some(&"v1.2.0"));

Implementations§

Source§

impl<T> VersionMap<T>

Source

pub fn new() -> Self

Creates a new empty VersionMap.

Source

pub fn try_insert( &mut self, version: Version, value: T, ) -> Result<(), (Version, T)>

Attempts to insert a version-value pair, returning an error if the version already exists.

Source

pub fn insert(&mut self, version: Version, value: T) -> Option<T>

Inserts a version-value pair, returning the previous value if the version existed.

Updates the alternates mapping appropriately.

Source

pub fn get(&self, version: &Version) -> Option<&T>

Gets a value by version, using alternate lookup if exact match is not found.

§Examples
use semver::Version;

let mut map = VersionMap::new();
map.insert(Version::new(0, 0, 9), "v0.0.9");
map.insert(Version::new(0, 1, 1), "v0.1.1");
map.insert(Version::new(1, 2, 1), "v1.2.1");

// Get latest patch
assert_eq!(map.get(&Version::new(0, 0, 9)), Some(&"v0.0.9"));

// Get latest minor
assert_eq!(map.get(&Version::new(0, 1, 0)), Some(&"v0.1.1"));

// Get latest major
assert_eq!(map.get(&Version::new(1, 0, 0)), Some(&"v1.2.1"));
Source

pub fn get_version(&self, version: &Version) -> Option<(&Version, &T)>

Like get, but returns the resolved version and value as a tuple.

§Examples
use semver::Version;

let mut map = VersionMap::new();
map.insert(Version::new(0, 0, 9), "v0.0.9");
map.insert(Version::new(0, 1, 1), "v0.1.1");
map.insert(Version::new(1, 2, 1), "v1.2.1");

// Get latest patch
assert_eq!(map.get_version(&Version::new(0, 0, 9)), Some((&Version::new(0, 0, 9), &"v0.0.9")));

// Get latest minor
assert_eq!(map.get_version(&Version::new(0, 1, 0)), Some((&Version::new(0, 1, 1), &"v0.1.1")));

// Get latest major
assert_eq!(map.get_version(&Version::new(1, 0, 0)), Some((&Version::new(1, 2, 1), &"v1.2.1")));
Source

pub fn get_or_latest(&self, version: Option<&Version>) -> Option<&T>

Gets a value by version or returns the latest version if no specific version is provided.

§Examples
use semver::Version;

let mut map = VersionMap::new();
map.insert(Version::new(0, 0, 9), "v0.0.9");
map.insert(Version::new(0, 1, 0), "v0.1.0");
map.insert(Version::new(0, 1, 1), "v0.1.1");
map.insert(Version::new(0, 5, 1), "v0.5.1");
map.insert(Version::new(1, 0, 0), "v1.0.0");
map.insert(Version::new(1, 2, 0), "v1.2.0");

// Get latest patch
assert_eq!(map.get_or_latest(Some(&Version::new(0, 0, 9))), Some(&"v0.0.9"));

// Get latest minor
assert_eq!(map.get_or_latest(Some(&Version::new(0, 1, 0))), Some(&"v0.1.1"));

// Get latest major
assert_eq!(map.get_or_latest(Some(&Version::new(1, 0, 0))), Some(&"v1.2.0"));

// Get the latest version
assert_eq!(map.get_or_latest(None), Some(&"v1.2.0"));
Source

pub fn get_or_latest_version( &self, version: Option<&Version>, ) -> Option<(&Version, &T)>

Gets a value by version or returns the latest version and its associated value if no specific version is provided.

§Examples
use semver::Version;

let mut map = VersionMap::new();
map.insert(Version::new(0, 0, 9), "v0.0.9");
map.insert(Version::new(0, 1, 0), "v0.1.0");
map.insert(Version::new(0, 1, 1), "v0.1.1");
map.insert(Version::new(0, 5, 1), "v0.5.1");
map.insert(Version::new(1, 0, 0), "v1.0.0");
map.insert(Version::new(1, 2, 0), "v1.2.0");

// Get latest patch
assert_eq!(map.get_or_latest_version(Some(&Version::new(0, 0, 9))), Some((&Version::new(0, 0, 9), &"v0.0.9")));

// Get latest minor
assert_eq!(map.get_or_latest_version(Some(&Version::new(0, 1, 0))), Some((&Version::new(0, 1, 1), &"v0.1.1")));

// Get latest major
assert_eq!(map.get_or_latest_version(Some(&Version::new(1, 0, 0))), Some((&Version::new(1, 2, 0), &"v1.2.0")));

// Get the latest version
assert_eq!(map.get_or_latest_version(None), Some((&Version::new(1, 2, 0), &"v1.2.0")));
Source

pub fn get_latest(&self) -> Option<(&Version, &T)>

Returns the latest version and its associated value.

Source

pub fn get_exact(&self, version: &Version) -> Option<&T>

Gets a value by exact version match only, without alternate lookup.

Source

pub fn remove(&mut self, version: &Version) -> Option<T>

Trait Implementations§

Source§

impl<T: Clone> Clone for VersionMap<T>

Source§

fn clone(&self) -> VersionMap<T>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Debug> Debug for VersionMap<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T> Default for VersionMap<T>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<T> Freeze for VersionMap<T>

§

impl<T> RefUnwindSafe for VersionMap<T>
where T: RefUnwindSafe,

§

impl<T> Send for VersionMap<T>
where T: Send,

§

impl<T> Sync for VersionMap<T>
where T: Sync,

§

impl<T> Unpin for VersionMap<T>

§

impl<T> UnwindSafe for VersionMap<T>
where T: RefUnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

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

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.