Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

publish: Read categories metadata from embedded Cargo.toml file #7209

Merged
merged 1 commit into from
Sep 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions src/controllers/krate/publish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,15 @@ pub async fn publish(app: AppState, req: BytesRequest) -> AppResult<Json<GoodCra
validate_url(documentation.as_deref(), "documentation")?;
validate_url(repository.as_deref(), "repository")?;

let categories = package
.categories
.map(|it| it.as_local().unwrap())
.unwrap_or_default();

if categories.len() > 5 {
return Err(cargo_err("expected at most 5 categories per crate"));
}

// Create a transaction on the database, if there are no errors,
// commit the transactions to record a new or updated crate.
conn.transaction(|conn| {
Expand All @@ -172,11 +181,7 @@ pub async fn publish(app: AppState, req: BytesRequest) -> AppResult<Json<GoodCra
.iter()
.map(|s| s.as_str())
.collect::<Vec<_>>();
let categories = metadata
.categories
.iter()
.map(|s| s.as_str())
.collect::<Vec<_>>();
let categories = categories.iter().map(|s| s.as_str()).collect::<Vec<_>>();

// Persist the new crate, if it doesn't already exist
let persist = NewCrate {
Expand Down
1 change: 0 additions & 1 deletion src/tests/builders/publish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,6 @@ impl PublishBuilder {
.map(u::EncodableKeyword)
.collect(),
),
categories: u::EncodableCategoryList(self.categories.clone()),
};

let mut tarball_builder = TarballBuilder::new();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ expression: response.into_json()
{
"errors": [
{
"detail": "invalid upload request: invalid length 6, expected at most 5 categories per crate at line 1 column 155"
"detail": "expected at most 5 categories per crate"
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 138"
"detail": "invalid upload request: invalid length 6, expected at most 5 keywords per crate at line 1 column 139"
}
]
}
17 changes: 0 additions & 17 deletions src/views/krate_publish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ pub struct PublishMetadata {
pub readme_file: Option<String>,
#[serde(default)]
pub keywords: EncodableKeywordList,
#[serde(default)]
pub categories: EncodableCategoryList,
}

#[derive(Serialize, Deserialize, Clone, Debug)]
Expand Down Expand Up @@ -195,21 +193,6 @@ impl<'de> Deserialize<'de> for EncodableKeywordList {
}
}

#[derive(Serialize, Debug, Deref, Default)]
pub struct EncodableCategoryList(pub Vec<String>);

impl<'de> Deserialize<'de> for EncodableCategoryList {
fn deserialize<D: Deserializer<'de>>(d: D) -> Result<EncodableCategoryList, D::Error> {
let inner = <Vec<String> as Deserialize<'de>>::deserialize(d)?;
if inner.len() > 5 {
let expected = "at most 5 categories per crate";
Err(de::Error::invalid_length(inner.len(), &expected))
} else {
Ok(EncodableCategoryList(inner))
}
}
}

#[test]
fn feature_deserializes_for_valid_features() {
use serde_json as json;
Expand Down