Skip to content

Commit

Permalink
Add serialization and deserialization for --find-links (#3619)
Browse files Browse the repository at this point in the history
## Summary

Closes #3617.
  • Loading branch information
charliermarsh committed May 15, 2024
1 parent 018a715 commit a735f6d
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 3 deletions.
24 changes: 21 additions & 3 deletions crates/distribution-types/src/index_url.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use std::str::FromStr;

use itertools::Either;
use once_cell::sync::Lazy;
use serde::{Deserialize, Serialize};
use url::Url;

use pep508_rs::{expand_env_vars, split_scheme, strip_host, Scheme, VerbatimUrl};
Expand Down Expand Up @@ -110,7 +109,7 @@ impl serde::ser::Serialize for IndexUrl {
where
S: serde::ser::Serializer,
{
self.verbatim().serialize(serializer)
self.to_string().serialize(serializer)
}
}

Expand Down Expand Up @@ -159,7 +158,7 @@ impl Deref for IndexUrl {
/// A directory with distributions or a URL to an HTML file with a flat listing of distributions.
///
/// Also known as `--find-links`.
#[derive(Debug, Clone, Hash, Eq, PartialEq, Serialize, Deserialize)]
#[derive(Debug, Clone, Hash, Eq, PartialEq)]
pub enum FlatIndexLocation {
Path(PathBuf),
Url(Url),
Expand All @@ -185,6 +184,25 @@ impl schemars::JsonSchema for FlatIndexLocation {
}
}

impl serde::ser::Serialize for FlatIndexLocation {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::ser::Serializer,
{
self.to_string().serialize(serializer)
}
}

impl<'de> serde::de::Deserialize<'de> for FlatIndexLocation {
fn deserialize<D>(deserializer: D) -> Result<FlatIndexLocation, D::Error>
where
D: serde::de::Deserializer<'de>,
{
let s = String::deserialize(deserializer)?;
FlatIndexLocation::from_str(&s).map_err(serde::de::Error::custom)
}
}

impl FromStr for FlatIndexLocation {
type Err = url::ParseError;

Expand Down
30 changes: 30 additions & 0 deletions crates/uv/tests/pip_compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8653,6 +8653,36 @@ fn resolve_configuration() -> Result<()> {
"###
);

// Write out a `--find-links` entry.
// Add an extra index URL entry to the `pyproject.toml` file.
pyproject.write_str(indoc::indoc! {r#"
[project]
name = "example"
version = "0.0.0"
[tool.uv.pip]
no-index = true
find-links = ["https://download.pytorch.org/whl/torch_stable.html"]
"#})?;

let requirements_in = context.temp_dir.child("requirements.in");
requirements_in.write_str("tqdm")?;

uv_snapshot!(context.compile()
.arg("requirements.in"), @r###"
success: true
exit_code: 0
----- stdout -----
# This file was autogenerated by uv via the following command:
# uv pip compile --cache-dir [CACHE_DIR] --exclude-newer 2024-03-25T00:00:00Z requirements.in
tqdm==4.66.2
# via -r requirements.in
----- stderr -----
Resolved 1 package in [TIME]
"###
);

Ok(())
}

Expand Down

0 comments on commit a735f6d

Please sign in to comment.