Improve uv toolchain list implementation (#4203)

Amends #4163 with review from Jane, thank you!

No behavior changes.
This commit is contained in:
Zanie Blue 2024-06-10 13:49:17 -04:00 committed by GitHub
parent 652c1126d3
commit 06a0fc65d4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1,5 +1,5 @@
use std::collections::BTreeSet;
use std::fmt::Write;
use std::ops::Deref;
use anyhow::Result;
use itertools::Itertools;
@ -26,18 +26,18 @@ pub(crate) async fn list(
warn_user!("`uv toolchain list` is experimental and may change without warning.");
}
let downloads = match includes {
ToolchainListIncludes::All => {
let request = PythonDownloadRequest::default();
request.iter_downloads().collect()
}
ToolchainListIncludes::Installed => Vec::new(),
ToolchainListIncludes::Default => {
let request = PythonDownloadRequest::from_env()?;
request.iter_downloads().collect()
}
let download_request = match includes {
ToolchainListIncludes::All => Some(PythonDownloadRequest::default()),
ToolchainListIncludes::Installed => None,
ToolchainListIncludes::Default => Some(PythonDownloadRequest::from_env()?),
};
let downloads = download_request
.as_ref()
.map(uv_toolchain::downloads::PythonDownloadRequest::iter_downloads)
.into_iter()
.flatten();
let installed = {
InstalledToolchains::from_settings()?
.init()?
@ -45,23 +45,21 @@ pub(crate) async fn list(
.collect_vec()
};
let mut output = Vec::new();
// Sort and de-duplicate the output.
let mut output = BTreeSet::new();
for toolchain in installed {
output.push((
toolchain.python_version().deref().version.clone(),
output.insert((
toolchain.python_version().version().clone(),
toolchain.key().to_owned(),
));
}
for download in downloads {
output.push((
download.python_version().deref().version.clone(),
output.insert((
download.python_version().version().clone(),
download.key().to_owned(),
));
}
output.sort();
output.dedup();
for (version, key) in output {
writeln!(printer.stdout(), "{:<8} ({key})", version.to_string())?;
}