Repository: astral-sh/uv
File: crates/uv/src/commands/publish.rs (Rust)

Code before:
        let (publish_url, credentials) = get_credentials(example_url.clone(), None, None)
            .await
            .unwrap();
        assert_eq!(publish_url, example_url);
        assert_eq!(credentials.username(), None);
        assert_eq!(credentials.password(), None);

        let (publish_url, credentials) = get_credentials(example_url_username.clone(), None, None)
            .await
            .unwrap();
        assert_eq!(publish_url, example_url);
        assert_eq!(credentials.username(), Some("ferris"));
        assert_eq!(credentials.password(), None);

        let (publish_url, credentials) =
            get_credentials(example_url_username_password.clone(), None, None)
                .await
                .unwrap();
        assert_eq!(publish_url, example_url);
        assert_eq!(credentials.username(), Some("ferris"));
        assert_eq!(credentials.password(), Some("f3rr1s"));

        // Ok: The username is the same between CLI/env vars and URL
        let (publish_url, credentials) = get_credentials(
            example_url_username_password.clone(),
            Some("ferris".to_string()),
            None,
        )
        .await
        .unwrap();
        assert_eq!(publish_url, example_url);
        assert_eq!(credentials.username(), Some("ferris"));
        assert_eq!(credentials.password(), Some("f3rr1s"));

        // Err: There are two different usernames between CLI/env vars and URL
        let err = get_credentials(
            example_url_username_password.clone(),
            Some("packaging-platypus".to_string()),
            None,
        )
        .await
        .unwrap_err();
        assert_snapshot!(
            err.to_string(),
            @"The username can't be set both in the publish URL and in the CLI"
        );

        // Ok: The username and password are the same between CLI/env vars and URL
        let (publish_url, credentials) = get_credentials(
            example_url_username_password.clone(),
            Some("ferris".to_string()),

Review comment (suggestion):
You could use `.filter` here if you prefer, like:

```suggestion
    let workspace = segments.next().filter(|segment| !segment.is_empty())?;
```

(I really don't have a preference)

Code after:
    let password =
        uv_console::password(password_prompt, &term).context("Failed to read password")?;
    Ok((Some(username), Some(password)))
}

/// Construct a Simple Index URL from a publish URL, if possible.
///
/// Matches against a publish URL of the form `/v1/upload/{workspace}/{registry}` and returns
/// `/simple/{workspace}/{registry}`.
fn infer_check_url(publish_url: &DisplaySafeUrl) -> Option<IndexUrl> {
    let mut segments = publish_url.path_segments()?;

    let v1 = segments.next()?;
    if v1 != "v1" {
        return None;
    }

    let upload = segments.next()?;
    if upload != "upload" {
        return None;
    }

    let workspace = segments.next()?;
    if workspace.is_empty() {
        return None;
    }

    let registry = segments.next()?;
    if registry.is_empty() {
        return None;
    }

    // Skip any empty segments (trailing slash handling)
    for remaining in segments {
        if !remaining.is_empty() {
            return None;
        }
    }

    // Reconstruct the URL with `/simple/{workspace}/{registry}`.
    let mut check_url = publish_url.clone();
    {
        let mut segments = check_url.path_segments_mut().ok()?;
        segments.clear();
        segments.push("simple").push(workspace).push(registry);
    }
    Some(IndexUrl::from(VerbatimUrl::from(check_url)))
}

#[cfg(test)]
mod tests {