use std::collections::BTreeSet;
pub const COOLDOWN_OFF: (&str, &str) = ("CARGO_RESOLVER_INCOMPATIBLE_PUBLISH_AGE", "allow");
#[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()
}
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;