Skip to content

Instantly share code, notes, and snippets.

@marti1125
Last active July 11, 2017 19:27
Show Gist options
  • Save marti1125/74fc9870f4f00cc88c529966f413adc8 to your computer and use it in GitHub Desktop.
Save marti1125/74fc9870f4f00cc88c529966f413adc8 to your computer and use it in GitHub Desktop.
enum MaintenanceValue
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)
})
}
}
@marti1125
Copy link
Author

Badge::Maintenance { value: MaintenanceValue:eprecated } => "deprecated" add this 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment