diff --git a/Cargo.lock b/Cargo.lock index 21190b19d8f..e128768610e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -27,14 +27,16 @@ dependencies = [ "migrate 0.1.0", "oauth2 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "openssl 0.9.6 (registry+https://github.com/rust-lang/crates.io-index)", - "postgres 0.13.5 (registry+https://github.com/rust-lang/crates.io-index)", + "postgres 0.14.0 (registry+https://github.com/rust-lang/crates.io-index)", "r2d2 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)", "r2d2-diesel 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)", - "r2d2_postgres 0.11.1 (registry+https://github.com/rust-lang/crates.io-index)", + "r2d2_postgres 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)", "rand 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)", "rustc-serialize 0.3.22 (registry+https://github.com/rust-lang/crates.io-index)", "s3 0.0.1", "semver 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 0.9.11 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_derive 0.9.11 (registry+https://github.com/rust-lang/crates.io-index)", "serde_json 0.9.9 (registry+https://github.com/rust-lang/crates.io-index)", "time 0.1.36 (registry+https://github.com/rust-lang/crates.io-index)", "toml 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", @@ -502,7 +504,7 @@ dependencies = [ name = "migrate" version = "0.1.0" dependencies = [ - "postgres 0.13.5 (registry+https://github.com/rust-lang/crates.io-index)", + "postgres 0.13.6 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -558,16 +560,19 @@ dependencies = [ [[package]] name = "phf" -version = "0.7.20" +version = "0.7.21" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "phf_shared 0.7.20 (registry+https://github.com/rust-lang/crates.io-index)", + "phf_shared 0.7.21 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] name = "phf_shared" -version = "0.7.20" +version = "0.7.21" source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "siphasher 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", +] [[package]] name = "pkg-config" @@ -576,7 +581,20 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "postgres" -version = "0.13.5" +version = "0.13.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "bufstream 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", + "fallible-iterator 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", + "hex 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", + "log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", + "postgres-protocol 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", + "postgres-shared 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "postgres" +version = "0.14.0" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "bufstream 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", @@ -585,7 +603,7 @@ dependencies = [ "log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", "openssl 0.9.6 (registry+https://github.com/rust-lang/crates.io-index)", "postgres-protocol 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", - "postgres-shared 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", + "postgres-shared 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -602,14 +620,25 @@ dependencies = [ [[package]] name = "postgres-shared" -version = "0.1.0" +version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "fallible-iterator 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", "hex 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", - "phf 0.7.20 (registry+https://github.com/rust-lang/crates.io-index)", + "phf 0.7.21 (registry+https://github.com/rust-lang/crates.io-index)", "postgres-protocol 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", - "rustc-serialize 0.3.22 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "postgres-shared" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "fallible-iterator 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", + "hex 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", + "phf 0.7.21 (registry+https://github.com/rust-lang/crates.io-index)", + "postgres-protocol 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_json 0.9.9 (registry+https://github.com/rust-lang/crates.io-index)", "time 0.1.36 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -643,10 +672,10 @@ dependencies = [ [[package]] name = "r2d2_postgres" -version = "0.11.1" +version = "0.12.0" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "postgres 0.13.5 (registry+https://github.com/rust-lang/crates.io-index)", + "postgres 0.14.0 (registry+https://github.com/rust-lang/crates.io-index)", "r2d2 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -739,6 +768,24 @@ name = "serde" version = "0.9.11" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "serde_codegen_internals" +version = "0.14.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "syn 0.11.8 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "serde_derive" +version = "0.9.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "quote 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_codegen_internals 0.14.1 (registry+https://github.com/rust-lang/crates.io-index)", + "syn 0.11.8 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "serde_json" version = "0.9.9" @@ -750,6 +797,11 @@ dependencies = [ "serde 0.9.11 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "siphasher" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "syn" version = "0.11.8" @@ -953,17 +1005,19 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum openssl 0.9.6 (registry+https://github.com/rust-lang/crates.io-index)" = "0c00da69323449142e00a5410f0e022b39e8bbb7dc569cee8fc6af279279483c" "checksum openssl-probe 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "756d49c8424483a3df3b5d735112b4da22109ced9a8294f1f5cdf80fb3810919" "checksum openssl-sys 0.9.6 (registry+https://github.com/rust-lang/crates.io-index)" = "b1482f9a06f56c906007e17ea14d73d102210b5d27bc948bf5e175f493f3f7c3" -"checksum phf 0.7.20 (registry+https://github.com/rust-lang/crates.io-index)" = "0c6afb2057bb5f846a7b75703f90bc1cef4970c35209f712925db7768e999202" -"checksum phf_shared 0.7.20 (registry+https://github.com/rust-lang/crates.io-index)" = "286385a0e50d4147bce15b2c19f0cf84c395b0e061aaf840898a7bf664c2cfb7" +"checksum phf 0.7.21 (registry+https://github.com/rust-lang/crates.io-index)" = "cb325642290f28ee14d8c6201159949a872f220c62af6e110a56ea914fbe42fc" +"checksum phf_shared 0.7.21 (registry+https://github.com/rust-lang/crates.io-index)" = "07e24b0ca9643bdecd0632f2b3da6b1b89bbb0030e0b992afc1113b23a7bc2f2" "checksum pkg-config 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)" = "3a8b4c6b8165cd1a1cd4b9b120978131389f64bdaf456435caa41e630edba903" -"checksum postgres 0.13.5 (registry+https://github.com/rust-lang/crates.io-index)" = "585ca978431cddac0aa926246f18fe30a47401eabbe9bbda573dc60389c10ea1" +"checksum postgres 0.13.6 (registry+https://github.com/rust-lang/crates.io-index)" = "a1ed6564a52406dbc555719ba186f5309c790bc5fd44a656bb787d1c49fbc6ac" +"checksum postgres 0.14.0 (registry+https://github.com/rust-lang/crates.io-index)" = "aadd34ecc7aa81eae593f8299bb57e066277cda3d63713b2352245060713466d" "checksum postgres-protocol 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "283e27d237a5772ef00c9e3f97e632f9a565ff514761af3e88e129576af7077c" -"checksum postgres-shared 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "6f09b8819c2586032ed23bfbe95f6edfbebdc18bf9d0fe02c1f785f659958fbb" +"checksum postgres-shared 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "ba919eba78ade418bdee00d325caccb0946ab0ccfeaeb364c0fe157d2a81c382" +"checksum postgres-shared 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "857ef343933a7d81e365a334f505a07db16ab7be64d9d7cd82c78d2f486777e4" "checksum pq-sys 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "b8b9304824c6f013d5cb3db2e3e4fbb07379dec552afaae67b652a0f20adf0ec" "checksum quote 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)" = "7375cf7ad34a92e8fd18dd9c42f58b9a11def59ab48bec955bf359a788335592" "checksum r2d2 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)" = "4ecfed1b03be2e66624ec87cef173dad54253f25405bd3c918b321e4dda3ad32" "checksum r2d2-diesel 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)" = "1549332d11a91ac5b944a65fd59b9fe5ead091260556ae427b6919dadb08cace" -"checksum r2d2_postgres 0.11.1 (registry+https://github.com/rust-lang/crates.io-index)" = "d9ea3d4773725bf025184675ad9c7328609e8e4ddd27557dd0b67fff29bf2c2f" +"checksum r2d2_postgres 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)" = "00aae18ea6279c73dea01c5816fcd7ee1d0369e957f9445aebcbcb2927dd2b5c" "checksum rand 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)" = "022e0636ec2519ddae48154b028864bdce4eaf7d35226ab8e65c611be97b189d" "checksum redox_syscall 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)" = "8dd35cc9a8bdec562c757e3d43c1526b5c6d2653e23e2315065bc25556550753" "checksum regex 0.1.80 (registry+https://github.com/rust-lang/crates.io-index)" = "4fd4ace6a8cf7860714a2c2280d6c1f7e6a413486c13298bbc86fd3da019402f" @@ -975,7 +1029,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum semver 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "ae2ff60ecdb19c255841c066cbfa5f8c2a4ada1eb3ae47c77ab6667128da71f5" "checksum semver-parser 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)" = "d8fff3c9c5a54636ab95acd8c1349926e04cb1eb8cd70b5adced8a1d1f703a67" "checksum serde 0.9.11 (registry+https://github.com/rust-lang/crates.io-index)" = "a702319c807c016e51f672e5c77d6f0b46afddd744b5e437d6b8436b888b458f" +"checksum serde_codegen_internals 0.14.1 (registry+https://github.com/rust-lang/crates.io-index)" = "4d52006899f910528a10631e5b727973fe668f3228109d1707ccf5bad5490b6e" +"checksum serde_derive 0.9.11 (registry+https://github.com/rust-lang/crates.io-index)" = "f15ea24bd037b2d64646b4d934fa99c649be66e3f7b29fb595a5543b212b1452" "checksum serde_json 0.9.9 (registry+https://github.com/rust-lang/crates.io-index)" = "dbc45439552eb8fb86907a2c41c1fd0ef97458efb87ff7f878db466eb581824e" +"checksum siphasher 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "2ffc669b726f2bc9a3bcff66e5e23b56ba6bf70e22a34c3d7b6d0b3450b65b84" "checksum syn 0.11.8 (registry+https://github.com/rust-lang/crates.io-index)" = "37c279fb816210c9bb28b2c292664581e7b87b4561e86b94df462664d8620bb8" "checksum synom 0.11.3 (registry+https://github.com/rust-lang/crates.io-index)" = "a393066ed9010ebaed60b9eafa373d4b1baac186dd7e008555b0f702b51945b6" "checksum thread-id 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a9539db560102d1cef46b8b78ce737ff0bb64e7e18d35b2a5688f7d097d0ff03" diff --git a/Cargo.toml b/Cargo.toml index f6fd61fd40b..0b17a909fb3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -23,9 +23,9 @@ git2 = "0.6.4" flate2 = "0.2" semver = "0.5" url = "1.2.1" -postgres = { version = "0.13", features = ["with-time", "with-openssl", "with-rustc-serialize"] } +postgres = { version = "0.14.0", features = ["with-time", "with-openssl", "with-serde_json"] } r2d2 = "0.7.0" -r2d2_postgres = "0.11" +r2d2_postgres = "0.12.0" openssl = "0.9" curl = "0.4" oauth2 = "0.3" @@ -39,7 +39,9 @@ diesel = { version = "0.11.0", features = ["postgres", "serde_json", "deprecated diesel_codegen = { version = "0.11.0", features = ["postgres"] } r2d2-diesel = "0.11.0" diesel_full_text_search = "0.11.0" -serde_json = "0.9.0" +serde_json = "0.9.9" +serde_derive = "0.9.11" +serde = "0.9.11" conduit = "0.8" conduit-conditional-get = "0.8" diff --git a/src/badge.rs b/src/badge.rs index c5c383da80e..c68afa5c6c2 100644 --- a/src/badge.rs +++ b/src/badge.rs @@ -7,25 +7,27 @@ use diesel::pg::Pg; use diesel::prelude::*; use pg::GenericConnection; use pg::rows::Row; -use rustc_serialize::Decodable; -use rustc_serialize::json::{Json, Decoder}; use serde_json; use std::collections::HashMap; -#[derive(Debug, PartialEq, Clone)] +#[derive(Debug, PartialEq, Clone, Deserialize, Serialize)] +#[serde(tag = "badge_type", content = "attributes")] pub enum Badge { + #[serde(rename = "travis-ci")] TravisCi { repository: String, branch: Option, }, + #[serde(rename = "appveyor")] Appveyor { repository: String, branch: Option, service: Option, }, + #[serde(rename = "gitlab")] GitLab { repository: String, branch: Option, }, } -#[derive(RustcEncodable, RustcDecodable, PartialEq, Debug)] +#[derive(RustcEncodable, RustcDecodable, PartialEq, Debug, Deserialize)] pub struct EncodableBadge { pub badge_type: String, pub attributes: HashMap, @@ -35,21 +37,18 @@ impl Queryable for Badge { type Row = (i32, String, serde_json::Value); fn build((_, badge_type, attributes): Self::Row) -> Self { - let attributes = serde_json::from_value::>(attributes) - .expect("attributes was not a map in the database"); - Self::from_attributes(&badge_type, &attributes) - .expect("invalid badge in the database") + let json = json!({"badge_type": badge_type, "attributes": attributes}); + serde_json::from_value(json) + .expect("Invalid CI badge in the database") } } impl Model for Badge { fn from_row(row: &Row) -> Badge { - let attributes: Json = row.get("attributes"); let badge_type: String = row.get("badge_type"); - let mut decoder = Decoder::new(attributes); - let attributes = HashMap::::decode(&mut decoder) - .expect("Attributes was not a json object"); - Self::from_attributes(&badge_type, &attributes) + let attributes: serde_json::Value = row.get("attributes"); + let json = json!({"badge_type": badge_type, "attributes": attributes}); + serde_json::from_value(json) .expect("Invalid CI badge in the database") } fn table_name(_: Option) -> &'static str { "badges" } @@ -57,10 +56,7 @@ impl Model for Badge { impl Badge { pub fn encodable(self) -> EncodableBadge { - EncodableBadge { - badge_type: self.badge_type().to_string(), - attributes: self.attributes(), - } + serde_json::from_value(serde_json::to_value(self).unwrap()).unwrap() } pub fn badge_type(&self) -> &'static str { @@ -71,101 +67,6 @@ impl Badge { } } - pub fn json_attributes(self) -> Json { - Json::Object(self.attributes().into_iter().map(|(k, v)| { - (k, Json::String(v)) - }).collect()) - } - - fn attributes(self) -> HashMap { - let mut attributes = HashMap::new(); - - match self { - Badge::TravisCi { branch, repository } => { - attributes.insert(String::from("repository"), repository); - if let Some(branch) = branch { - attributes.insert( - String::from("branch"), - branch - ); - } - }, - Badge::Appveyor { service, branch, repository } => { - attributes.insert(String::from("repository"), repository); - if let Some(branch) = branch { - attributes.insert( - String::from("branch"), - branch - ); - } - if let Some(service) = service { - attributes.insert( - String::from("service"), - service - ); - } - }, - Badge::GitLab { branch, repository } => { - attributes.insert(String::from("repository"), repository); - if let Some(branch) = branch { - attributes.insert( - String::from("branch"), - branch - ); - } - }, - } - - attributes - } - - fn from_attributes(badge_type: &str, - attributes: &HashMap) - -> Result { - match badge_type { - "travis-ci" => { - match attributes.get("repository") { - Some(repository) => { - Ok(Badge::TravisCi { - repository: repository.to_string(), - branch: attributes.get("branch") - .map(String::to_string), - }) - }, - None => Err(badge_type.to_string()), - } - }, - "appveyor" => { - match attributes.get("repository") { - Some(repository) => { - Ok(Badge::Appveyor { - repository: repository.to_string(), - branch: attributes.get("branch") - .map(String::to_string), - service: attributes.get("service") - .map(String::to_string), - - }) - }, - None => Err(badge_type.to_string()), - } - }, - "gitlab" => { - match attributes.get("repository") { - Some(repository) => { - Ok(Badge::GitLab { - repository: repository.to_string(), - branch: attributes.get("branch") - .map(String::to_string), - }) - }, - None => Err(badge_type.to_string()), - } - }, - _ => Err(badge_type.to_string()), - } - } - pub fn update_crate(conn: &GenericConnection, krate: &Crate, badges: HashMap>) @@ -173,10 +74,11 @@ impl Badge { let mut invalid_badges = vec![]; - let badges: Vec<_> = badges.iter().filter_map(|(k, v)| { - Badge::from_attributes(k, v).map_err(|invalid_badge| { - invalid_badges.push(invalid_badge) - }).ok() + let badges: Vec = badges.into_iter().filter_map(|(k, v)| { + let json = json!({"badge_type": k, "attributes": v}); + serde_json::from_value(json) + .map_err(|_| invalid_badges.push(k)) + .ok() }).collect(); conn.execute("\ @@ -186,12 +88,13 @@ impl Badge { )?; for badge in badges { + let json = serde_json::to_value(badge)?; conn.execute("\ INSERT INTO badges (crate_id, badge_type, attributes) \ VALUES ($1, $2, $3) \ ON CONFLICT (crate_id, badge_type) DO UPDATE \ SET attributes = EXCLUDED.attributes;", - &[&krate.id, &badge.badge_type(), &badge.json_attributes()] + &[&krate.id, &json["badge_type"].as_str(), &json["attributes"]] )?; } Ok(invalid_badges) diff --git a/src/lib.rs b/src/lib.rs index 440ab3cbb3f..442f4e4d7ea 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,11 +4,11 @@ //! implemented in the [keyword](keyword/index.html), [krate](krate/index.html), //! [user](user/index.html) and [version](version/index.html) modules. -#[macro_use] extern crate log; #[macro_use] extern crate diesel; #[macro_use] extern crate diesel_codegen; -extern crate postgres as pg; -extern crate rustc_serialize; +#[macro_use] extern crate log; +#[macro_use] extern crate serde_json; +#[macro_use] extern crate serde_derive; extern crate curl; extern crate diesel_full_text_search; extern crate dotenv; @@ -17,16 +17,17 @@ extern crate git2; extern crate license_exprs; extern crate oauth2; extern crate openssl; +extern crate postgres as pg; extern crate r2d2; extern crate r2d2_diesel; extern crate r2d2_postgres; extern crate rand; +extern crate rustc_serialize; extern crate s3; extern crate semver; -extern crate serde_json; extern crate time; -extern crate url; extern crate toml; +extern crate url; extern crate conduit; extern crate conduit_conditional_get;