Skip to content

Commit 410dc33

Browse files
authored
Make DisplaySafeUrlRef Copy and other minor PR follow-ups (#13683)
This PR implements a few review follow-ups from #13560. In particular, it * Makes `DisplaySafeUrlRef` implement `Copy` so that it can be passed by value. * Updates `to_string_with_credentials` methods with `displayable_with_credentials`, returning an `impl Display` instead of `String` for greater flexibility. * Updates the `DisplaySafeUrl` and `DisplaySafeUrlRef` `Debug` implementations to match the underlying `Url` `Debug` implementation (with the exception that credentials are masked). * Replaces an unnecessary `DisplaySafeUrl::from(Url::from_file_path` with `DisplaySafeUrl::from_file_path`
1 parent 90a21ae commit 410dc33

16 files changed

+1610
-167
lines changed

crates/uv-auth/src/credentials.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ impl Credentials {
145145
/// If a username is provided, it must match the login in the netrc file or [`None`] is returned.
146146
pub(crate) fn from_netrc(
147147
netrc: &Netrc,
148-
url: &DisplaySafeUrlRef<'_>,
148+
url: DisplaySafeUrlRef<'_>,
149149
username: Option<&str>,
150150
) -> Option<Self> {
151151
let host = url.host_str()?;

crates/uv-auth/src/keyring.rs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ impl KeyringProvider {
3838
#[instrument(skip_all, fields(url = % url.to_string(), username))]
3939
pub async fn fetch(
4040
&self,
41-
url: &DisplaySafeUrlRef<'_>,
41+
url: DisplaySafeUrlRef<'_>,
4242
username: Option<&str>,
4343
) -> Option<Credentials> {
4444
// Validate the request
@@ -229,7 +229,7 @@ mod tests {
229229
let keyring = KeyringProvider::empty();
230230
// Panics due to debug assertion; returns `None` in production
231231
let result = std::panic::AssertUnwindSafe(
232-
keyring.fetch(&DisplaySafeUrlRef::from(&url), Some("user")),
232+
keyring.fetch(DisplaySafeUrlRef::from(&url), Some("user")),
233233
)
234234
.catch_unwind()
235235
.await;
@@ -242,7 +242,7 @@ mod tests {
242242
let keyring = KeyringProvider::empty();
243243
// Panics due to debug assertion; returns `None` in production
244244
let result = std::panic::AssertUnwindSafe(
245-
keyring.fetch(&DisplaySafeUrlRef::from(&url), Some(url.username())),
245+
keyring.fetch(DisplaySafeUrlRef::from(&url), Some(url.username())),
246246
)
247247
.catch_unwind()
248248
.await;
@@ -255,7 +255,7 @@ mod tests {
255255
let keyring = KeyringProvider::empty();
256256
// Panics due to debug assertion; returns `None` in production
257257
let result = std::panic::AssertUnwindSafe(
258-
keyring.fetch(&DisplaySafeUrlRef::from(&url), Some(url.username())),
258+
keyring.fetch(DisplaySafeUrlRef::from(&url), Some(url.username())),
259259
)
260260
.catch_unwind()
261261
.await;
@@ -267,7 +267,7 @@ mod tests {
267267
let url = Url::parse("https://example.com").unwrap();
268268
let url = DisplaySafeUrlRef::from(&url);
269269
let keyring = KeyringProvider::empty();
270-
let credentials = keyring.fetch(&url, Some("user"));
270+
let credentials = keyring.fetch(url, Some("user"));
271271
assert!(credentials.await.is_none());
272272
}
273273

@@ -277,7 +277,7 @@ mod tests {
277277
let keyring = KeyringProvider::dummy([(url.host_str().unwrap(), "user", "password")]);
278278
assert_eq!(
279279
keyring
280-
.fetch(&DisplaySafeUrlRef::from(&url), Some("user"))
280+
.fetch(DisplaySafeUrlRef::from(&url), Some("user"))
281281
.await,
282282
Some(Credentials::basic(
283283
Some("user".to_string()),
@@ -287,7 +287,7 @@ mod tests {
287287
assert_eq!(
288288
keyring
289289
.fetch(
290-
&DisplaySafeUrlRef::from(&url.join("test").unwrap()),
290+
DisplaySafeUrlRef::from(&url.join("test").unwrap()),
291291
Some("user")
292292
)
293293
.await,
@@ -303,7 +303,7 @@ mod tests {
303303
let url = Url::parse("https://example.com").unwrap();
304304
let keyring = KeyringProvider::dummy([("other.com", "user", "password")]);
305305
let credentials = keyring
306-
.fetch(&DisplaySafeUrlRef::from(&url), Some("user"))
306+
.fetch(DisplaySafeUrlRef::from(&url), Some("user"))
307307
.await;
308308
assert_eq!(credentials, None);
309309
}
@@ -318,7 +318,7 @@ mod tests {
318318
assert_eq!(
319319
keyring
320320
.fetch(
321-
&DisplaySafeUrlRef::from(&url.join("foo").unwrap()),
321+
DisplaySafeUrlRef::from(&url.join("foo").unwrap()),
322322
Some("user")
323323
)
324324
.await,
@@ -329,7 +329,7 @@ mod tests {
329329
);
330330
assert_eq!(
331331
keyring
332-
.fetch(&DisplaySafeUrlRef::from(&url), Some("user"))
332+
.fetch(DisplaySafeUrlRef::from(&url), Some("user"))
333333
.await,
334334
Some(Credentials::basic(
335335
Some("user".to_string()),
@@ -339,7 +339,7 @@ mod tests {
339339
assert_eq!(
340340
keyring
341341
.fetch(
342-
&DisplaySafeUrlRef::from(&url.join("bar").unwrap()),
342+
DisplaySafeUrlRef::from(&url.join("bar").unwrap()),
343343
Some("user")
344344
)
345345
.await,
@@ -355,7 +355,7 @@ mod tests {
355355
let url = Url::parse("https://example.com").unwrap();
356356
let keyring = KeyringProvider::dummy([(url.host_str().unwrap(), "user", "password")]);
357357
let credentials = keyring
358-
.fetch(&DisplaySafeUrlRef::from(&url), Some("user"))
358+
.fetch(DisplaySafeUrlRef::from(&url), Some("user"))
359359
.await;
360360
assert_eq!(
361361
credentials,
@@ -370,7 +370,7 @@ mod tests {
370370
async fn fetch_url_no_username() {
371371
let url = Url::parse("https://example.com").unwrap();
372372
let keyring = KeyringProvider::dummy([(url.host_str().unwrap(), "user", "password")]);
373-
let credentials = keyring.fetch(&DisplaySafeUrlRef::from(&url), None).await;
373+
let credentials = keyring.fetch(DisplaySafeUrlRef::from(&url), None).await;
374374
assert_eq!(
375375
credentials,
376376
Some(Credentials::basic(
@@ -385,14 +385,14 @@ mod tests {
385385
let url = Url::parse("https://example.com").unwrap();
386386
let keyring = KeyringProvider::dummy([(url.host_str().unwrap(), "foo", "password")]);
387387
let credentials = keyring
388-
.fetch(&DisplaySafeUrlRef::from(&url), Some("bar"))
388+
.fetch(DisplaySafeUrlRef::from(&url), Some("bar"))
389389
.await;
390390
assert_eq!(credentials, None);
391391

392392
// Still fails if we have `foo` in the URL itself
393393
let url = Url::parse("https://[email protected]").unwrap();
394394
let credentials = keyring
395-
.fetch(&DisplaySafeUrlRef::from(&url), Some("bar"))
395+
.fetch(DisplaySafeUrlRef::from(&url), Some("bar"))
396396
.await;
397397
assert_eq!(credentials, None);
398398
}

crates/uv-auth/src/middleware.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -508,7 +508,7 @@ impl AuthMiddleware {
508508
debug!("Checking netrc for credentials for {url}");
509509
Credentials::from_netrc(
510510
netrc,
511-
&url,
511+
url,
512512
credentials
513513
.as_ref()
514514
.and_then(|credentials| credentials.username()),
@@ -529,17 +529,17 @@ impl AuthMiddleware {
529529
if let Some(username) = credentials.and_then(|credentials| credentials.username()) {
530530
if let Some(index_url) = maybe_index_url {
531531
debug!("Checking keyring for credentials for index URL {}@{}", username, index_url);
532-
keyring.fetch(&DisplaySafeUrlRef::from(index_url), Some(username)).await
532+
keyring.fetch(DisplaySafeUrlRef::from(index_url), Some(username)).await
533533
} else {
534534
debug!("Checking keyring for credentials for full URL {}@{}", username, url);
535-
keyring.fetch(&url, Some(username)).await
535+
keyring.fetch(url, Some(username)).await
536536
}
537537
} else if matches!(auth_policy, AuthPolicy::Always) {
538538
if let Some(index_url) = maybe_index_url {
539539
debug!(
540540
"Checking keyring for credentials for index URL {index_url} without username due to `authenticate = always`"
541541
);
542-
keyring.fetch(&DisplaySafeUrlRef::from(index_url), None).await
542+
keyring.fetch(DisplaySafeUrlRef::from(index_url), None).await
543543
} else {
544544
None
545545
}

0 commit comments

Comments
 (0)