Skip to content

Commit c32286d

Browse files
committed
libserialize: Code cleanup
1 parent fec0f16 commit c32286d

File tree

1 file changed

+65
-69
lines changed

1 file changed

+65
-69
lines changed

src/libserialize/json.rs

Lines changed: 65 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@
9191
//! fn main() {
9292
//! let object = TestStruct {
9393
//! data_int: 1,
94-
//! data_str: "toto".to_string(),
94+
//! data_str: "homura".to_string(),
9595
//! data_vector: vec![2,3,4,5],
9696
//! };
9797
//!
@@ -178,7 +178,7 @@
178178
//! // Serialize using `ToJson`
179179
//! let input_data = TestStruct {
180180
//! data_int: 1,
181-
//! data_str: "toto".to_string(),
181+
//! data_str: "madoka".to_string(),
182182
//! data_vector: vec![2,3,4,5],
183183
//! };
184184
//! let json_obj: Json = input_data.to_json();
@@ -443,7 +443,9 @@ impl<'a> ::Encoder<io::IoError> for Encoder<'a> {
443443
fn emit_f64(&mut self, v: f64) -> EncodeResult {
444444
write!(self.writer, "{}", fmt_number_or_null(v))
445445
}
446-
fn emit_f32(&mut self, v: f32) -> EncodeResult { self.emit_f64(v as f64) }
446+
fn emit_f32(&mut self, v: f32) -> EncodeResult {
447+
self.emit_f64(v as f64)
448+
}
447449

448450
fn emit_char(&mut self, v: char) -> EncodeResult {
449451
escape_char(self.writer, v)
@@ -452,7 +454,9 @@ impl<'a> ::Encoder<io::IoError> for Encoder<'a> {
452454
escape_str(self.writer, v)
453455
}
454456

455-
fn emit_enum(&mut self, _name: &str, f: |&mut Encoder<'a>| -> EncodeResult) -> EncodeResult {
457+
fn emit_enum(&mut self,
458+
_name: &str,
459+
f: |&mut Encoder<'a>| -> EncodeResult) -> EncodeResult {
456460
f(self)
457461
}
458462

@@ -663,7 +667,7 @@ impl<'a> ::Encoder<io::IoError> for PrettyEncoder<'a> {
663667

664668
fn emit_enum_variant(&mut self,
665669
name: &str,
666-
_: uint,
670+
_id: uint,
667671
cnt: uint,
668672
f: |&mut PrettyEncoder<'a>| -> EncodeResult) -> EncodeResult {
669673
if cnt == 0 {
@@ -1962,30 +1966,22 @@ macro_rules! read_primitive {
19621966
($name:ident, $ty:ty) => {
19631967
fn $name(&mut self) -> DecodeResult<$ty> {
19641968
match self.pop() {
1965-
Json::I64(f) => {
1966-
match num::cast(f) {
1967-
Some(f) => Ok(f),
1968-
None => Err(ExpectedError("Number".to_string(), format!("{}", f))),
1969-
}
1970-
}
1971-
Json::U64(f) => {
1972-
match num::cast(f) {
1973-
Some(f) => Ok(f),
1974-
None => Err(ExpectedError("Number".to_string(), format!("{}", f))),
1975-
}
1976-
}
1977-
Json::F64(f) => {
1978-
Err(ExpectedError("Integer".to_string(), format!("{}", f)))
1979-
}
1980-
Json::String(s) => {
1981-
// re: #12967.. a type w/ numeric keys (ie HashMap<uint, V> etc)
1982-
// is going to have a string here, as per JSON spec.
1983-
match std::str::from_str(s.as_slice()) {
1984-
Some(f) => Ok(f),
1985-
None => Err(ExpectedError("Number".to_string(), s)),
1986-
}
1969+
Json::I64(f) => match num::cast(f) {
1970+
Some(f) => Ok(f),
1971+
None => Err(ExpectedError("Number".to_string(), format!("{}", f))),
1972+
},
1973+
Json::U64(f) => match num::cast(f) {
1974+
Some(f) => Ok(f),
1975+
None => Err(ExpectedError("Number".to_string(), format!("{}", f))),
1976+
},
1977+
Json::F64(f) => Err(ExpectedError("Integer".to_string(), format!("{}", f))),
1978+
// re: #12967.. a type w/ numeric keys (ie HashMap<uint, V> etc)
1979+
// is going to have a string here, as per JSON spec.
1980+
Json::String(s) => match std::str::from_str(s.as_slice()) {
1981+
Some(f) => Ok(f),
1982+
None => Err(ExpectedError("Number".to_string(), s)),
19871983
},
1988-
value => Err(ExpectedError("Number".to_string(), format!("{}", value)))
1984+
value => Err(ExpectedError("Number".to_string(), format!("{}", value))),
19891985
}
19901986
}
19911987
}
@@ -2484,75 +2480,75 @@ mod tests {
24842480

24852481
#[test]
24862482
fn test_write_null() {
2487-
assert_eq!(Null.to_string().into_string(), "null");
2488-
assert_eq!(Null.to_pretty_str().into_string(), "null");
2483+
assert_eq!(Null.to_string(), "null");
2484+
assert_eq!(Null.to_pretty_str(), "null");
24892485
}
24902486

24912487
#[test]
24922488
fn test_write_i64() {
2493-
assert_eq!(U64(0).to_string().into_string(), "0");
2494-
assert_eq!(U64(0).to_pretty_str().into_string(), "0");
2489+
assert_eq!(U64(0).to_string(), "0");
2490+
assert_eq!(U64(0).to_pretty_str(), "0");
24952491

2496-
assert_eq!(U64(1234).to_string().into_string(), "1234");
2497-
assert_eq!(U64(1234).to_pretty_str().into_string(), "1234");
2492+
assert_eq!(U64(1234).to_string(), "1234");
2493+
assert_eq!(U64(1234).to_pretty_str(), "1234");
24982494

2499-
assert_eq!(I64(-5678).to_string().into_string(), "-5678");
2500-
assert_eq!(I64(-5678).to_pretty_str().into_string(), "-5678");
2495+
assert_eq!(I64(-5678).to_string(), "-5678");
2496+
assert_eq!(I64(-5678).to_pretty_str(), "-5678");
25012497

25022498
assert_eq!(U64(7650007200025252000).to_string(), "7650007200025252000");
25032499
assert_eq!(U64(7650007200025252000).to_pretty_str(), "7650007200025252000");
25042500
}
25052501

25062502
#[test]
25072503
fn test_write_f64() {
2508-
assert_eq!(F64(3.0).to_string().into_string(), "3.0");
2509-
assert_eq!(F64(3.0).to_pretty_str().into_string(), "3.0");
2504+
assert_eq!(F64(3.0).to_string(), "3.0");
2505+
assert_eq!(F64(3.0).to_pretty_str(), "3.0");
25102506

2511-
assert_eq!(F64(3.1).to_string().into_string(), "3.1");
2512-
assert_eq!(F64(3.1).to_pretty_str().into_string(), "3.1");
2507+
assert_eq!(F64(3.1).to_string(), "3.1");
2508+
assert_eq!(F64(3.1).to_pretty_str(), "3.1");
25132509

2514-
assert_eq!(F64(-1.5).to_string().into_string(), "-1.5");
2515-
assert_eq!(F64(-1.5).to_pretty_str().into_string(), "-1.5");
2510+
assert_eq!(F64(-1.5).to_string(), "-1.5");
2511+
assert_eq!(F64(-1.5).to_pretty_str(), "-1.5");
25162512

2517-
assert_eq!(F64(0.5).to_string().into_string(), "0.5");
2518-
assert_eq!(F64(0.5).to_pretty_str().into_string(), "0.5");
2513+
assert_eq!(F64(0.5).to_string(), "0.5");
2514+
assert_eq!(F64(0.5).to_pretty_str(), "0.5");
25192515

2520-
assert_eq!(F64(f64::NAN).to_string().into_string(), "null");
2521-
assert_eq!(F64(f64::NAN).to_pretty_str().into_string(), "null");
2516+
assert_eq!(F64(f64::NAN).to_string(), "null");
2517+
assert_eq!(F64(f64::NAN).to_pretty_str(), "null");
25222518

2523-
assert_eq!(F64(f64::INFINITY).to_string().into_string(), "null");
2524-
assert_eq!(F64(f64::INFINITY).to_pretty_str().into_string(), "null");
2519+
assert_eq!(F64(f64::INFINITY).to_string(), "null");
2520+
assert_eq!(F64(f64::INFINITY).to_pretty_str(), "null");
25252521

2526-
assert_eq!(F64(f64::NEG_INFINITY).to_string().into_string(), "null");
2527-
assert_eq!(F64(f64::NEG_INFINITY).to_pretty_str().into_string(), "null");
2522+
assert_eq!(F64(f64::NEG_INFINITY).to_string(), "null");
2523+
assert_eq!(F64(f64::NEG_INFINITY).to_pretty_str(), "null");
25282524
}
25292525

25302526
#[test]
25312527
fn test_write_str() {
2532-
assert_eq!(String("".to_string()).to_string().into_string(), "\"\"");
2533-
assert_eq!(String("".to_string()).to_pretty_str().into_string(), "\"\"");
2528+
assert_eq!(String("".to_string()).to_string(), "\"\"");
2529+
assert_eq!(String("".to_string()).to_pretty_str(), "\"\"");
25342530

2535-
assert_eq!(String("foo".to_string()).to_string().into_string(), "\"foo\"");
2536-
assert_eq!(String("foo".to_string()).to_pretty_str().into_string(), "\"foo\"");
2531+
assert_eq!(String("homura".to_string()).to_string(), "\"homura\"");
2532+
assert_eq!(String("madoka".to_string()).to_pretty_str(), "\"madoka\"");
25372533
}
25382534

25392535
#[test]
25402536
fn test_write_bool() {
2541-
assert_eq!(Boolean(true).to_string().into_string(), "true");
2542-
assert_eq!(Boolean(true).to_pretty_str().into_string(), "true");
2537+
assert_eq!(Boolean(true).to_string(), "true");
2538+
assert_eq!(Boolean(true).to_pretty_str(), "true");
25432539

2544-
assert_eq!(Boolean(false).to_string().into_string(), "false");
2545-
assert_eq!(Boolean(false).to_pretty_str().into_string(), "false");
2540+
assert_eq!(Boolean(false).to_string(), "false");
2541+
assert_eq!(Boolean(false).to_pretty_str(), "false");
25462542
}
25472543

25482544
#[test]
25492545
fn test_write_array() {
2550-
assert_eq!(Array(vec![]).to_string().into_string(), "[]");
2551-
assert_eq!(Array(vec![]).to_pretty_str().into_string(), "[]");
2546+
assert_eq!(Array(vec![]).to_string(), "[]");
2547+
assert_eq!(Array(vec![]).to_pretty_str(), "[]");
25522548

2553-
assert_eq!(Array(vec![Boolean(true)]).to_string().into_string(), "[true]");
2549+
assert_eq!(Array(vec![Boolean(true)]).to_string(), "[true]");
25542550
assert_eq!(
2555-
Array(vec![Boolean(true)]).to_pretty_str().into_string(),
2551+
Array(vec![Boolean(true)]).to_pretty_str(),
25562552
"\
25572553
[\n \
25582554
true\n\
@@ -2564,10 +2560,10 @@ mod tests {
25642560
Null,
25652561
Array(vec![String("foo\nbar".to_string()), F64(3.5)])]);
25662562

2567-
assert_eq!(long_test_array.to_string().into_string(),
2563+
assert_eq!(long_test_array.to_string(),
25682564
"[false,null,[\"foo\\nbar\",3.5]]");
25692565
assert_eq!(
2570-
long_test_array.to_pretty_str().into_string(),
2566+
long_test_array.to_pretty_str(),
25712567
"\
25722568
[\n \
25732569
false,\n \
@@ -2582,13 +2578,13 @@ mod tests {
25822578

25832579
#[test]
25842580
fn test_write_object() {
2585-
assert_eq!(mk_object(&[]).to_string().into_string(), "{}");
2586-
assert_eq!(mk_object(&[]).to_pretty_str().into_string(), "{}");
2581+
assert_eq!(mk_object(&[]).to_string(), "{}");
2582+
assert_eq!(mk_object(&[]).to_pretty_str(), "{}");
25872583

25882584
assert_eq!(
25892585
mk_object(&[
25902586
("a".to_string(), Boolean(true))
2591-
]).to_string().into_string(),
2587+
]).to_string(),
25922588
"{\"a\":true}"
25932589
);
25942590
assert_eq!(
@@ -2607,7 +2603,7 @@ mod tests {
26072603
]);
26082604

26092605
assert_eq!(
2610-
complex_obj.to_string().into_string(),
2606+
complex_obj.to_string(),
26112607
"{\
26122608
\"b\":[\
26132609
{\"c\":\"\\f\\r\"},\
@@ -2616,7 +2612,7 @@ mod tests {
26162612
}"
26172613
);
26182614
assert_eq!(
2619-
complex_obj.to_pretty_str().into_string(),
2615+
complex_obj.to_pretty_str(),
26202616
"\
26212617
{\n \
26222618
\"b\": [\n \

0 commit comments

Comments
 (0)