diff --git a/crates/huak_python_manager/src/resolve.rs b/crates/huak_python_manager/src/resolve.rs index 1c5a7198..75c99d52 100644 --- a/crates/huak_python_manager/src/resolve.rs +++ b/crates/huak_python_manager/src/resolve.rs @@ -1,5 +1,5 @@ use crate::{ - releases::{self, Release, RELEASES}, + releases::{Release, RELEASES}, version::RequestedVersion, }; use std::env::consts::{ARCH, OS}; @@ -32,7 +32,7 @@ fn resolve_release_with_options(options: &Options) -> Option> { 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) @@ -40,32 +40,6 @@ fn resolve_release_with_options(options: &Options) -> Option> { } } -/// 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 { diff --git a/crates/huak_python_manager/src/version.rs b/crates/huak_python_manager/src/version.rs index 8c6ac734..35124d67 100644 --- a/crates/huak_python_manager/src/version.rs +++ b/crates/huak_python_manager/src/version.rs @@ -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, @@ -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) + } +}