@@ -5,7 +5,8 @@ use Model;
5
5
use std:: collections:: HashMap ;
6
6
use pg:: GenericConnection ;
7
7
use pg:: rows:: Row ;
8
- use rustc_serialize:: json:: Json ;
8
+ use rustc_serialize:: Decodable ;
9
+ use rustc_serialize:: json:: { Json , Decoder } ;
9
10
10
11
#[ derive( Debug , PartialEq , Clone ) ]
11
12
pub enum Badge {
@@ -29,61 +30,12 @@ pub struct EncodableBadge {
29
30
impl Model for Badge {
30
31
fn from_row ( row : & Row ) -> Badge {
31
32
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" )
87
39
}
88
40
fn table_name ( _: Option < Badge > ) -> & ' static str { "badges" }
89
41
}
0 commit comments