@@ -64,10 +64,10 @@ fn run_json_tests(json_data: &str, parse: &fn (input: ~str) -> json::Json) {
64
64
#[ test]
65
65
fn component_value_list( ) {
66
66
do run_json_tests( include_str ! ( "css-parsing-tests/component_value_list.json" ) ) |input| {
67
- let mut parser = Parser :: from_str( input) ;
67
+ let parser = & mut Parser :: from_str( input) ;
68
68
let mut results = ~[ ] ;
69
69
loop {
70
- match next_component_value( & mut parser) {
70
+ match next_component_value( parser) {
71
71
Some ( ( c, _) ) => results. push( c) ,
72
72
None => break ,
73
73
}
@@ -80,13 +80,9 @@ fn component_value_list() {
80
80
#[ test]
81
81
fn one_component_value( ) {
82
82
do run_json_tests( include_str ! ( "css-parsing-tests/one_component_value.json" ) ) |input| {
83
- let mut iter = ComponentValueIterator :: from_str( input) ;
84
- match iter. next_non_whitespace( ) {
85
- None => json : : List ( ~[ json:: String ( ~"error") , json:: String ( ~"empty") ] ) ,
86
- Some( ( component_value, _) ) => match iter. next_non_whitespace( ) {
87
- Some ( _) => json:: List ( ~[ json:: String ( ~"error") , json:: String ( ~"extra-input") ] ) ,
88
- None => component_value. to_json( ) ,
89
- }
83
+ match parse_one_component_value( & mut ComponentValueIterator :: from_str( input) ) {
84
+ Ok ( ( component_value, _location) ) => component_value. to_json( ) ,
85
+ Err ( reason) => reason. to_json( ) ,
90
86
}
91
87
}
92
88
}
@@ -95,15 +91,12 @@ fn one_component_value() {
95
91
#[ test]
96
92
fn declaration_list( ) {
97
93
do run_json_tests( include_str ! ( "css-parsing-tests/declaration_list.json" ) ) |input| {
98
- let mut iter = ComponentValueIterator :: from_str( input) ;
94
+ let iter = & mut ComponentValueIterator :: from_str( input) ;
99
95
let mut declarations = ~[ ] ;
100
96
loop {
101
- match parse_declaration_or_at_rule( & mut iter) {
97
+ match parse_declaration_or_at_rule( iter) {
102
98
None => break ,
103
- Some ( result) => declarations. push( match result {
104
- Ok ( declaration) => declaration. to_json( ) ,
105
- Err ( reason) => reason. to_json( ) ,
106
- } )
99
+ Some ( result) => declarations. push( result_to_json( result) ) ,
107
100
}
108
101
}
109
102
json:: List ( declarations)
@@ -114,26 +107,20 @@ fn declaration_list() {
114
107
#[ test]
115
108
fn one_declaration( ) {
116
109
do run_json_tests( include_str ! ( "css-parsing-tests/one_declaration.json" ) ) |input| {
117
- match parse_one_declaration( & mut ComponentValueIterator :: from_str( input) ) {
118
- Ok ( declaration) => declaration. to_json( ) ,
119
- Err ( reason) => reason. to_json( ) ,
120
- }
110
+ result_to_json( parse_one_declaration( & mut ComponentValueIterator :: from_str( input) ) )
121
111
}
122
112
}
123
113
124
114
125
115
#[ test]
126
116
fn rule_list( ) {
127
117
do run_json_tests( include_str ! ( "css-parsing-tests/rule_list.json" ) ) |input| {
128
- let mut iter = ComponentValueIterator :: from_str( input) ;
118
+ let iter = & mut ComponentValueIterator :: from_str( input) ;
129
119
let mut rules = ~[ ] ;
130
120
loop {
131
- match parse_rule( & mut iter) {
121
+ match parse_rule( iter) {
132
122
None => break ,
133
- Some ( result) => rules. push( match result {
134
- Ok ( rule) => rule. to_json( ) ,
135
- Err ( reason) => reason. to_json( ) ,
136
- } )
123
+ Some ( result) => rules. push( result_to_json( result) ) ,
137
124
}
138
125
}
139
126
json:: List ( rules)
@@ -144,10 +131,15 @@ fn rule_list() {
144
131
#[ test]
145
132
fn one_rule( ) {
146
133
do run_json_tests( include_str ! ( "css-parsing-tests/one_rule.json" ) ) |input| {
147
- match parse_one_rule( & mut ComponentValueIterator :: from_str( input) ) {
148
- Ok ( rule) => rule. to_json( ) ,
149
- Err ( reason) => reason. to_json( ) ,
150
- }
134
+ result_to_json( parse_one_rule( & mut ComponentValueIterator :: from_str( input) ) )
135
+ }
136
+ }
137
+
138
+
139
+ fn result_to_json < A : ToJson , B : ToJson > ( result: Result < A , B > ) -> json:: Json {
140
+ match result {
141
+ Ok ( ref a) => a. to_json( ) ,
142
+ Err ( ref b) => b. to_json( ) ,
151
143
}
152
144
}
153
145
0 commit comments