use crate::error::KopiError;
use crate::storage::InstalledJdk;
pub fn format_multiple_jdk_matches_error(
version_spec: &str,
matching_jdks: &[InstalledJdk],
) -> KopiError {
let jdk_list: Vec<String> = matching_jdks
.iter()
.map(|jdk| format!(" - {}@{}", jdk.distribution, jdk.version))
.collect();
eprintln!("Error: Multiple JDKs match the pattern '{version_spec}'");
eprintln!("\nFound the following JDKs:");
for jdk_str in &jdk_list {
eprintln!("{jdk_str}");
}
eprintln!("\nPlease specify exactly one JDK to uninstall using the full version:");
eprintln!(" kopi uninstall <distribution>@<full-version>");
eprintln!("\nExample:");
if let Some(first_jdk) = matching_jdks.first() {
eprintln!(
" kopi uninstall {}@{}",
first_jdk.distribution, first_jdk.version
);
}
KopiError::SystemError(format!(
"Multiple JDKs match '{version_spec}'. Please specify exactly one JDK to uninstall"
))
}
pub fn format_no_jdk_matches_error(version_spec: &str) -> KopiError {
eprintln!("Error: No JDKs match the pattern '{version_spec}'");
eprintln!("\nUse 'kopi list' to see available JDKs");
eprintln!("Use 'kopi uninstall <distribution>@<version>' to uninstall a specific JDK");
KopiError::SystemError(format!(
"No JDKs match '{version_spec}'. Use 'kopi list' to see available JDKs"
))
}
pub fn format_jdk_not_found_error(distribution: &str, version: &str) -> KopiError {
eprintln!("Error: JDK {distribution}@{version} is not installed");
eprintln!("\nUse 'kopi list' to see available JDKs");
eprintln!("Use 'kopi install {distribution}@{version}' to install this JDK");
KopiError::SystemError(format!("JDK {distribution}@{version} is not installed"))
}
pub fn format_uninstall_cancelled_error(jdk: &InstalledJdk) -> KopiError {
eprintln!(
"Uninstall cancelled for {}@{}",
jdk.distribution, jdk.version
);
KopiError::SystemError(format!(
"Uninstall cancelled for {}@{}",
jdk.distribution, jdk.version
))
}
#[cfg(test)]
mod tests {
use super::*;
use crate::test::fixtures::create_test_jdk_collection;
#[test]
fn test_format_multiple_jdk_matches_error() {
let jdks = create_test_jdk_collection();
let error = format_multiple_jdk_matches_error("temurin", &jdks[0..2]);
match error {
KopiError::SystemError(msg) => {
assert!(msg.contains("Multiple JDKs match 'temurin'"));
}
_ => panic!("Expected SystemError"),
}
}
#[test]
fn test_format_no_jdk_matches_error() {
let error = format_no_jdk_matches_error("nonexistent");
match error {
KopiError::SystemError(msg) => {
assert!(msg.contains("No JDKs match 'nonexistent'"));
}
_ => panic!("Expected SystemError"),
}
}
#[test]
fn test_format_jdk_not_found_error() {
let error = format_jdk_not_found_error("temurin", "99.0.0");
match error {
KopiError::SystemError(msg) => {
assert!(msg.contains("JDK temurin@99.0.0 is not installed"));
}
_ => panic!("Expected SystemError"),
}
}
#[test]
fn test_format_uninstall_cancelled_error() {
let jdk = create_test_jdk_collection().into_iter().next().unwrap();
let error = format_uninstall_cancelled_error(&jdk);
match error {
KopiError::SystemError(msg) => {
assert!(msg.contains("Uninstall cancelled"));
}
_ => panic!("Expected SystemError"),
}
}
}