Skip to content

Commit

Permalink
add bump {version} {which}
Browse files Browse the repository at this point in the history
  • Loading branch information
jhheider committed Mar 24, 2023
1 parent 618d6c9 commit ff00829
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 2 deletions.
15 changes: 14 additions & 1 deletion src/args/mod.rs
@@ -1,4 +1,7 @@
use crate::{range::Range, semver::Semver};
use crate::{
range::Range,
semver::{bump::SemverComponent, Semver},
};
use anyhow::{Context, Result};
use clap::{arg, command, ArgAction, ArgMatches, Command};

Expand Down Expand Up @@ -40,6 +43,16 @@ pub fn setup() -> Command {
.arg(arg!([left] "the first version to compare").value_parser(Semver::parse))
.arg(arg!([right] "the second version to compare").value_parser(Semver::parse)),
)
// Semver::bump
.subcommand(
Command::new("bump")
.about("bumps a version")
.arg(arg!([semver] "the version to bump").value_parser(Semver::parse))
.arg(
arg!([bump] "the bump to apply (major|minor|patch)")
.value_parser(SemverComponent::parse),
),
)
// Range::validate-range
.subcommand(
Command::new("validate-range")
Expand Down
13 changes: 13 additions & 0 deletions src/main.rs
Expand Up @@ -23,6 +23,8 @@ fn main() -> Result<()> {
// TODO: factor out as much as possible for testing
#[cfg(not(tarpaulin_include))]
fn handle_command(matches: Option<(&str, &ArgMatches)>) -> Result<()> {
use crate::semver::bump::SemverComponent;

match matches {
// Semver::validate
Some(("validate", args)) => {
Expand Down Expand Up @@ -83,6 +85,17 @@ fn handle_command(matches: Option<(&str, &ArgMatches)>) -> Result<()> {
}
}

// Semver::bump
Some(("bump", args)) => {
let v_in = get_arg::<Semver>(args, "semver")?;
let bump = get_arg::<SemverComponent>(args, "bump")?;

let v_out = v_in.bump(&bump)?;

println!("{}", v_out.raw);
Ok(())
}

// Range::validate
Some(("validate-range", args)) => {
let range = get_arg::<Range>(args, "range")?;
Expand Down
39 changes: 39 additions & 0 deletions src/semver/bump.rs
@@ -0,0 +1,39 @@
use anyhow::{bail, Result};

use super::Semver;

impl<'a> Semver<'a> {
pub fn bump(&self, which: &SemverComponent) -> Result<Self> {
match which {
SemverComponent::Major => Self::from((self.major + 1, 0, 0)),
SemverComponent::Minor => Self::from((self.major, self.minor + 1, 0)),
SemverComponent::Patch => Self::from((self.major, self.minor, self.patch + 1)),
SemverComponent::None => Ok(self.clone()),
}
}
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum SemverComponent {
Major,
Minor,
Patch,
None,
}

impl SemverComponent {
pub fn parse(s: &str) -> Result<Self> {
match s {
"major" => Ok(Self::Major),
"minor" => Ok(Self::Minor),
"patch" => Ok(Self::Patch),
_ => bail!("invalid bump component '{}'", s),
}
}
}

impl From<&str> for SemverComponent {
fn from(s: &str) -> Self {
Self::parse(s).unwrap_or(Self::None)
}
}
1 change: 1 addition & 0 deletions src/semver/mod.rs
@@ -1,3 +1,4 @@
pub mod bump;
mod compare;
mod parse;

Expand Down
4 changes: 4 additions & 0 deletions src/semver/parse.rs
Expand Up @@ -42,4 +42,8 @@ impl<'a> Semver<'a> {
..Default::default()
})
}

pub fn from(input: (usize, usize, usize)) -> Result<Self> {
Self::parse(&format!("{}.{}.{}", input.0, input.1, input.2))
}
}
23 changes: 22 additions & 1 deletion src/tests/semver.rs
@@ -1,4 +1,4 @@
use crate::semver::Semver;
use crate::semver::{bump::SemverComponent, Semver};
use anyhow::Result;

#[test]
Expand Down Expand Up @@ -57,6 +57,27 @@ fn test_compare() -> Result<()> {

Ok(())
}

#[test]
fn test_bump() -> Result<()> {
let a = Semver::parse("1.2.3")?;
let b = Semver::parse("1.2.4")?;
let c = Semver::parse("1.3.0")?;
let d = Semver::parse("2.0.0")?;

assert_eq!(a.bump(&SemverComponent::Major)?, d);
assert_eq!(a.bump(&SemverComponent::Minor)?, c);
assert_eq!(a.bump(&SemverComponent::Patch)?, b);
assert_eq!(a.bump(&SemverComponent::None)?, a);

assert_eq!(SemverComponent::from("major"), SemverComponent::Major);
assert_eq!(SemverComponent::from("minor"), SemverComponent::Minor);
assert_eq!(SemverComponent::from("patch"), SemverComponent::Patch);
assert_eq!(SemverComponent::from("gibberish"), SemverComponent::None);

Ok(())
}

#[test]
fn test_infinty() {
let inf = Semver::infinty();
Expand Down

0 comments on commit ff00829

Please sign in to comment.