Skip to content

Commit

Permalink
Move RequestedVersion to resolve.rs and implement Display (#781)
Browse files Browse the repository at this point in the history
Also adds `println` to `huak_python_formatter` cli install operation
  • Loading branch information
cnpryer authored Oct 21, 2023
1 parent 7c3db2f commit 0a48de0
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 42 deletions.
3 changes: 2 additions & 1 deletion crates/huak_python_manager/src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::version::RequestedVersion;
use crate::resolve::RequestedVersion;
use anyhow::Error;
use clap::{Parser, Subcommand};

Expand Down Expand Up @@ -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()
Expand Down
1 change: 0 additions & 1 deletion crates/huak_python_manager/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ mod cli;
mod install;
mod releases;
mod resolve;
mod version;

fn main() {
setup_panic!();
Expand Down
57 changes: 53 additions & 4 deletions crates/huak_python_manager/src/resolve.rs
Original file line number Diff line number Diff line change
@@ -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<Release<'static>> {
Expand Down Expand Up @@ -157,6 +159,53 @@ impl PartialEq<ReleaseBuildConfiguration> for &str {
}
}

#[derive(Debug, Clone)]
pub(crate) struct RequestedVersion {
pub(crate) major: Option<u8>,
pub(crate) minor: Option<u8>,
pub(crate) patch: Option<u8>,
}

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<Self, Self::Err> {
let mut parts = s
.split('.')
.map(|it| it.parse::<u8>().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;
Expand Down
36 changes: 0 additions & 36 deletions crates/huak_python_manager/src/version.rs

This file was deleted.

0 comments on commit 0a48de0

Please sign in to comment.