Last active
July 11, 2017 19:27
-
-
Save marti1125/74fc9870f4f00cc88c529966f413adc8 to your computer and use it in GitHub Desktop.
enum MaintenanceValue
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use krate::Crate; | |
use schema::badges; | |
use util::CargoResult; | |
use diesel::pg::{Pg, PgConnection}; | |
use diesel::prelude::*; | |
use serde_json; | |
use std::collections::HashMap; | |
#[derive(Debug, PartialEq, Clone, Deserialize, Serialize)] | |
#[serde(tag = "badge_type", content = "attributes")] | |
pub enum Badge { | |
#[serde(rename = "travis-ci")] | |
TravisCi { | |
repository: String, branch: Option<String>, | |
}, | |
#[serde(rename = "appveyor")] | |
Appveyor { | |
repository: String, branch: Option<String>, service: Option<String>, | |
}, | |
#[serde(rename = "gitlab")] | |
GitLab { | |
repository: String, branch: Option<String>, | |
}, | |
#[serde(rename = "maintenance")] | |
Maintenance { | |
value: MaintenanceValue | |
}, | |
} | |
#[derive(Debug, PartialEq, Clone, Deserialize, Serialize)] | |
#[serde(tag = "badge_type", content = "attributes")] | |
pub enum MaintenanceValue { | |
#[serde(rename = "actively-developed")] | |
ActivelyDeveloped, | |
#[serde(rename = "passively-maintained")] | |
PassivelyMaintained, | |
#[serde(rename = "as-is")] | |
AsIs, | |
#[serde(rename = "none")] | |
None, | |
#[serde(rename = "experimental")] | |
Experimental, | |
#[serde(rename = "looking-for-maintainer")] | |
LookingForMaintainer, | |
#[serde(rename = "deprecated")] | |
Deprecated, | |
} | |
#[derive(RustcEncodable, RustcDecodable, PartialEq, Debug, Deserialize)] | |
pub struct EncodableBadge { | |
pub badge_type: String, | |
pub attributes: HashMap<String, Option<String>>, | |
} | |
impl Queryable<badges::SqlType, Pg> for Badge { | |
type Row = (i32, String, serde_json::Value); | |
fn build((_, badge_type, attributes): Self::Row) -> Self { | |
let json = json!({"badge_type": badge_type, "attributes": attributes}); | |
serde_json::from_value(json) | |
.expect("Invalid CI badge in the database") | |
} | |
} | |
impl Badge { | |
pub fn encodable(self) -> EncodableBadge { | |
serde_json::from_value(serde_json::to_value(self).unwrap()).unwrap() | |
} | |
pub fn badge_type(&self) -> &'static str { | |
match *self { | |
Badge::TravisCi {..} => "travis-ci", | |
Badge::Appveyor {..} => "appveyor", | |
Badge::GitLab {..} => "gitlab", | |
Badge::Maintenance {..} => "maintenance", | |
} | |
} | |
pub fn update_crate<'a>(conn: &PgConnection, | |
krate: &Crate, | |
badges: Option<&'a HashMap<String, HashMap<String, String>>>) | |
-> CargoResult<Vec<&'a str>> { | |
use diesel::{insert, delete}; | |
#[derive(Insertable)] | |
#[table_name="badges"] | |
struct NewBadge<'a> { | |
crate_id: i32, | |
badge_type: &'a str, | |
attributes: serde_json::Value, | |
} | |
let mut invalid_badges = vec![]; | |
let mut new_badges = vec![]; | |
if let Some(badges) = badges { | |
for (k, v) in badges { | |
let attributes_json = serde_json::to_value(v).unwrap(); | |
let json = json!({"badge_type": k, "attributes": attributes_json}); | |
if serde_json::from_value::<Badge>(json).is_ok() { | |
new_badges.push(NewBadge { | |
crate_id: krate.id, | |
badge_type: &**k, | |
attributes: attributes_json, | |
}); | |
} else { | |
invalid_badges.push(&**k); | |
} | |
} | |
} | |
conn.transaction(|| { | |
delete(badges::table.filter(badges::crate_id.eq(krate.id))).execute(conn)?; | |
insert(&new_badges).into(badges::table).execute(conn)?; | |
Ok(invalid_badges) | |
}) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Badge::Maintenance { value: MaintenanceValue:eprecated } => "deprecated" add this 👍