use anyhow::{Context, Result, bail, ensure};
use semver::Version;
use crate::config::Prerelease;
use crate::fragment::Bump;
#[must_use]
pub fn unreleased(version: &str) -> bool {
Version::parse(version)
.is_ok_and(|version| (version.major, version.minor, version.patch) == (0, 0, 0))
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub enum FirstRelease {
#[default]
Patch,
Minor,
}
pub fn apply(current: &str, bump: Bump, first: FirstRelease) -> Result<String> {
apply_with(current, bump, first, None)
}
pub fn apply_with(
current: &str,
bump: Bump,
first: FirstRelease,
prerelease: Option<&Prerelease>,
) -> Result<String> {
let version = Version::parse(current).with_context(|| format!("semver {current:?}"))?;
let counted = prerelease
.filter(|_| !version.pre.is_empty())
.map(|tag| Counted::read(&version, tag))
.transpose()?;
let settled = counted
.as_ref()
.map_or(version.pre.is_empty(), |counted| counted.same_tag);
if bump == Bump::None && settled {
return Ok(current.to_owned());
}
if unreleased(current) && bump > Bump::Patch && first == FirstRelease::Patch {
bail!(
"0.0.0 refuses a {} bump: a new package releases 0.0.1 first, from a patch \
fragment; set `first_minor: true` on the package to open at 0.1.0",
bump.as_str()
);
}
if bump == Bump::Major && version.major == 0 {
bail!("0.x refuses a major bump (current {current}); use minor or wait for 1.0");
}
let mut next = Version::new(version.major, version.minor, version.patch);
if version.pre.is_empty() || !reaches(&version, bump) {
match bump {
Bump::None => {}
Bump::Patch => next.patch += 1,
Bump::Minor => {
next.minor += 1;
next.patch = 0;
}
Bump::Major => {
next.major += 1;
next.minor = 0;
next.patch = 0;
}
}
}
let Some(tag) = prerelease else {
return Ok(next.to_string());
};
let same_base =
(next.major, next.minor, next.patch) == (version.major, version.minor, version.patch);
let count = match counted {
Some(counted) if counted.same_tag && same_base => counted.count + 1,
Some(_) | None => 1,
};
next.pre = semver::Prerelease::new(&format!("{}.{count}", tag.as_str()))
.with_context(|| format!("prerelease {:?}", tag.as_str()))?;
ensure!(
next > version,
"{next} sorts below the current {current}; set `prerelease` to a tag that sorts above \
the current one, or remove it to release the stable version"
);
Ok(next.to_string())
}
fn reaches(version: &Version, bump: Bump) -> bool {
let placeholder = (version.major, version.minor, version.patch) == (0, 0, 0);
match bump {
Bump::None => true,
Bump::Patch => !placeholder,
Bump::Minor => !placeholder && version.patch == 0,
Bump::Major => !placeholder && version.minor == 0 && version.patch == 0,
}
}
struct Counted {
same_tag: bool,
count: u64,
}
impl Counted {
fn read(version: &Version, tag: &Prerelease) -> Result<Self> {
let pre = version.pre.as_str();
let shaped = pre
.split_once('.')
.filter(|(name, _)| {
!name.is_empty() && name.bytes().all(|byte| byte.is_ascii_lowercase())
})
.and_then(|(name, count)| Some((name, count.parse::<u64>().ok()?)));
let Some((name, count)) = shaped else {
let base = format!("{}.{}.{}", version.major, version.minor, version.patch);
bail!(
"{version} has a prerelease verctl cannot count from; verctl writes \
`<tag>.<count>`, such as {base}-{}.2: change the version in the manifest to that \
shape",
tag.as_str()
);
};
Ok(Self {
same_tag: name == tag.as_str(),
count,
})
}
}