Skip to content

Commit

Permalink
Fix schema
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Oct 3, 2024
1 parent ae0be0e commit 5a563c6
Show file tree
Hide file tree
Showing 8 changed files with 151 additions and 102 deletions.
56 changes: 31 additions & 25 deletions crates/uv-cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use uv_configuration::{
ConfigSettingEntry, ExportFormat, IndexStrategy, KeyringProviderType, PackageNameSpecifier,
TargetTriple, TrustedHost, TrustedPublishing, VersionControlSystem,
};
use uv_distribution_types::{Index, IndexUrl, Origin};
use uv_distribution_types::{Index, IndexUrl, Origin, PipExtraIndex, PipFindLinks, PipIndex};
use uv_normalize::{ExtraName, PackageName};
use uv_pep508::Requirement;
use uv_pypi_types::VerbatimParsedUrl;
Expand Down Expand Up @@ -772,48 +772,54 @@ impl<T> Maybe<T> {
}
}

/// Parse an `--index-url` argument into an [`Index`], mapping the empty string to `None`.
fn parse_index_url(input: &str) -> Result<Maybe<Index>, String> {
/// Parse an `--index-url` argument into an [`PipIndex`], mapping the empty string to `None`.
fn parse_index_url(input: &str) -> Result<Maybe<PipIndex>, String> {
if input.is_empty() {
Ok(Maybe::None)
} else {
match IndexUrl::from_str(input).map(Index::from_index_url) {
Ok(index) => Ok(Maybe::Some(Index {
IndexUrl::from_str(input)
.map(Index::from_index_url)
.map(|index| Index {
origin: Some(Origin::Cli),
..index
})),
Err(err) => Err(err.to_string()),
}
})
.map(PipIndex::from)
.map(Maybe::Some)
.map_err(|err| err.to_string())
}
}

/// Parse an `--extra-index-url` argument into an [`Index`], mapping the empty string to `None`.
fn parse_extra_index_url(input: &str) -> Result<Maybe<Index>, String> {
/// Parse an `--extra-index-url` argument into an [`PipExtraIndex`], mapping the empty string to `None`.
fn parse_extra_index_url(input: &str) -> Result<Maybe<PipExtraIndex>, String> {
if input.is_empty() {
Ok(Maybe::None)
} else {
match IndexUrl::from_str(input).map(Index::from_extra_index_url) {
Ok(index) => Ok(Maybe::Some(Index {
IndexUrl::from_str(input)
.map(Index::from_extra_index_url)
.map(|index| Index {
origin: Some(Origin::Cli),
..index
})),
Err(err) => Err(err.to_string()),
}
})
.map(PipExtraIndex::from)
.map(Maybe::Some)
.map_err(|err| err.to_string())
}
}

/// Parse a `--find-links` argument into an [`Index`], mapping the empty string to `None`.
fn parse_find_links(input: &str) -> Result<Maybe<Index>, String> {
/// Parse a `--find-links` argument into an [`PipFindLinks`], mapping the empty string to `None`.
fn parse_find_links(input: &str) -> Result<Maybe<PipFindLinks>, String> {
if input.is_empty() {
Ok(Maybe::None)
} else {
match IndexUrl::from_str(input).map(Index::from_find_links) {
Ok(index) => Ok(Maybe::Some(Index {
IndexUrl::from_str(input)
.map(Index::from_find_links)
.map(|index| Index {
origin: Some(Origin::Cli),
..index
})),
Err(err) => Err(err.to_string()),
}
})
.map(PipFindLinks::from)
.map(Maybe::Some)
.map_err(|err| err.to_string())
}
}

Expand Down Expand Up @@ -3800,7 +3806,7 @@ pub struct IndexArgs {
/// The index given by this flag is given lower priority than all other
/// indexes specified via the `--extra-index-url` flag.
#[arg(long, short, env = "UV_INDEX_URL", value_parser = parse_index_url, help_heading = "Index options")]
pub index_url: Option<Maybe<Index>>,
pub index_url: Option<Maybe<PipIndex>>,

/// (Deprecated: use `--index` instead) Extra URLs of package indexes to use, in addition to `--index-url`.
///
Expand All @@ -3811,7 +3817,7 @@ pub struct IndexArgs {
/// `--index-url` (which defaults to PyPI). When multiple `--extra-index-url` flags are
/// provided, earlier values take priority.
#[arg(long, env = "UV_EXTRA_INDEX_URL", value_delimiter = ' ', value_parser = parse_extra_index_url, help_heading = "Index options")]
pub extra_index_url: Option<Vec<Maybe<Index>>>,
pub extra_index_url: Option<Vec<Maybe<PipExtraIndex>>>,

/// Locations to search for candidate distributions, in addition to those found in the registry
/// indexes.
Expand All @@ -3822,7 +3828,7 @@ pub struct IndexArgs {
/// If a URL, the page must contain a flat list of links to package files adhering to the
/// formats described above.
#[arg(long, short, value_parser = parse_find_links, help_heading = "Index options")]
pub find_links: Option<Vec<Maybe<Index>>>,
pub find_links: Option<Vec<Maybe<PipFindLinks>>>,

/// Ignore the registry index (e.g., PyPI), instead relying on direct URL dependencies and those
/// provided via `--find-links`.
Expand Down
5 changes: 4 additions & 1 deletion crates/uv-cli/src/options.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use uv_cache::Refresh;
use uv_configuration::ConfigSettings;
use uv_distribution_types::{PipExtraIndex, PipFindLinks, PipIndex};
use uv_resolver::PrereleaseMode;
use uv_settings::{Combine, PipOptions, ResolverInstallerOptions, ResolverOptions};

Expand Down Expand Up @@ -201,18 +202,20 @@ impl From<IndexArgs> for PipOptions {
.combine(
index.map(|index| index.into_iter().filter_map(Maybe::into_option).collect()),
),
index_url: index_url.and_then(Maybe::into_option),
index_url: index_url.and_then(Maybe::into_option).map(PipIndex::from),
extra_index_url: extra_index_url.map(|extra_index_urls| {
extra_index_urls
.into_iter()
.filter_map(Maybe::into_option)
.map(PipExtraIndex::from)
.collect()
}),
no_index: if no_index { Some(true) } else { None },
find_links: find_links.map(|find_links| {
find_links
.into_iter()
.filter_map(Maybe::into_option)
.map(PipFindLinks::from)
.collect()
}),
..PipOptions::default()
Expand Down
2 changes: 2 additions & 0 deletions crates/uv-distribution-types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ pub use crate::index::*;
pub use crate::index_url::*;
pub use crate::installed::*;
pub use crate::origin::*;
pub use crate::pip_index::*;
pub use crate::prioritized_distribution::*;
pub use crate::resolution::*;
pub use crate::resolved::*;
Expand All @@ -81,6 +82,7 @@ mod index;
mod index_url;
mod installed;
mod origin;
mod pip_index;
mod prioritized_distribution;
mod resolution;
mod resolved;
Expand Down
59 changes: 59 additions & 0 deletions crates/uv-distribution-types/src/pip_index.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
//! Compatibility structs for converting between [`IndexUrl`] and [`Index`]. These structs are
//! parsed and deserialized as [`IndexUrl`], but are stored as [`Index`] with the appropriate
//! flags set.

use serde::{Deserialize, Deserializer, Serialize};

use crate::{Index, IndexUrl};

macro_rules! impl_index {
($name:ident, $from:expr) => {
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct $name(Index);

impl From<$name> for Index {
fn from(value: $name) -> Self {
value.0
}
}

impl From<Index> for $name {
fn from(value: Index) -> Self {
Self(value)
}
}

impl Serialize for $name {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
self.0.url().serialize(serializer)
}
}

impl<'de> Deserialize<'de> for $name {
fn deserialize<D>(deserializer: D) -> Result<$name, D::Error>
where
D: Deserializer<'de>,
{
IndexUrl::deserialize(deserializer).map($from).map(Self)
}
}

#[cfg(feature = "schemars")]
impl schemars::JsonSchema for $name {
fn schema_name() -> String {
IndexUrl::schema_name()
}

fn json_schema(gen: &mut schemars::gen::SchemaGenerator) -> schemars::schema::Schema {
IndexUrl::json_schema(gen)
}
}
};
}

impl_index!(PipIndex, Index::from_index_url);
impl_index!(PipExtraIndex, Index::from_extra_index_url);
impl_index!(PipFindLinks, Index::from_find_links);
7 changes: 5 additions & 2 deletions crates/uv-settings/src/combine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use url::Url;
use uv_configuration::{
ConfigSettings, IndexStrategy, KeyringProviderType, TargetTriple, TrustedPublishing,
};
use uv_distribution_types::{Index, IndexUrl};
use uv_distribution_types::{Index, IndexUrl, PipExtraIndex, PipFindLinks, PipIndex};
use uv_install_wheel::linker::LinkMode;
use uv_pypi_types::SupportedEnvironments;
use uv_python::{PythonDownloads, PythonPreference, PythonVersion};
Expand Down Expand Up @@ -75,11 +75,13 @@ impl_combine_or!(ExcludeNewer);
impl_combine_or!(Index);
impl_combine_or!(IndexStrategy);
impl_combine_or!(IndexUrl);
impl_combine_or!(Url);
impl_combine_or!(KeyringProviderType);
impl_combine_or!(LinkMode);
impl_combine_or!(NonZeroUsize);
impl_combine_or!(PathBuf);
impl_combine_or!(PipExtraIndex);
impl_combine_or!(PipFindLinks);
impl_combine_or!(PipIndex);
impl_combine_or!(PrereleaseMode);
impl_combine_or!(PythonDownloads);
impl_combine_or!(PythonPreference);
Expand All @@ -89,6 +91,7 @@ impl_combine_or!(String);
impl_combine_or!(SupportedEnvironments);
impl_combine_or!(TargetTriple);
impl_combine_or!(TrustedPublishing);
impl_combine_or!(Url);
impl_combine_or!(bool);

impl<T> Combine for Option<Vec<T>> {
Expand Down
Loading

0 comments on commit 5a563c6

Please sign in to comment.