Add preview mode and use for warning in uv run (#3192)

Adds hidden `--preview` / `--no-preview` flags with `UV_PREVIEW`
environment variable support. Copies the `PreviewMode` type from Ruff.

Does a little bit of extra work to port `uv run` to the new settings
model.

Note we allow `uv run` invocations without preview and only use its
presence to toggle an experimental warning.

## Test plan

```
❯ cargo run -q -- run --no-workspace -- python --version
warning: `uv run` is experimental and may change without warning.
Python 3.12.2
❯ cargo run -q -- run --no-workspace --preview -- python --version
Python 3.12.2
❯ UV_PREVIEW=1 cargo run -q -- run --no-workspace -- python --version
Python 3.12.2
```
This commit is contained in:
Zanie Blue 2024-04-22 15:41:15 -05:00 committed by GitHub
parent 11c6a07bb5
commit b9419e67aa
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 109 additions and 3 deletions

View file

@ -5,6 +5,7 @@ pub use constraints::*;
pub use name_specifiers::*;
pub use overrides::*;
pub use package_options::*;
pub use preview::*;
pub use target_triple::*;
mod authentication;
@ -14,4 +15,5 @@ mod constraints;
mod name_specifiers;
mod overrides;
mod package_options;
mod preview;
mod target_triple;

View file

@ -0,0 +1,37 @@
use std::fmt::{Display, Formatter};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum PreviewMode {
#[default]
Disabled,
Enabled,
}
impl PreviewMode {
pub fn is_enabled(&self) -> bool {
matches!(self, Self::Enabled)
}
pub fn is_disabled(&self) -> bool {
matches!(self, Self::Disabled)
}
}
impl From<bool> for PreviewMode {
fn from(version: bool) -> Self {
if version {
PreviewMode::Enabled
} else {
PreviewMode::Disabled
}
}
}
impl Display for PreviewMode {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
Self::Disabled => write!(f, "disabled"),
Self::Enabled => write!(f, "enabled"),
}
}
}