From 79a1b5af30e239067f99e46108915c2e23aab871 Mon Sep 17 00:00:00 2001 From: Wynd Date: Sun, 17 Nov 2024 14:49:15 +0200 Subject: [PATCH] New clap arg to output the given semver to stdout --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/main.rs | 33 +++++++++++++++++++++++++++------ 3 files changed, 29 insertions(+), 8 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 8381f44..f385233 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -53,7 +53,7 @@ dependencies = [ [[package]] name = "cargo-semver" -version = "0.1.0" +version = "0.2.0" dependencies = [ "clap", "semver", diff --git a/Cargo.toml b/Cargo.toml index 744fdce..5eabbfd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "cargo-semver" -version = "0.1.0" +version = "0.2.0" edition = "2021" keywords = ["cargo", "semver"] categories = [ diff --git a/src/main.rs b/src/main.rs index 7f158dd..8e68898 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,6 @@ -use std::fs; +use std::{default, fs}; -use clap::{command, Parser}; +use clap::{command, Parser, ValueEnum}; use semver::Version; use toml_edit::{value, DocumentMut}; @@ -11,6 +11,13 @@ enum CargoCli { Semver(SemverArgs), } +#[derive(Clone, Debug, PartialEq, Eq, ValueEnum)] +pub enum VerType { + Major, + Minor, + Patch, +} + #[derive(Clone, Debug, Parser, PartialEq, Eq)] #[command(version, about, long_about = None)] pub struct SemverArgs { @@ -22,15 +29,14 @@ pub struct SemverArgs { #[arg(long("bump-major"), default_value_t = false)] pub bump_major: bool, + + #[arg(long("get"), value_enum)] + pub get: Option, } fn main() { let CargoCli::Semver(args) = CargoCli::parse(); - if !args.bump_major && !args.bump_minor && !args.bump_patch { - panic!("Specify which version to bump by using --bump-patch --bump-minor or --bump-major"); - } - let toml = fs::read_to_string("./Cargo.toml").unwrap(); let mut doc = toml.parse::().expect("invalid doc"); @@ -38,6 +44,21 @@ fn main() { let semver = semver.trim(); let mut semver = Version::parse(semver).expect("invalid semver"); + if let Some(ver_type) = args.get { + let ver = match ver_type { + VerType::Major => semver.major, + VerType::Minor => semver.minor, + VerType::Patch => semver.patch, + }; + + print!("{ver}"); + std::process::exit(0); + } + + if !args.bump_major && !args.bump_minor && !args.bump_patch { + panic!("Specify which version to bump by using --bump-patch --bump-minor or --bump-major"); + } + if args.bump_patch { semver.patch += 1; }