use regex::Regex;
use semver::Version;
use crate::VersionIncrement;
#[derive(Debug)]
pub struct VersionUpdater {
pub(crate) features_always_increment_minor: bool,
pub(crate) breaking_always_increment_major: bool,
pub(crate) custom_major_increment_regex: Option<Regex>,
pub(crate) custom_minor_increment_regex: Option<Regex>,
}
impl Default for VersionUpdater {
fn default() -> Self {
Self::new()
}
}
impl VersionUpdater {
pub fn new() -> Self {
Self {
features_always_increment_minor: false,
breaking_always_increment_major: false,
custom_major_increment_regex: None,
custom_minor_increment_regex: None,
}
}
pub fn with_features_always_increment_minor(
mut self,
features_always_increment_minor: bool,
) -> Self {
self.features_always_increment_minor = features_always_increment_minor;
self
}
pub fn with_breaking_always_increment_major(
mut self,
breaking_always_increment_major: bool,
) -> Self {
self.breaking_always_increment_major = breaking_always_increment_major;
self
}
pub fn with_custom_major_increment_regex(
mut self,
custom_major_increment_regex: &str,
) -> Result<Self, regex::Error> {
let regex = Regex::new(custom_major_increment_regex)?;
self.custom_major_increment_regex = Some(regex);
Ok(self)
}
pub fn with_custom_minor_increment_regex(
mut self,
custom_minor_increment_regex: &str,
) -> Result<Self, regex::Error> {
let regex = Regex::new(custom_minor_increment_regex)?;
self.custom_minor_increment_regex = Some(regex);
Ok(self)
}
pub fn increment<I>(self, version: &Version, commits: I) -> Version
where
I: IntoIterator,
I::Item: AsRef<str>,
{
let increment = VersionIncrement::from_commits_with_updater(&self, version, commits);
match increment {
Some(increment) => increment.bump(version),
None => version.clone(),
}
}
}