Add serialization and deserialization for --find-links (#3619)

## Summary

Closes https://github.com/astral-sh/uv/issues/3617.
This commit is contained in:
Charlie Marsh 2024-05-15 15:11:07 -04:00 committed by GitHub
parent 018a7150d6
commit a735f6d80c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 51 additions and 3 deletions

View file

@ -6,7 +6,6 @@ use std::str::FromStr;
use itertools::Either; use itertools::Either;
use once_cell::sync::Lazy; use once_cell::sync::Lazy;
use serde::{Deserialize, Serialize};
use url::Url; use url::Url;
use pep508_rs::{expand_env_vars, split_scheme, strip_host, Scheme, VerbatimUrl}; use pep508_rs::{expand_env_vars, split_scheme, strip_host, Scheme, VerbatimUrl};
@ -110,7 +109,7 @@ impl serde::ser::Serialize for IndexUrl {
where where
S: serde::ser::Serializer, S: serde::ser::Serializer,
{ {
self.verbatim().serialize(serializer) self.to_string().serialize(serializer)
} }
} }
@ -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. /// A directory with distributions or a URL to an HTML file with a flat listing of distributions.
/// ///
/// Also known as `--find-links`. /// Also known as `--find-links`.
#[derive(Debug, Clone, Hash, Eq, PartialEq, Serialize, Deserialize)] #[derive(Debug, Clone, Hash, Eq, PartialEq)]
pub enum FlatIndexLocation { pub enum FlatIndexLocation {
Path(PathBuf), Path(PathBuf),
Url(Url), Url(Url),
@ -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 { impl FromStr for FlatIndexLocation {
type Err = url::ParseError; type Err = url::ParseError;

View file

@ -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(()) Ok(())
} }