-
Notifications
You must be signed in to change notification settings - Fork 0
/
versioning.rs
41 lines (39 loc) · 1.18 KB
/
versioning.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
use semver::{Error, Version, VersionReq};
/// Find the maximum version that satisfies the given version requirement string.
///
/// # Arguments
///
/// * `version_req_str` - A string slice that holds the version requirement.
/// * `iterable` - An iterator over a collection of strings that can be parsed into versions.
///
/// # Returns
///
/// * `Ok(Some(Version))` - The maximum version that satisfies the version requirement.
/// * `Ok(None)` - If no version satisfies the version requirement.
/// * `Err(Error)` - If the version requirement string is invalid.
///
/// # Example
///
/// ```
/// use development_tools::find_max_matching_version;
/// use semver::Version;
///
/// assert_eq!(
/// find_max_matching_version("<= 1.0.0", vec!["0.9.0", "1.0.0", "1.0.1"]).unwrap(),
/// Some(Version::parse("1.0.0").unwrap())
/// );
/// ```
pub fn find_max_matching_version<'a, I>(
version_req_str: &str,
iterable: I,
) -> Result<Option<Version>, Error>
where
I: IntoIterator<Item = &'a str>,
{
let v_req = VersionReq::parse(version_req_str)?;
Ok(iterable
.into_iter()
.filter_map(|s| Version::parse(s).ok())
.filter(|s| v_req.matches(s))
.max())
}