verctl-core 0.9.6

Core configuration, manifest, fragment, changelog, and version domain logic for verctl
Documentation
//! What cargo's publish-age cooldown refuses, and the setting that turns the
//! cooldown off for one run.
//!
//! `semver` and `release` both read cargo's error for the crates the cooldown
//! refused, so the two checks parse it one way.

use std::collections::BTreeSet;

/// The setting that lets cargo resolve a release younger than the cooldown.
pub const COOLDOWN_OFF: (&str, &str) = ("CARGO_RESOLVER_INCOMPATIBLE_PUBLISH_AGE", "allow");

/// The crates whose release the publish-age cooldown rejected, from cargo's
/// error in `output`, sorted and without repeats.
///
/// Cargo 1.100 reports one as `failed to select a version for the
/// requirement` naming the crate, followed by any number of `version ... is
/// yanked` lines and then `version <ver> is too new (published ...)`.
#[must_use]
pub fn cooldown_rejections(output: &str) -> Vec<String> {
    const REQUIREMENT: &str = "failed to select a version for the requirement `";
    const TOO_NEW: &str = " is too new (published ";
    let plain = strip_ansi(output);
    let lines: Vec<&str> = plain.lines().collect();
    let mut names = BTreeSet::new();
    for (index, line) in lines.iter().enumerate() {
        let Some((_, rest)) = line.split_once(REQUIREMENT) else {
            continue;
        };
        let Some((name, _)) = rest.split_once(" = ") else {
            continue;
        };
        for next in lines.iter().skip(index + 1) {
            let trimmed = next.trim();
            if trimmed.is_empty()
                || trimmed.starts_with("location searched:")
                || trimmed.contains(REQUIREMENT)
            {
                break;
            }
            if let Some(after_version) = trimmed.strip_prefix("version ")
                && after_version.contains(TOO_NEW)
            {
                names.insert(name.to_owned());
            }
        }
    }
    names.into_iter().collect()
}

/// `text` without its ANSI escape sequences: `ESC [`, then everything up to a
/// final byte in `@` to `~`.
fn strip_ansi(text: &str) -> String {
    let mut plain = String::with_capacity(text.len());
    let mut chars = text.chars().peekable();
    while let Some(next) = chars.next() {
        if next == '\x1b' && chars.next_if_eq(&'[').is_some() {
            for skipped in chars.by_ref() {
                if ('@'..='~').contains(&skipped) {
                    break;
                }
            }
        } else {
            plain.push(next);
        }
    }
    plain
}

#[cfg(test)]
mod tests;