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>
impl<T> VersionMap<T>
Sourcepub fn try_insert(
&mut self,
version: Version,
value: T,
) -> Result<(), (Version, T)>
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.
Sourcepub fn insert(&mut self, version: Version, value: T) -> Option<T>
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.
Sourcepub fn get(&self, version: &Version) -> Option<&T>
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"));
Sourcepub fn get_version(&self, version: &Version) -> Option<(&Version, &T)>
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")));
Sourcepub fn get_or_latest(&self, version: Option<&Version>) -> Option<&T>
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"));
Sourcepub fn get_or_latest_version(
&self,
version: Option<&Version>,
) -> Option<(&Version, &T)>
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")));
Sourcepub fn get_latest(&self) -> Option<(&Version, &T)>
pub fn get_latest(&self) -> Option<(&Version, &T)>
Returns the latest version and its associated value.
Sourcepub fn get_exact(&self, version: &Version) -> Option<&T>
pub fn get_exact(&self, version: &Version) -> Option<&T>
Gets a value by exact version match only, without alternate lookup.
pub fn remove(&mut self, version: &Version) -> Option<T>
Trait Implementations§
Source§impl<T: Clone> Clone for VersionMap<T>
impl<T: Clone> Clone for VersionMap<T>
Source§fn clone(&self) -> VersionMap<T>
fn clone(&self) -> VersionMap<T>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more