Skip to content

Commit

Permalink
feat: converting error handing to anyhow
Browse files Browse the repository at this point in the history
  • Loading branch information
DeveloperC286 committed Oct 16, 2024
1 parent d51ea10 commit 1020045
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 9 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.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ lazy_static = "1.4.0"
strum = "0.26.0"
strum_macros = "0.26.0"

# For error handling.
anyhow = "1.0.89"


[dev-dependencies]
# For parameterized testing.
Expand Down
16 changes: 7 additions & 9 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@ extern crate pretty_env_logger;

use std::io::{stdin, Read};

use anyhow::{bail, Result};
use clap::Parser;
use git2::Repository;

pub use crate::calculation_mode::CalculationMode;
use crate::cli::Arguments;
pub use crate::commits::Commits;
pub use crate::git_history_mode::GitHistoryMode;
use crate::commits::Commits;

mod calculation_mode;
mod cli;
Expand All @@ -27,12 +26,13 @@ fn main() {
let arguments = Arguments::parse();
trace!("The command line arguments provided are {arguments:?}.");

if run(arguments).is_err() {
if let Err(err) = run(arguments) {
error!("{:?}", err);
std::process::exit(ERROR_EXIT_CODE);
}
}

fn run(arguments: Arguments) -> Result<(), git2::Error> {
fn run(arguments: Arguments) -> Result<()> {
let commits = match (
arguments.from_stdin,
arguments.from_commit_hash,
Expand Down Expand Up @@ -63,17 +63,15 @@ fn run(arguments: Arguments) -> Result<(), git2::Error> {
)
}
(_, _, _) => {
unreachable!("Invalid combination of arguments.");
bail!("Invalid combination of arguments.");
}
}?;
let expected_version =
commits.get_next_version(arguments.from_version, arguments.calculation_mode);

if let Some(current_version) = arguments.current_version {
if current_version < expected_version {
let error_message = format!("The current version {current_version} is not larger or equal to the expected version {expected_version}.");
error!("{error_message}");
return Err(git2::Error::from_str(&error_message));
bail!(format!("The current version {current_version} is not larger or equal to the expected version {expected_version}."));
}
info!("The current version {current_version} is larger or equal to the expected version {expected_version}.");
} else {
Expand Down

0 comments on commit 1020045

Please sign in to comment.