use kernel::Maturity;
use sprawling::release;
use sprawling::release::Built;
use std::process::ExitCode;
use wire::{Registry, RegistryNewest, RegistryReading, ReleaseAnswer};
pub(super) fn headline(maturity: Maturity) -> String {
format!(
"sprawling {} ({}){}",
env!("CARGO_PKG_VERSION"),
maturity.word(),
cut()
)
}
fn cut() -> String {
match release::built() {
Ok(Built::Released(mine)) => format!(", released {}", mine.released()),
Ok(Built::FromSource) => String::from(", built from source"),
Err(err) => format!(", carrying a tag that does not decode: {}", err.recovery()),
}
}
pub(super) fn check() -> ExitCode {
println!();
match release::answer() {
ReleaseAnswer::Refused { refusal } => {
eprintln!("could not check: {refusal}");
eprintln!("recovery: {}", refusal.recovery());
ExitCode::FAILURE
}
ReleaseAnswer::Unreleased { registries, .. } => {
println!("this binary was built from source, so it is none of the published releases.");
for line in ®istries {
println!("{}", registry_said(line));
}
ExitCode::SUCCESS
}
ReleaseAnswer::Stands {
mine,
registries,
verdict,
update,
} => {
let Some(newest) = registries.iter().find_map(|line| match &line.reading {
RegistryReading::Read { newest } if line.registry == Registry::Npm => Some(newest),
RegistryReading::Read { .. } | RegistryReading::Refused { .. } => None,
}) else {
eprintln!("could not check: the answer carried no npm reading");
return ExitCode::FAILURE;
};
match verdict {
kernel::ReleaseVerdict::Current => println!(
"{} is the newest release published. Nothing to do.",
mine.version
),
kernel::ReleaseVerdict::Behind => {
println!(
"a newer release is published: {}, cut {} (this binary is {}).",
newest.version, newest.released, mine.version
);
println!();
match &update.command {
Some(command) => println!(" update {command}"),
None => println!(
" update this binary's install channel is not one this city can name; every archive is at https://github.com/2youg1/sprawling-agents/releases"
),
}
println!();
println!(
"Nothing was downloaded and nothing was changed. \
Updating is yours to run."
);
}
kernel::ReleaseVerdict::Ahead => println!(
"this binary ({}) is newer than the newest on npm ({}); \
the packages are published after the release page.",
mine.version, newest.version
),
}
ExitCode::SUCCESS
}
}
}
fn registry_said(line: &RegistryNewest) -> String {
let registry = match line.registry {
Registry::Npm => "npm",
Registry::CratesIo => "crates.io",
};
match &line.reading {
RegistryReading::Read { newest } => format!(
"newest on {registry}: {}, cut {}",
newest.version, newest.released
),
RegistryReading::Refused { refusal } => format!("{registry} could not be read: {refusal}"),
}
}
#[cfg(test)]
mod tests {
use super::{cut, headline};
use kernel::Maturity;
#[test]
fn the_status_line_says_the_maturity_it_is_given() {
assert_eq!(
headline(Maturity::Alpha),
format!("sprawling {} (alpha){}", env!("CARGO_PKG_VERSION"), cut())
);
}
}