From 207e342effc66e27c6b3978d6e8601039633b8d0 Mon Sep 17 00:00:00 2001 From: Sean Griffin Date: Mon, 6 Mar 2017 16:29:15 -0500 Subject: [PATCH] Remove duplicated deserialization logic for `Badge` --- src/badge.rs | 64 +++++++--------------------------------------------- 1 file changed, 8 insertions(+), 56 deletions(-) diff --git a/src/badge.rs b/src/badge.rs index 30dea5a8bee..e73eac01c7c 100644 --- a/src/badge.rs +++ b/src/badge.rs @@ -5,7 +5,8 @@ use Model; use std::collections::HashMap; use pg::GenericConnection; use pg::rows::Row; -use rustc_serialize::json::Json; +use rustc_serialize::Decodable; +use rustc_serialize::json::{Json, Decoder}; #[derive(Debug, PartialEq, Clone)] pub enum Badge { @@ -29,61 +30,12 @@ pub struct EncodableBadge { impl Model for Badge { fn from_row(row: &Row) -> Badge { let attributes: Json = row.get("attributes"); - if let Json::Object(attributes) = attributes { - let badge_type: String = row.get("badge_type"); - match badge_type.as_str() { - "travis-ci" => { - Badge::TravisCi { - branch: attributes.get("branch") - .and_then(Json::as_string) - .map(str::to_string), - repository: attributes.get("repository") - .and_then(Json::as_string) - .map(str::to_string) - .expect("Invalid TravisCi badge \ - without repository in the \ - database"), - } - }, - "appveyor" => { - Badge::Appveyor { - service: attributes.get("service") - .and_then(Json::as_string) - .map(str::to_string), - branch: attributes.get("branch") - .and_then(Json::as_string) - .map(str::to_string), - repository: attributes.get("repository") - .and_then(Json::as_string) - .map(str::to_string) - .expect("Invalid Appveyor badge \ - without repository in the \ - database"), - } - }, - "gitlab" => { - Badge::GitLab { - branch: attributes.get("branch") - .and_then(Json::as_string) - .map(str::to_string), - repository: attributes.get("repository") - .and_then(Json::as_string) - .map(str::to_string) - .expect("Invalid GitLab badge \ - without repository in the \ - database"), - } - }, - _ => { - panic!("Unknown badge type {} in the database", badge_type); - }, - } - } else { - panic!( - "badge attributes {:?} in the database was not a JSON object", - 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) + .expect("Invalid CI badge in the database") } fn table_name(_: Option) -> &'static str { "badges" } }