Skip to content

Commit 408545c

Browse files
authored
publish: Read categories metadata from embedded Cargo.toml file (#7209)
... instead of the metadata JSON blob
1 parent 36b3f92 commit 408545c

File tree

5 files changed

+12
-25
lines changed

5 files changed

+12
-25
lines changed

src/controllers/krate/publish.rs

+10-5
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,15 @@ pub async fn publish(app: AppState, req: BytesRequest) -> AppResult<Json<GoodCra
157157
validate_url(documentation.as_deref(), "documentation")?;
158158
validate_url(repository.as_deref(), "repository")?;
159159

160+
let categories = package
161+
.categories
162+
.map(|it| it.as_local().unwrap())
163+
.unwrap_or_default();
164+
165+
if categories.len() > 5 {
166+
return Err(cargo_err("expected at most 5 categories per crate"));
167+
}
168+
160169
// Create a transaction on the database, if there are no errors,
161170
// commit the transactions to record a new or updated crate.
162171
conn.transaction(|conn| {
@@ -172,11 +181,7 @@ pub async fn publish(app: AppState, req: BytesRequest) -> AppResult<Json<GoodCra
172181
.iter()
173182
.map(|s| s.as_str())
174183
.collect::<Vec<_>>();
175-
let categories = metadata
176-
.categories
177-
.iter()
178-
.map(|s| s.as_str())
179-
.collect::<Vec<_>>();
184+
let categories = categories.iter().map(|s| s.as_str()).collect::<Vec<_>>();
180185

181186
// Persist the new crate, if it doesn't already exist
182187
let persist = NewCrate {

src/tests/builders/publish.rs

-1
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,6 @@ impl PublishBuilder {
151151
.map(u::EncodableKeyword)
152152
.collect(),
153153
),
154-
categories: u::EncodableCategoryList(self.categories.clone()),
155154
};
156155

157156
let mut tarball_builder = TarballBuilder::new();

src/tests/krate/publish/snapshots/all__krate__publish__categories__too_many_categories.snap

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ expression: response.into_json()
55
{
66
"errors": [
77
{
8-
"detail": "invalid upload request: invalid length 6, expected at most 5 categories per crate at line 1 column 155"
8+
"detail": "expected at most 5 categories per crate"
99
}
1010
]
1111
}

src/tests/krate/publish/snapshots/all__krate__publish__keywords__too_many_keywords.snap

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ expression: response.into_json()
55
{
66
"errors": [
77
{
8-
"detail": "invalid upload request: invalid length 6, expected at most 5 keywords per crate at line 1 column 138"
8+
"detail": "invalid upload request: invalid length 6, expected at most 5 keywords per crate at line 1 column 139"
99
}
1010
]
1111
}

src/views/krate_publish.rs

-17
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@ pub struct PublishMetadata {
2525
pub readme_file: Option<String>,
2626
#[serde(default)]
2727
pub keywords: EncodableKeywordList,
28-
#[serde(default)]
29-
pub categories: EncodableCategoryList,
3028
}
3129

3230
#[derive(Serialize, Deserialize, Clone, Debug)]
@@ -195,21 +193,6 @@ impl<'de> Deserialize<'de> for EncodableKeywordList {
195193
}
196194
}
197195

198-
#[derive(Serialize, Debug, Deref, Default)]
199-
pub struct EncodableCategoryList(pub Vec<String>);
200-
201-
impl<'de> Deserialize<'de> for EncodableCategoryList {
202-
fn deserialize<D: Deserializer<'de>>(d: D) -> Result<EncodableCategoryList, D::Error> {
203-
let inner = <Vec<String> as Deserialize<'de>>::deserialize(d)?;
204-
if inner.len() > 5 {
205-
let expected = "at most 5 categories per crate";
206-
Err(de::Error::invalid_length(inner.len(), &expected))
207-
} else {
208-
Ok(EncodableCategoryList(inner))
209-
}
210-
}
211-
}
212-
213196
#[test]
214197
fn feature_deserializes_for_valid_features() {
215198
use serde_json as json;

0 commit comments

Comments
 (0)