From e48c35a06e6c58c196ec63a2271218bbe2c8575a Mon Sep 17 00:00:00 2001 From: Austin Liu Date: Mon, 2 Sep 2024 17:08:33 +0800 Subject: [PATCH 1/4] Normalize repo url Signed-off-by: Austin Liu --- src/upload.rs | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/src/upload.rs b/src/upload.rs index 5560a62e1..dbe4e3fb4 100644 --- a/src/upload.rs +++ b/src/upload.rs @@ -69,6 +69,10 @@ impl PublishOpt { self.non_interactive = true; } } + /// Helper function to normalize URLs by removing trailing slashes + fn normalize_url(url: &str) -> &str { + url.trim_end_matches('/') + } } /// Error type for different types of errors that can happen when uploading a @@ -316,9 +320,18 @@ fn complete_registry(opt: &PublishOpt) -> Result { let pypirc = load_pypirc(); let (registry_name, registry_url) = if let Some(repository_url) = opt.repository_url.as_deref() { - let name = match repository_url { - PublishOpt::DEFAULT_REPOSITORY_URL => Some("pypi"), - PublishOpt::TEST_REPOSITORY_URL => Some("testpypi"), + let normalized_url = PublishOpt::normalize_url(repository_url); + let name = match normalized_url { + normalized + if normalized == PublishOpt::normalize_url(PublishOpt::DEFAULT_REPOSITORY_URL) => + { + Some("pypi") + } + normalized + if normalized == PublishOpt::normalize_url(PublishOpt::TEST_REPOSITORY_URL) => + { + Some("testpypi") + } _ => None, }; (name, repository_url.to_string()) From fa5a47010737207e104efba04a516affaf739c68 Mon Sep 17 00:00:00 2001 From: Austin Liu Date: Mon, 2 Sep 2024 18:02:20 +0800 Subject: [PATCH 2/4] Remove redundant `normalize_url()` Signed-off-by: Austin Liu --- src/upload.rs | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/src/upload.rs b/src/upload.rs index dbe4e3fb4..267b6ca2c 100644 --- a/src/upload.rs +++ b/src/upload.rs @@ -59,8 +59,8 @@ pub struct PublishOpt { } impl PublishOpt { - const DEFAULT_REPOSITORY_URL: &'static str = "https://upload.pypi.org/legacy/"; - const TEST_REPOSITORY_URL: &'static str = "https://test.pypi.org/legacy/"; + const DEFAULT_REPOSITORY_URL: &'static str = "https://upload.pypi.org/legacy"; + const TEST_REPOSITORY_URL: &'static str = "https://test.pypi.org/legacy"; /// Set to non interactive mode if we're running on CI pub fn non_interactive_on_ci(&mut self) { @@ -322,16 +322,8 @@ fn complete_registry(opt: &PublishOpt) -> Result { { let normalized_url = PublishOpt::normalize_url(repository_url); let name = match normalized_url { - normalized - if normalized == PublishOpt::normalize_url(PublishOpt::DEFAULT_REPOSITORY_URL) => - { - Some("pypi") - } - normalized - if normalized == PublishOpt::normalize_url(PublishOpt::TEST_REPOSITORY_URL) => - { - Some("testpypi") - } + normalized if normalized == PublishOpt::DEFAULT_REPOSITORY_URL => Some("pypi"), + normalized if normalized == PublishOpt::TEST_REPOSITORY_URL => Some("testpypi"), _ => None, }; (name, repository_url.to_string()) From 48c04031a4721e8dd3ea00f9fa351e9ff6f7fa14 Mon Sep 17 00:00:00 2001 From: Austin Liu Date: Mon, 2 Sep 2024 18:09:09 +0800 Subject: [PATCH 3/4] Simplify pattern matching Signed-off-by: Austin Liu --- src/upload.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/upload.rs b/src/upload.rs index 267b6ca2c..8ee18e47a 100644 --- a/src/upload.rs +++ b/src/upload.rs @@ -322,8 +322,8 @@ fn complete_registry(opt: &PublishOpt) -> Result { { let normalized_url = PublishOpt::normalize_url(repository_url); let name = match normalized_url { - normalized if normalized == PublishOpt::DEFAULT_REPOSITORY_URL => Some("pypi"), - normalized if normalized == PublishOpt::TEST_REPOSITORY_URL => Some("testpypi"), + PublishOpt::DEFAULT_REPOSITORY_URL => Some("pypi"), + PublishOpt::TEST_REPOSITORY_URL => Some("testpypi"), _ => None, }; (name, repository_url.to_string()) From 3c8e77d10f41458b8fbfc89d8033be628f521a50 Mon Sep 17 00:00:00 2001 From: Austin Liu Date: Mon, 2 Sep 2024 18:12:17 +0800 Subject: [PATCH 4/4] Refactor for simplification Signed-off-by: Austin Liu --- src/upload.rs | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/upload.rs b/src/upload.rs index 8ee18e47a..b4d56b7ff 100644 --- a/src/upload.rs +++ b/src/upload.rs @@ -69,10 +69,6 @@ impl PublishOpt { self.non_interactive = true; } } - /// Helper function to normalize URLs by removing trailing slashes - fn normalize_url(url: &str) -> &str { - url.trim_end_matches('/') - } } /// Error type for different types of errors that can happen when uploading a @@ -320,8 +316,8 @@ fn complete_registry(opt: &PublishOpt) -> Result { let pypirc = load_pypirc(); let (registry_name, registry_url) = if let Some(repository_url) = opt.repository_url.as_deref() { - let normalized_url = PublishOpt::normalize_url(repository_url); - let name = match normalized_url { + // to normalize URLs by removing trailing slashes + let name = match repository_url.trim_end_matches('/') { PublishOpt::DEFAULT_REPOSITORY_URL => Some("pypi"), PublishOpt::TEST_REPOSITORY_URL => Some("testpypi"), _ => None,