From 0a48de0fde64026b5e6c2fdfc370d6ba2abb699f Mon Sep 17 00:00:00 2001 From: Chris Pryer <14341145+cnpryer@users.noreply.github.com> Date: Fri, 20 Oct 2023 20:42:55 -0400 Subject: [PATCH] Move `RequestedVersion` to resolve.rs and implement `Display` (#781) Also adds `println` to `huak_python_formatter` cli install operation --- crates/huak_python_manager/src/cli.rs | 3 +- crates/huak_python_manager/src/main.rs | 1 - crates/huak_python_manager/src/resolve.rs | 57 +++++++++++++++++++++-- crates/huak_python_manager/src/version.rs | 36 -------------- 4 files changed, 55 insertions(+), 42 deletions(-) delete mode 100644 crates/huak_python_manager/src/version.rs diff --git a/crates/huak_python_manager/src/cli.rs b/crates/huak_python_manager/src/cli.rs index 9669e810..b1341c8a 100644 --- a/crates/huak_python_manager/src/cli.rs +++ b/crates/huak_python_manager/src/cli.rs @@ -1,4 +1,4 @@ -use crate::version::RequestedVersion; +use crate::resolve::RequestedVersion; use anyhow::Error; use clap::{Parser, Subcommand}; @@ -39,6 +39,7 @@ mod cmd { use crate::resolve::{Options, Strategy}; pub(crate) fn install(version: RequestedVersion) -> Result<(), Error> { + println!("installing Python {version}"); install_to_home(&Strategy::Selection(Options { version: Some(version), ..Default::default() diff --git a/crates/huak_python_manager/src/main.rs b/crates/huak_python_manager/src/main.rs index 19251f24..de7929d3 100644 --- a/crates/huak_python_manager/src/main.rs +++ b/crates/huak_python_manager/src/main.rs @@ -7,7 +7,6 @@ mod cli; mod install; mod releases; mod resolve; -mod version; fn main() { setup_panic!(); diff --git a/crates/huak_python_manager/src/resolve.rs b/crates/huak_python_manager/src/resolve.rs index 75c99d52..9a3b57b2 100644 --- a/crates/huak_python_manager/src/resolve.rs +++ b/crates/huak_python_manager/src/resolve.rs @@ -1,8 +1,10 @@ -use crate::{ - releases::{Release, RELEASES}, - version::RequestedVersion, +use crate::releases::{Release, Version, RELEASES}; +use anyhow::Error; // TODO(cnpryer): Library code should use thiserror +use std::{ + env::consts::{ARCH, OS}, + fmt::Display, + str::FromStr, }; -use std::env::consts::{ARCH, OS}; /// Resolve a Python Release based on a resolution `Strategy`. pub(crate) fn resolve_release(strategy: &Strategy) -> Option> { @@ -157,6 +159,53 @@ impl PartialEq for &str { } } +#[derive(Debug, Clone)] +pub(crate) struct RequestedVersion { + pub(crate) major: Option, + pub(crate) minor: Option, + pub(crate) patch: Option, +} + +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) + } +} + +impl FromStr for RequestedVersion { + type Err = Error; + + fn from_str(s: &str) -> Result { + let mut parts = s + .split('.') + .map(|it| it.parse::().expect("parsed requested version part")); + + Ok(RequestedVersion { + major: parts.next(), + minor: parts.next(), + patch: parts.next(), + }) + } +} + +impl Display for RequestedVersion { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + if let Some(major) = self.major { + write!(f, "{major}")?; + } + if let Some(minor) = self.minor { + write!(f, ".{minor}")?; + } + if let Some(patch) = self.patch { + write!(f, ".{patch}")?; + } + Ok(()) + } +} + #[cfg(test)] mod tests { use std::str::FromStr; diff --git a/crates/huak_python_manager/src/version.rs b/crates/huak_python_manager/src/version.rs deleted file mode 100644 index 35124d67..00000000 --- a/crates/huak_python_manager/src/version.rs +++ /dev/null @@ -1,36 +0,0 @@ -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, - pub(crate) minor: Option, - pub(crate) patch: Option, -} - -impl FromStr for RequestedVersion { - type Err = Error; - - fn from_str(s: &str) -> Result { - let mut parts = s - .split('.') - .map(|it| it.parse::().expect("parsed requested version part")); - - Ok(RequestedVersion { - major: parts.next(), - minor: parts.next(), - patch: parts.next(), - }) - } -} - -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) - } -}