Skip to content

Commit

Permalink
Merge pull request #18 from rodneylab/refactor__simplify_semver_logic
Browse files Browse the repository at this point in the history
refactor: 🏄🏽 simplify semver logic
  • Loading branch information
rodneylab authored Jan 14, 2025
2 parents 922769e + 3deca57 commit 6897c15
Show file tree
Hide file tree
Showing 5 changed files with 305 additions and 169 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ env_logger = "0.11.6"
git2 = "0.20.0"
log = "0.4.22"
nom = "7.1.3"
semver = "1.0.24"
serde = { version = "1.0.217", features = ["derive"] }
toml = "0.8.19"

Expand Down
2 changes: 1 addition & 1 deletion justfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ coverage:
LLVM_PROFILE_FILE="rust_crate_diffs-%p-%m.profraw" cargo test
grcov . -s . --binary-path ./target/debug/ -t html --branch --ignore-not-existing \
-o ./target/debug/coverage/
open ./target/debug/coverage
open --reveal ./target/debug/coverage
sed -i '' "s|href=\"https://cdn.jsdelivr.net/npm/[email protected]/css/bulma.min.css\"|href=\"file://`pwd`/.cache/bulma.min.css\"|g" ./target/debug/coverage/**/*.html
mkdir -p .cache
curl --time-cond .cache/bulma.min.css -C - -Lo .cache/bulma.min.css \
Expand Down
94 changes: 42 additions & 52 deletions src/domain/cargo_toml/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,32 @@ impl File {
})
}

fn get_version(value: &CargoDependencyValue) -> anyhow::Result<SemverVersion> {
match value {
CargoDependencyValue::Simple(version) => SemverVersion::new(version).map_err(|error| {
anyhow!(
"Unexpected semver {version} found while computing dependency changes: \
{error}",
)
}),
CargoDependencyValue::Detailed(DetailedCargoDependency { version, .. }) => {
SemverVersion::new(version).map_err(|_| {
anyhow!(
"Unexpected semver version `{version}` found while computing \
dependency changes"
)
})
}
CargoDependencyValue::Git(GitCargoDependency { git }) => {
log::warn!(
"Git dependency `{git}` found, but version change detection for git \
dependencies is not currently supported"
);
SemverVersion::new("0").map_err(|_| unreachable!("Version 0 should be valid"))
}
}
}

fn get_changes_from_current_dependencies(
current_dependencies: &BTreeMap<String, CargoDependencyValue>,
previous_dependencies: &BTreeMap<String, CargoDependencyValue>,
Expand All @@ -73,53 +99,11 @@ impl File {
result: &mut String,
) -> anyhow::Result<()> {
for (name, current_value) in current_dependencies {
let current_version = match current_value {
CargoDependencyValue::Simple(version) => {
match SemverVersion::new(version) {
Ok(value) => value,
Err(error) => return Err(anyhow!(error).context(
"Unexpected semver version found while computing dependency changes",
)),
}
}
CargoDependencyValue::Detailed(DetailedCargoDependency { version, .. }) => {
match SemverVersion::new(version) {
Ok(value)=> value,
Err(error) => return Err(anyhow!(error).context(
"Unexpected semver version `{version}` found while computing dependency changes"
)
)
}
},
CargoDependencyValue::Git(GitCargoDependency { git }) => {
log::warn!("Git dependency `{git}` found, but version change detection for git dependencies is not currently supported");
SemverVersion::new("0").expect("`0` should be a valid semver version")
}
};
let current_version = Self::get_version(current_value)?;
if let Some(previous_value) = previous_dependencies.get(name) {
// Handle dependencies in previous and current (filtering for ones with changed
// versions)
let previous_version = match previous_value {
CargoDependencyValue::Simple(version) => match SemverVersion::new(version) {
Ok(value) => value,
Err(error) => return Err(anyhow!(error).context(
"Unexpected semver version found while computing dependency changes",
)),
},
CargoDependencyValue::Detailed(DetailedCargoDependency { version, .. }) => {
match SemverVersion::new(version){
Ok(value) => value,
Err(error) => return Err(anyhow!(error).context(
"Unexpected semver version `{version}` found while computing dependency changes"
)
)
}
}
CargoDependencyValue::Git(GitCargoDependency { git }) => {
log::warn!("Git dependency `{git}` found, but version change detection for git dependencies is not currently supported");
SemverVersion::new("0").expect("`0` should be a valid semver version")
}
};
let previous_version = Self::get_version(previous_value)?;

// Housekeeping to make previous keys into a list of only crates removed in the
// current Cargo.toml
Expand All @@ -130,42 +114,48 @@ Ok(value) => value,
Some(Ordering::Greater) => {
if let Some(label_value) = label {
result.push_str(&format!(
"{change_type} bump {name} {label_value} from {previous_version} to {current_version}\n",
"{change_type} bump {name} {label_value} from {previous_version} \
to {current_version}\n",
));
} else {
result.push_str(&format!(
"{change_type} bump {name} from {previous_version} to {current_version}\n",
"{change_type} bump {name} from {previous_version} to \
{current_version}\n",
));
}
}
Some(Ordering::Equal) => {}
Some(Ordering::Less) => {
if let Some(label_value) = label {
result.push_str(&format!(
"{change_type} drop {name} {label_value} from {previous_version} to {current_version}\n"
"{change_type} drop {name} {label_value} from {previous_version} to \
{current_version}\n"
));
} else {
result.push_str(&format!(
"{change_type} drop {name} from {previous_version} to {current_version}\n"
));
"{change_type} drop {name} from {previous_version} to \
{current_version}\n"
));
}
}
None => {
if let Some(label_value) = label {
result.push_str(&format!(
"{change_type} change {name} {label_value} from {previous_version} to {current_version}\n"
"{change_type} change {name} {label_value} from {previous_version} \
to {current_version}\n"
));
} else {
result.push_str(&format!(
"{change_type} change {name} from {previous_version} to {current_version}\n"
"{change_type} change {name} from {previous_version} to \
{current_version}\n"
));
}
}
}
} else {
// Handle added dependencies
if let Some(label_value) = label {
result.push_str(&format!("✨ add {name} {label_value} {current_version}\n",));
result.push_str(&format!("✨ add {name} {label_value} {current_version}\n"));
} else {
result.push_str(&format!("✨ add {name} {current_version}\n",));
}
Expand Down
Loading

0 comments on commit 6897c15

Please sign in to comment.