Skip to content

Commit

Permalink
Simplify options
Browse files Browse the repository at this point in the history
  • Loading branch information
zanieb committed Jun 14, 2024
1 parent 5794aa6 commit 473ea73
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 87 deletions.
9 changes: 2 additions & 7 deletions crates/uv/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1750,13 +1750,8 @@ pub(crate) struct ToolchainInstallArgs {
#[derive(Args)]
#[allow(clippy::struct_excessive_bools)]
pub(crate) struct ToolchainFindArgs {
/// The version to find.
#[arg(long)]
pub(crate) version: Option<String>,

/// The implementation to find.
#[arg(long)]
pub(crate) implementation: Option<String>,
/// The toolchain request.
pub(crate) request: Option<String>,
}

#[derive(Args)]
Expand Down
26 changes: 5 additions & 21 deletions crates/uv/src/commands/toolchain/find.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
use anyhow::Result;
use std::fmt::Write;
use std::str::FromStr;

use uv_cache::Cache;
use uv_configuration::PreviewMode;
use uv_fs::Simplified;
use uv_toolchain::{ImplementationName, SystemPython, Toolchain, ToolchainRequest, VersionRequest};
use uv_toolchain::{SystemPython, Toolchain, ToolchainRequest};
use uv_warnings::warn_user;

use crate::commands::ExitStatus;
Expand All @@ -14,8 +13,7 @@ use crate::printer::Printer;
/// Find a toolchain.
#[allow(clippy::too_many_arguments)]
pub(crate) async fn find(
version: Option<String>,
implementation: Option<String>,
request: Option<String>,
preview: PreviewMode,
cache: &Cache,
printer: Printer,
Expand All @@ -24,24 +22,10 @@ pub(crate) async fn find(
warn_user!("`uv toolchain find` is experimental and may change without warning.");
}

let implementation = implementation
.as_deref()
.map(ImplementationName::from_str)
.transpose()?;
let version = version
.as_deref()
.map(VersionRequest::from_str)
.transpose()?;

let request = match (version, implementation) {
(None, None) => ToolchainRequest::Any,
(Some(version), None) => ToolchainRequest::Version(version),
(Some(version), Some(implementation)) => {
ToolchainRequest::ImplementationVersion(implementation, version)
}
(None, Some(implementation)) => ToolchainRequest::Implementation(implementation),
let request = match request {
Some(request) => ToolchainRequest::parse(&request),
None => ToolchainRequest::Any,
};

let toolchain = Toolchain::find_requested(
&request,
SystemPython::Required,
Expand Down
11 changes: 2 additions & 9 deletions crates/uv/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -801,19 +801,12 @@ async fn run() -> Result<ExitStatus> {
command: ToolchainCommand::Find(args),
}) => {
// Resolve the settings from the command-line arguments and workspace configuration.
let args = settings::ToolchainFindSettings::resolve(args, workspace);
let args = settings::ToolchainFindSettings::resolve(args, filesystem);

// Initialize the cache.
let cache = cache.init()?;

commands::toolchain_find(
args.version,
args.implementation,
globals.preview,
&cache,
printer,
)
.await
commands::toolchain_find(args.request, globals.preview, &cache, printer).await
}
}
}
Expand Down
15 changes: 4 additions & 11 deletions crates/uv/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,23 +278,16 @@ impl ToolchainInstallSettings {
#[allow(clippy::struct_excessive_bools)]
#[derive(Debug, Clone)]
pub(crate) struct ToolchainFindSettings {
pub(crate) version: Option<String>,
pub(crate) implementation: Option<String>,
pub(crate) request: Option<String>,
}

impl ToolchainFindSettings {
/// Resolve the [`ToolchainFindSettings`] from the CLI and workspace configuration.
#[allow(clippy::needless_pass_by_value)]
pub(crate) fn resolve(args: ToolchainFindArgs, _workspace: Option<Workspace>) -> Self {
let ToolchainFindArgs {
version,
implementation,
} = args;
pub(crate) fn resolve(args: ToolchainFindArgs, _filesystem: Option<FilesystemOptions>) -> Self {
let ToolchainFindArgs { request } = args;

Self {
version,
implementation,
}
Self { request }
}
}

Expand Down
40 changes: 1 addition & 39 deletions crates/uv/tests/toolchain_find.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ fn toolchain_find() {

// Request Python 3.12
uv_snapshot!(filters, context.toolchain_find()
.arg("--version")
.arg("3.12")
.env("UV_TEST_PYTHON_PATH", &python_path), @r###"
success: true
Expand All @@ -69,7 +68,6 @@ fn toolchain_find() {

// Request Python 3.11
uv_snapshot!(filters, context.toolchain_find()
.arg("--version")
.arg("3.11")
.env("UV_TEST_PYTHON_PATH", &python_path), @r###"
success: true
Expand All @@ -82,7 +80,6 @@ fn toolchain_find() {

// Request CPython
uv_snapshot!(filters, context.toolchain_find()
.arg("--implementation")
.arg("cpython")
.env("UV_TEST_PYTHON_PATH", &python_path), @r###"
success: true
Expand All @@ -95,10 +92,7 @@ fn toolchain_find() {

// Request CPython 3.12
uv_snapshot!(filters, context.toolchain_find()
.arg("--implementation")
.arg("cpython")
.arg("--version")
.arg("3.12")
.arg("cpython@3.12")
.env("UV_TEST_PYTHON_PATH", &python_path), @r###"
success: true
exit_code: 0
Expand All @@ -110,7 +104,6 @@ fn toolchain_find() {

// Request PyPy
uv_snapshot!(filters, context.toolchain_find()
.arg("--implementation")
.arg("pypy")
.env("UV_TEST_PYTHON_PATH", &python_path), @r###"
success: false
Expand All @@ -137,7 +130,6 @@ fn toolchain_find() {

// Request Python 3.11
uv_snapshot!(filters, context.toolchain_find()
.arg("--version")
.arg("3.11")
.env("UV_TEST_PYTHON_PATH", &python_path), @r###"
success: true
Expand All @@ -148,33 +140,3 @@ fn toolchain_find() {
----- stderr -----
"###);
}

#[test]
fn toolchain_find_invalid_implementation() {
let context = TestContext::new("3.12");

// No interpreters on the path
uv_snapshot!(context.filters(), context.toolchain_find().arg("--implementation").arg("foobar"), @r###"
success: false
exit_code: 2
----- stdout -----
----- stderr -----
error: Unknown Python implementation `foobar`
"###);
}

#[test]
fn toolchain_find_invalid_version() {
let context = TestContext::new("3.12");

// No interpreters on the path
uv_snapshot!(context.filters(), context.toolchain_find().arg("--version").arg("foobar"), @r###"
success: false
exit_code: 2
----- stdout -----
----- stderr -----
error: Invalid version request: foobar
"###);
}

0 comments on commit 473ea73

Please sign in to comment.