From bc5deeb2e3d10538c03a22f9e1ad670a4ce0dc4e Mon Sep 17 00:00:00 2001 From: Tobias Bieniek Date: Fri, 29 Sep 2023 16:13:42 +0200 Subject: [PATCH] publish: Read `keywords` metadata from embedded `Cargo.toml` file ... instead of the metadata JSON blob --- src/controllers/krate/publish.rs | 25 ++++++++++--- src/tests/builders/publish.rs | 7 ---- ...te__publish__keywords__bad_keywords-2.snap | 2 +- ...te__publish__keywords__bad_keywords-3.snap | 2 +- ...rate__publish__keywords__bad_keywords.snap | 2 +- ..._publish__keywords__too_many_keywords.snap | 2 +- src/views/krate_publish.rs | 36 ------------------- 7 files changed, 24 insertions(+), 52 deletions(-) diff --git a/src/controllers/krate/publish.rs b/src/controllers/krate/publish.rs index 41e79d6b851..05e3887d747 100644 --- a/src/controllers/krate/publish.rs +++ b/src/controllers/krate/publish.rs @@ -157,6 +157,25 @@ pub async fn publish(app: AppState, req: BytesRequest) -> AppResult 5 { + return Err(cargo_err("expected at most 5 keywords per crate")); + } + + for keyword in keywords.iter() { + if keyword.len() > 20 { + return Err(cargo_err(&format!( + "\"{keyword}\" is an invalid keyword (keywords must have less than 20 characters)" + ))); + } else if !Keyword::valid_name(keyword) { + return Err(cargo_err(&format!("\"{keyword}\" is an invalid keyword"))); + } + } + let categories = package .categories .map(|it| it.as_local().unwrap()) @@ -176,11 +195,7 @@ pub async fn publish(app: AppState, req: BytesRequest) -> AppResult>(); + let keywords = keywords.iter().map(|s| s.as_str()).collect::>(); let categories = categories.iter().map(|s| s.as_str()).collect::>(); // Persist the new crate, if it doesn't already exist diff --git a/src/tests/builders/publish.rs b/src/tests/builders/publish.rs index 2e1dd6408f2..bf9663a5f3f 100644 --- a/src/tests/builders/publish.rs +++ b/src/tests/builders/publish.rs @@ -144,13 +144,6 @@ impl PublishBuilder { deps: self.deps.clone(), readme: self.readme, readme_file: None, - keywords: u::EncodableKeywordList( - self.keywords - .clone() - .into_iter() - .map(u::EncodableKeyword) - .collect(), - ), }; let mut tarball_builder = TarballBuilder::new(); diff --git a/src/tests/krate/publish/snapshots/all__krate__publish__keywords__bad_keywords-2.snap b/src/tests/krate/publish/snapshots/all__krate__publish__keywords__bad_keywords-2.snap index 3a5f4a5cd87..31a5e5376b7 100644 --- a/src/tests/krate/publish/snapshots/all__krate__publish__keywords__bad_keywords-2.snap +++ b/src/tests/krate/publish/snapshots/all__krate__publish__keywords__bad_keywords-2.snap @@ -5,7 +5,7 @@ expression: response.into_json() { "errors": [ { - "detail": "invalid upload request: invalid value: string \"?@?%\", expected a valid keyword specifier at line 1 column 113" + "detail": "\"?@?%\" is an invalid keyword" } ] } diff --git a/src/tests/krate/publish/snapshots/all__krate__publish__keywords__bad_keywords-3.snap b/src/tests/krate/publish/snapshots/all__krate__publish__keywords__bad_keywords-3.snap index 789e18bacf7..d8f0da91108 100644 --- a/src/tests/krate/publish/snapshots/all__krate__publish__keywords__bad_keywords-3.snap +++ b/src/tests/krate/publish/snapshots/all__krate__publish__keywords__bad_keywords-3.snap @@ -5,7 +5,7 @@ expression: response.into_json() { "errors": [ { - "detail": "invalid upload request: invalid value: string \"áccênts\", expected a valid keyword specifier at line 1 column 118" + "detail": "\"áccênts\" is an invalid keyword" } ] } diff --git a/src/tests/krate/publish/snapshots/all__krate__publish__keywords__bad_keywords.snap b/src/tests/krate/publish/snapshots/all__krate__publish__keywords__bad_keywords.snap index 1d4484bf966..39a2408a37c 100644 --- a/src/tests/krate/publish/snapshots/all__krate__publish__keywords__bad_keywords.snap +++ b/src/tests/krate/publish/snapshots/all__krate__publish__keywords__bad_keywords.snap @@ -5,7 +5,7 @@ expression: response.into_json() { "errors": [ { - "detail": "invalid upload request: invalid length 29, expected a keyword with less than 20 characters at line 1 column 138" + "detail": "\"super-long-keyword-name-oh-no\" is an invalid keyword (keywords must have less than 20 characters)" } ] } diff --git a/src/tests/krate/publish/snapshots/all__krate__publish__keywords__too_many_keywords.snap b/src/tests/krate/publish/snapshots/all__krate__publish__keywords__too_many_keywords.snap index 5e183db775b..c06d34e03bf 100644 --- a/src/tests/krate/publish/snapshots/all__krate__publish__keywords__too_many_keywords.snap +++ b/src/tests/krate/publish/snapshots/all__krate__publish__keywords__too_many_keywords.snap @@ -5,7 +5,7 @@ expression: response.into_json() { "errors": [ { - "detail": "invalid upload request: invalid length 6, expected at most 5 keywords per crate at line 1 column 139" + "detail": "expected at most 5 keywords per crate" } ] } diff --git a/src/views/krate_publish.rs b/src/views/krate_publish.rs index eb0cf0031eb..76f765b7f2b 100644 --- a/src/views/krate_publish.rs +++ b/src/views/krate_publish.rs @@ -13,7 +13,6 @@ use crate::models::krate::MAX_NAME_LENGTH; use crate::models::Crate; use crate::models::DependencyKind; -use crate::models::Keyword as CrateKeyword; #[derive(Deserialize, Serialize, Debug)] pub struct PublishMetadata { @@ -23,8 +22,6 @@ pub struct PublishMetadata { pub features: BTreeMap>, pub readme: Option, pub readme_file: Option, - #[serde(default)] - pub keywords: EncodableKeywordList, } #[derive(Serialize, Deserialize, Clone, Debug)] @@ -87,25 +84,6 @@ impl<'de> Deserialize<'de> for EncodableDependencyName { } } -#[derive(Serialize, Debug, Deref)] -pub struct EncodableKeyword(pub String); - -impl<'de> Deserialize<'de> for EncodableKeyword { - fn deserialize>(d: D) -> Result { - let s = String::deserialize(d)?; - if s.len() > 20 { - let expected = "a keyword with less than 20 characters"; - Err(de::Error::invalid_length(s.len(), &expected)) - } else if !CrateKeyword::valid_name(&s) { - let value = de::Unexpected::Str(&s); - let expected = "a valid keyword specifier"; - Err(de::Error::invalid_value(value, &expected)) - } else { - Ok(EncodableKeyword(s)) - } - } -} - #[derive(PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Clone, Debug, Deref)] pub struct EncodableFeatureName(pub String); @@ -179,20 +157,6 @@ impl<'de> Deserialize<'de> for EncodableCrateVersionReq { } } -#[derive(Serialize, Debug, Deref, Default)] -pub struct EncodableKeywordList(pub Vec); - -impl<'de> Deserialize<'de> for EncodableKeywordList { - fn deserialize>(d: D) -> Result { - let inner = as Deserialize<'de>>::deserialize(d)?; - if inner.len() > 5 { - let expected = "at most 5 keywords per crate"; - return Err(de::Error::invalid_length(inner.len(), &expected)); - } - Ok(EncodableKeywordList(inner)) - } -} - #[test] fn feature_deserializes_for_valid_features() { use serde_json as json;