Skip to content

Commit

Permalink
fix: accept partial versions
Browse files Browse the repository at this point in the history
  • Loading branch information
fdaciuk committed Jun 23, 2022
1 parent ef306fd commit d290939
Showing 1 changed file with 16 additions and 16 deletions.
32 changes: 16 additions & 16 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ pub fn get_updated_version(
cargo_toml_content: &str,
label: &VersionLabel,
) -> Result<String, Box<dyn Error>> {
let current_version_tuple = get_version(cargo_toml_content)?;
let current_version =
parser::get_version_from_cargo_toml(cargo_toml_content)?;
let current_version_tuple = string_version_to_tuple(&current_version)?;
let (major, minor, patch) = current_version_tuple;
let new_version = match label {
VersionLabel::Major => format!("{}.0.0", major + 1),
Expand All @@ -61,22 +63,28 @@ pub fn tuple_version_to_string(tuple_version: &(u32, u32, u32)) -> String {
)
}

pub fn get_version(
cargo_toml_content: &str,
fn get_padded_version(numeric_version: &str) -> Result<String, Box<dyn Error>> {
let (major, minor, patch) = string_version_to_tuple(numeric_version)?;
let new_version = format!("{}.{}.{}", major, minor, patch);
Ok(new_version)
}

fn string_version_to_tuple(
version: &str,
) -> Result<(u32, u32, u32), Box<dyn Error>> {
let version = parser::get_version_from_cargo_toml(cargo_toml_content)?;
let version = version.replace('v', "");
let mut version_split = version.split('.');
let major = version_split.next().unwrap().parse()?;
let minor = version_split.next().unwrap().parse()?;
let patch = version_split.next().unwrap().parse()?;
let major = version_split.next().unwrap_or("0").parse::<u32>()?;
let minor = version_split.next().unwrap_or("0").parse::<u32>()?;
let patch = version_split.next().unwrap_or("0").parse::<u32>()?;
Ok((major, minor, patch))
}

fn parse_numeric_version(
current_version_tuple: &(u32, u32, u32),
numeric_version: &str,
) -> Result<String, Box<dyn Error>> {
let new_version = numeric_version.replace('v', "");
let new_version = get_padded_version(numeric_version)?;
let current_version_string = tuple_version_to_string(current_version_tuple);
let current_version_number =
string_version_to_number(&current_version_string)?;
Expand Down Expand Up @@ -157,14 +165,6 @@ other = {{ version = \"1.1.8\" }}
};
}

#[test]
fn should_get_version_from_cargo_toml() {
let cargo_toml = get_cargo_toml("2.8.1");
let actual = get_version(&cargo_toml).unwrap();
let expected = (2, 8, 1);
assert_eq!(actual, expected);
}

#[test]
fn should_update_version_by_patch_label() {
let cargo_toml = get_cargo_toml("0.0.1");
Expand Down

0 comments on commit d290939

Please sign in to comment.