Skip to content

Commit

Permalink
Auto merge of #3301 - integer32llc:categories, r=alexcrichton
Browse files Browse the repository at this point in the history
Upload categories specified in the manifest

This adds support for uploading categories to crates.io, if they are specified in the manifest.

This goes with rust-lang/crates.io#473. It should be fine to merge this PR either before or after that one; crates.io master doesn't care if the categories are in the metadata or not. With that PR, I was able to use this patch with cargo to add categories to a crate!
  • Loading branch information
bors committed Jan 17, 2017
2 parents a43403b + f697b8c commit 7ba2012
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 11 deletions.
1 change: 1 addition & 0 deletions src/cargo/core/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ pub struct VirtualManifest {
pub struct ManifestMetadata {
pub authors: Vec<String>,
pub keywords: Vec<String>,
pub categories: Vec<String>,
pub license: Option<String>,
pub license_file: Option<String>,
pub description: Option<String>, // not markdown
Expand Down
23 changes: 19 additions & 4 deletions src/cargo/ops/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ fn transmit(config: &Config,
let ManifestMetadata {
ref authors, ref description, ref homepage, ref documentation,
ref keywords, ref readme, ref repository, ref license, ref license_file,
ref categories,
} = *manifest.metadata();
let readme = match *readme {
Some(ref readme) => Some(paths::read(&pkg.root().join(readme))?),
Expand All @@ -133,7 +134,7 @@ fn transmit(config: &Config,
return Ok(());
}

registry.publish(&NewCrate {
let publish = registry.publish(&NewCrate {
name: pkg.name().to_string(),
vers: pkg.version().to_string(),
deps: deps,
Expand All @@ -143,13 +144,27 @@ fn transmit(config: &Config,
homepage: homepage.clone(),
documentation: documentation.clone(),
keywords: keywords.clone(),
categories: categories.clone(),
readme: readme,
repository: repository.clone(),
license: license.clone(),
license_file: license_file.clone(),
}, tarball).map_err(|e| {
human(e.to_string())
})
}, tarball);

match publish {
Ok(warnings) => {
if !warnings.invalid_categories.is_empty() {
let msg = format!("\
the following are not valid category slugs and were \
ignored: {}. Please see https://crates.io/category_slugs \
for the list of all category slugs. \
", warnings.invalid_categories.join(", "));
config.shell().warn(&msg)?;
}
Ok(())
},
Err(e) => Err(human(e.to_string())),
}
}

pub fn registry_configuration(config: &Config) -> CargoResult<RegistryConfig> {
Expand Down
2 changes: 2 additions & 0 deletions src/cargo/util/toml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,7 @@ pub struct TomlProject {
documentation: Option<String>,
readme: Option<String>,
keywords: Option<Vec<String>>,
categories: Option<Vec<String>>,
license: Option<String>,
license_file: Option<String>,
repository: Option<String>,
Expand Down Expand Up @@ -654,6 +655,7 @@ impl TomlManifest {
license_file: project.license_file.clone(),
repository: project.repository.clone(),
keywords: project.keywords.clone().unwrap_or(Vec::new()),
categories: project.categories.clone().unwrap_or(Vec::new()),
};

let workspace_config = match (self.workspace.as_ref(),
Expand Down
37 changes: 32 additions & 5 deletions src/crates-io/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use std::io::{self, Cursor};
use std::result;

use curl::easy::{Easy, List};
use rustc_serialize::json;
use rustc_serialize::json::{self, Json};

use url::percent_encoding::{percent_encode, QUERY_ENCODE_SET};

Expand Down Expand Up @@ -39,6 +39,7 @@ pub enum Error {
NotFound,
JsonEncodeError(json::EncoderError),
JsonDecodeError(json::DecoderError),
JsonParseError(json::ParserError),
}

impl From<json::EncoderError> for Error {
Expand All @@ -53,6 +54,12 @@ impl From<json::DecoderError> for Error {
}
}

impl From<json::ParserError> for Error {
fn from(err: json::ParserError) -> Error {
Error::JsonParseError(err)
}
}

impl From<curl::Error> for Error {
fn from(err: curl::Error) -> Error {
Error::Curl(err)
Expand All @@ -78,6 +85,7 @@ pub struct NewCrate {
pub homepage: Option<String>,
pub readme: Option<String>,
pub keywords: Vec<String>,
pub categories: Vec<String>,
pub license: Option<String>,
pub license_file: Option<String>,
pub repository: Option<String>,
Expand All @@ -103,14 +111,17 @@ pub struct User {
pub name: Option<String>,
}

pub struct Warnings {
pub invalid_categories: Vec<String>,
}

#[derive(RustcDecodable)] struct R { ok: bool }
#[derive(RustcDecodable)] struct ApiErrorList { errors: Vec<ApiError> }
#[derive(RustcDecodable)] struct ApiError { detail: String }
#[derive(RustcEncodable)] struct OwnersReq<'a> { users: &'a [&'a str] }
#[derive(RustcDecodable)] struct Users { users: Vec<User> }
#[derive(RustcDecodable)] struct TotalCrates { total: u32 }
#[derive(RustcDecodable)] struct Crates { crates: Vec<Crate>, meta: TotalCrates }

impl Registry {
pub fn new(host: String, token: Option<String>) -> Registry {
Registry::new_handle(host, token, Easy::new())
Expand Down Expand Up @@ -147,7 +158,8 @@ impl Registry {
Ok(json::decode::<Users>(&body)?.users)
}

pub fn publish(&mut self, krate: &NewCrate, tarball: &File) -> Result<()> {
pub fn publish(&mut self, krate: &NewCrate, tarball: &File)
-> Result<Warnings> {
let json = json::encode(krate)?;
// Prepare the body. The format of the upload request is:
//
Expand Down Expand Up @@ -190,10 +202,24 @@ impl Registry {
headers.append(&format!("Authorization: {}", token))?;
self.handle.http_headers(headers)?;

let _body = handle(&mut self.handle, &mut |buf| {
let body = handle(&mut self.handle, &mut |buf| {
body.read(buf).unwrap_or(0)
})?;
Ok(())
// Can't derive RustcDecodable because JSON has a key named "crate" :(
let response = if body.len() > 0 {
Json::from_str(&body)?
} else {
Json::from_str("{}")?
};
let invalid_categories: Vec<String> =
response
.find_path(&["warnings", "invalid_categories"])
.and_then(Json::as_array)
.map(|x| {
x.iter().flat_map(Json::as_string).map(Into::into).collect()
})
.unwrap_or_else(Vec::new);
Ok(Warnings { invalid_categories: invalid_categories })
}

pub fn search(&mut self, query: &str, limit: u8) -> Result<(Vec<Crate>, u32)> {
Expand Down Expand Up @@ -328,6 +354,7 @@ impl fmt::Display for Error {
Error::NotFound => write!(f, "cannot find crate"),
Error::JsonEncodeError(ref e) => write!(f, "json encode error: {}", e),
Error::JsonDecodeError(ref e) => write!(f, "json decode error: {}", e),
Error::JsonParseError(ref e) => write!(f, "json parse error: {}", e),
}
}
}
10 changes: 8 additions & 2 deletions src/doc/manifest.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,16 @@ repository = "..."
# contents of this file are stored and indexed in the registry.
readme = "..."

# This is a small list of keywords used to categorize and search for this
# package.
# This is a list of up to five keywords that describe this crate. Keywords
# are searchable on crates.io, and you may choose any words that would
# help someone find this crate.
keywords = ["...", "..."]

# This is a list of up to five categories where this crate would fit.
# Categories are a fixed list available at crates.io/categories, and
# they must match exactly.
categories = ["...", "..."]

# This is a string description of the license for this package. Currently
# crates.io will validate the license provided against a whitelist of known
# license identifiers from http://spdx.org/licenses/. Multiple licenses can be
Expand Down

0 comments on commit 7ba2012

Please sign in to comment.