Skip to content

Commit

Permalink
Refactor is_version as RequestedVersion.matches_version
Browse files Browse the repository at this point in the history
  • Loading branch information
cnpryer committed Oct 20, 2023
1 parent 0214b3a commit cdc9a7f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 28 deletions.
30 changes: 2 additions & 28 deletions crates/huak_python_manager/src/resolve.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{
releases::{self, Release, RELEASES},
releases::{Release, RELEASES},
version::RequestedVersion,
};
use std::env::consts::{ARCH, OS};
Expand Down Expand Up @@ -32,40 +32,14 @@ fn resolve_release_with_options(options: &Options) -> Option<Release<'static>> {
if let Some(req) = options.version.as_ref() {
candidates
.into_iter()
.find(|it| is_requested_version(it.version, req))
.find(|it| req.matches_version(it.version))
.copied()
} else {
candidates.first().map(|it| **it)
}
}
}

/// Evaluates if some Python release's version is what was requested.
fn is_requested_version(
release_version: releases::Version,
requested_version: &RequestedVersion,
) -> bool {
if let Some(major) = requested_version.major {
if release_version.major != major {
return false;
}
}

if let Some(minor) = requested_version.minor {
if release_version.minor != minor {
return false;
}
}

if let Some(patch) = requested_version.patch {
if release_version.patch != patch {
return false;
}
}

true
}

#[derive(Default)]
/// The strategy used for resolving a Python releases.
pub(crate) enum Strategy {
Expand Down
11 changes: 11 additions & 0 deletions crates/huak_python_manager/src/version.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use anyhow::Error; // TODO(cnpryer): Library code should use thiserror
use std::str::FromStr;

use crate::releases::Version;

#[derive(Debug, Clone)]
pub(crate) struct RequestedVersion {
pub(crate) major: Option<u8>,
Expand All @@ -23,3 +25,12 @@ impl FromStr for RequestedVersion {
})
}
}

impl RequestedVersion {
/// Evaluates if some Python release's version is what was requested.
pub(crate) fn matches_version(&self, version: Version) -> bool {
self.major.map_or(true, |it| it == version.major)
&& self.minor.map_or(true, |it| it == version.minor)
&& self.patch.map_or(true, |it| it == version.patch)
}
}

0 comments on commit cdc9a7f

Please sign in to comment.