Skip to content

Commit

Permalink
refactor: Move VersionRange parsing to CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Sep 5, 2023
1 parent 9dc84db commit 60ce1a0
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 6 deletions.
5 changes: 3 additions & 2 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use lexopt::{
ValueExt,
};

use crate::{term, Feature, Rustup};
use crate::{term, version::VersionRange, Feature, Rustup};

pub(crate) struct Args {
pub(crate) leading_args: Vec<String>,
Expand Down Expand Up @@ -52,7 +52,7 @@ pub(crate) struct Args {
/// --print-command-list
pub(crate) print_command_list: bool,
/// --version-range
pub(crate) version_range: Option<String>,
pub(crate) version_range: Option<VersionRange>,
/// --version-step
pub(crate) version_step: Option<String>,

Expand Down Expand Up @@ -520,6 +520,7 @@ impl Args {
requires("--clean-per-version", &["--version-range"])?;
}
}
let version_range = version_range.map(|v| v.parse()).transpose()?;

if no_dev_deps {
info!(
Expand Down
1 change: 0 additions & 1 deletion src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ impl Context {
this.version_range = this
.args
.version_range
.as_ref()
.map(|range| rustup::version_range(range, this.args.version_step.as_deref(), &this))
.transpose()?;

Expand Down
4 changes: 2 additions & 2 deletions src/rustup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ impl Rustup {
}

pub(crate) fn version_range(
range: &str,
range: VersionRange,
step: Option<&str>,
cx: &Context,
) -> Result<Vec<(u32, String)>> {
Expand All @@ -41,7 +41,7 @@ pub(crate) fn version_range(
Ok(())
};

let VersionRange { start_inclusive, end_inclusive } = range.parse()?;
let VersionRange { start_inclusive, end_inclusive } = range;

let start_inclusive = match start_inclusive {
Some(start) => start,
Expand Down
29 changes: 28 additions & 1 deletion src/version.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,26 @@
use std::str::FromStr;
use std::{fmt, str::FromStr};

use anyhow::{Context as _, Error, Result};

#[derive(Copy, Clone)]
pub(crate) struct Version {
pub(crate) major: u32,
pub(crate) minor: u32,
pub(crate) patch: Option<u32>,
}

impl fmt::Display for Version {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let major = self.major;
let minor = self.minor;
write!(f, "{major}.{minor}")?;
if let Some(patch) = self.patch {
write!(f, ".{patch}")?;
}
Ok(())
}
}

impl FromStr for Version {
type Err = Error;

Expand All @@ -20,11 +33,25 @@ impl FromStr for Version {
}
}

#[derive(Copy, Clone)]
pub(crate) struct VersionRange {
pub(crate) start_inclusive: Option<Version>,
pub(crate) end_inclusive: Option<Version>,
}

impl fmt::Display for VersionRange {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if let Some(start) = self.start_inclusive {
write!(f, "{start}")?;
}
write!(f, "..")?;
if let Some(end) = self.end_inclusive {
write!(f, "{end}")?;
}
Ok(())
}
}

impl FromStr for VersionRange {
type Err = Error;

Expand Down

0 comments on commit 60ce1a0

Please sign in to comment.