Skip to content

Commit 1a0bba2

Browse files
Merge pull request #598 from sgrif/sg-cleanup-badge-from-row
Remove duplicated deserialization logic for `Badge`
2 parents 24613d7 + 207e342 commit 1a0bba2

File tree

1 file changed

+8
-56
lines changed

1 file changed

+8
-56
lines changed

src/badge.rs

Lines changed: 8 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ use Model;
55
use std::collections::HashMap;
66
use pg::GenericConnection;
77
use pg::rows::Row;
8-
use rustc_serialize::json::Json;
8+
use rustc_serialize::Decodable;
9+
use rustc_serialize::json::{Json, Decoder};
910

1011
#[derive(Debug, PartialEq, Clone)]
1112
pub enum Badge {
@@ -29,61 +30,12 @@ pub struct EncodableBadge {
2930
impl Model for Badge {
3031
fn from_row(row: &Row) -> Badge {
3132
let attributes: Json = row.get("attributes");
32-
if let Json::Object(attributes) = attributes {
33-
let badge_type: String = row.get("badge_type");
34-
match badge_type.as_str() {
35-
"travis-ci" => {
36-
Badge::TravisCi {
37-
branch: attributes.get("branch")
38-
.and_then(Json::as_string)
39-
.map(str::to_string),
40-
repository: attributes.get("repository")
41-
.and_then(Json::as_string)
42-
.map(str::to_string)
43-
.expect("Invalid TravisCi badge \
44-
without repository in the \
45-
database"),
46-
}
47-
},
48-
"appveyor" => {
49-
Badge::Appveyor {
50-
service: attributes.get("service")
51-
.and_then(Json::as_string)
52-
.map(str::to_string),
53-
branch: attributes.get("branch")
54-
.and_then(Json::as_string)
55-
.map(str::to_string),
56-
repository: attributes.get("repository")
57-
.and_then(Json::as_string)
58-
.map(str::to_string)
59-
.expect("Invalid Appveyor badge \
60-
without repository in the \
61-
database"),
62-
}
63-
},
64-
"gitlab" => {
65-
Badge::GitLab {
66-
branch: attributes.get("branch")
67-
.and_then(Json::as_string)
68-
.map(str::to_string),
69-
repository: attributes.get("repository")
70-
.and_then(Json::as_string)
71-
.map(str::to_string)
72-
.expect("Invalid GitLab badge \
73-
without repository in the \
74-
database"),
75-
}
76-
},
77-
_ => {
78-
panic!("Unknown badge type {} in the database", badge_type);
79-
},
80-
}
81-
} else {
82-
panic!(
83-
"badge attributes {:?} in the database was not a JSON object",
84-
attributes
85-
);
86-
}
33+
let badge_type: String = row.get("badge_type");
34+
let mut decoder = Decoder::new(attributes);
35+
let attributes = HashMap::<String, String>::decode(&mut decoder)
36+
.expect("Attributes was not a json object");
37+
Self::from_attributes(&badge_type, &attributes)
38+
.expect("Invalid CI badge in the database")
8739
}
8840
fn table_name(_: Option<Badge>) -> &'static str { "badges" }
8941
}

0 commit comments

Comments
 (0)