Skip to content

Commit d32e964

Browse files
authored
Merge pull request #22 from maciejhirsz/0.7.0
0.7.0 partial equality, closes #21
2 parents daa6b14 + 5947ade commit d32e964

File tree

5 files changed

+130
-39
lines changed

5 files changed

+130
-39
lines changed

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "json"
3-
version = "0.6.1"
3+
version = "0.7.0"
44
authors = ["Maciej Hirsz <[email protected]>"]
55
description = "JSON implementation in Rust"
66
repository = "https://github.com/maciejhirsz/json-rust"

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ let data = json::parse(r#"
3838
3939
"#).unwrap();
4040

41-
assert!(data["code"].is(200));
42-
assert!(data["success"].is(true));
41+
assert!(data["code"] == 200);
42+
assert!(data["success"] == true));
4343
assert!(data["payload"]["features"].is_array());
44-
assert!(data["payload"]["features"][0].is("awesome"));
44+
assert!(data["payload"]["features"][0] == "awesome");
4545
assert!(data["payload"]["features"].contains("easyAPI"));
4646

4747
// Error resilient: non-existent values default to null

src/lib.rs

+93-3
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@
3434
//!
3535
//! "#).unwrap();
3636
//!
37-
//! assert!(data["code"].is(200));
38-
//! assert!(data["success"].is(true));
37+
//! assert!(data["code"] == 200);
38+
//! assert!(data["success"] == true);
3939
//! assert!(data["payload"]["features"].is_array());
40-
//! assert!(data["payload"]["features"][0].is("awesome"));
40+
//! assert!(data["payload"]["features"][0] == "awesome");
4141
//! assert!(data["payload"]["features"].contains("easyAPI"));
4242
//!
4343
//! // Error resilient: non-existent values default to null
@@ -333,6 +333,33 @@ macro_rules! implement {
333333
}
334334
}
335335

336+
impl PartialEq<$from> for JsonValue {
337+
fn eq(&self, other: &$from) -> bool {
338+
match *self {
339+
JsonValue::$to(ref value) => value == &(*other as $wanted),
340+
_ => false
341+
}
342+
}
343+
}
344+
345+
impl<'a> PartialEq<$from> for &'a JsonValue {
346+
fn eq(&self, other: &$from) -> bool {
347+
match **self {
348+
JsonValue::$to(ref value) => value == &(*other as $wanted),
349+
_ => false
350+
}
351+
}
352+
}
353+
354+
impl PartialEq<JsonValue> for $from {
355+
fn eq(&self, other: &JsonValue) -> bool {
356+
match *other {
357+
JsonValue::$to(ref value) => value == &(*self as $wanted),
358+
_ => false
359+
}
360+
}
361+
}
362+
336363
implement_extras!($from);
337364
};
338365
($to:ident, $from:ty) => {
@@ -342,6 +369,33 @@ macro_rules! implement {
342369
}
343370
}
344371

372+
impl PartialEq<$from> for JsonValue {
373+
fn eq(&self, other: &$from) -> bool {
374+
match *self {
375+
JsonValue::$to(ref value) => value == other,
376+
_ => false
377+
}
378+
}
379+
}
380+
381+
impl<'a> PartialEq<$from> for &'a JsonValue {
382+
fn eq(&self, other: &$from) -> bool {
383+
match **self {
384+
JsonValue::$to(ref value) => value == other,
385+
_ => false
386+
}
387+
}
388+
}
389+
390+
impl PartialEq<JsonValue> for $from {
391+
fn eq(&self, other: &JsonValue) -> bool {
392+
match *other {
393+
JsonValue::$to(ref value) => value == self,
394+
_ => false
395+
}
396+
}
397+
}
398+
345399
implement_extras!($from);
346400
}
347401
}
@@ -391,6 +445,42 @@ impl From<Option<JsonValue>> for JsonValue {
391445
}
392446
}
393447

448+
impl<'a> PartialEq<&'a str> for JsonValue {
449+
fn eq(&self, other: &&str) -> bool {
450+
match *self {
451+
JsonValue::String(ref value) => value == *other,
452+
_ => false
453+
}
454+
}
455+
}
456+
457+
impl<'a> PartialEq<JsonValue> for &'a str {
458+
fn eq(&self, other: &JsonValue) -> bool {
459+
match *other {
460+
JsonValue::String(ref value) => value == *self,
461+
_ => false
462+
}
463+
}
464+
}
465+
466+
impl PartialEq<str> for JsonValue {
467+
fn eq(&self, other: &str) -> bool {
468+
match *self {
469+
JsonValue::String(ref value) => value == other,
470+
_ => false
471+
}
472+
}
473+
}
474+
475+
impl<'a> PartialEq<JsonValue> for str {
476+
fn eq(&self, other: &JsonValue) -> bool {
477+
match *other {
478+
JsonValue::String(ref value) => value == self,
479+
_ => false
480+
}
481+
}
482+
}
483+
394484
implement!(String, String);
395485
implement!(Number, isize as f64);
396486
implement!(Number, usize as f64);

src/value.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ impl JsonValue {
5050
}
5151

5252
/// Checks if the value stored matches `other`.
53+
#[deprecated(since="0.7.0", note="Use `value == other` instead")]
5354
pub fn is<T>(&self, other: T) -> bool where T: Into<JsonValue> {
5455
*self == other.into()
5556
}
@@ -151,7 +152,7 @@ impl JsonValue {
151152
}
152153
}
153154

154-
#[deprecated(since="0.6.1", note="Unnecessary, use `as_bool` instead")]
155+
#[deprecated(since="0.6.1", note="Use `as_bool` instead")]
155156
pub fn as_boolean(&self) -> JsonResult<&bool> {
156157
match *self {
157158
JsonValue::Boolean(ref value) => Ok(value),

tests/lib.rs

+31-31
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,14 @@ fn is_as_boolean() {
6565
fn is_true() {
6666
let boolean = JsonValue::Boolean(true);
6767

68-
assert!(boolean.is(true));
68+
assert_eq!(boolean, true);
6969
}
7070

7171
#[test]
7272
fn is_false() {
7373
let boolean = JsonValue::Boolean(false);
7474

75-
assert!(boolean.is(false));
75+
assert_eq!(boolean, false);
7676
}
7777

7878
#[test]
@@ -268,12 +268,12 @@ fn object_dump_pretty() {
268268

269269
#[test]
270270
fn parse_true() {
271-
assert!(parse("true").unwrap().is(true));
271+
assert_eq!(parse("true").unwrap(), true);
272272
}
273273

274274
#[test]
275275
fn parse_false() {
276-
assert!(parse("false").unwrap().is(false));
276+
assert_eq!(parse("false").unwrap(), false);
277277
}
278278

279279
#[test]
@@ -283,35 +283,35 @@ fn parse_null() {
283283

284284
#[test]
285285
fn parse_number() {
286-
assert!(parse("3.14").unwrap().is(3.14))
286+
assert_eq!(parse("3.14").unwrap(), 3.14);
287287
}
288288

289289
#[test]
290290
fn parse_integer() {
291-
assert!(parse("42").unwrap().is(42));
291+
assert_eq!(parse("42").unwrap(), 42);
292292
}
293293

294294
#[test]
295295
fn parse_negative_integer() {
296-
assert!(parse("-42").unwrap().is(-42));
296+
assert_eq!(parse("-42").unwrap(), -42);
297297
}
298298

299299
#[test]
300300
fn parse_number_with_e() {
301-
assert!(parse("5e2").unwrap().is(500));
302-
assert!(parse("5E2").unwrap().is(500));
301+
assert_eq!(parse("5e2").unwrap(), 500);
302+
assert_eq!(parse("5E2").unwrap(), 500);
303303
}
304304

305305
#[test]
306306
fn parse_number_with_positive_e() {
307-
assert!(parse("5e+2").unwrap().is(500));
308-
assert!(parse("5E+2").unwrap().is(500));
307+
assert_eq!(parse("5e+2").unwrap(), 500);
308+
assert_eq!(parse("5E+2").unwrap(), 500);
309309
}
310310

311311
#[test]
312312
fn parse_number_with_negative_e() {
313-
assert!(parse("5e-2").unwrap().is(0.05));
314-
assert!(parse("5E-2").unwrap().is(0.05));
313+
assert_eq!(parse("5e-2").unwrap(), 0.05);
314+
assert_eq!(parse("5E-2").unwrap(), 0.05);
315315
}
316316

317317
#[test]
@@ -382,7 +382,7 @@ fn parse_and_index_from_object() {
382382
let data = parse("{ \"pi\": 3.14 }").unwrap();
383383
let ref pi = data["pi"];
384384

385-
assert!(pi.is(3.14));
385+
assert_eq!(pi, 3.14);
386386
}
387387

388388
#[test]
@@ -395,11 +395,11 @@ fn parse_and_index_mut_from_object() {
395395
396396
"#).unwrap();
397397

398-
assert!(data["foo"].is(100));
398+
assert_eq!(data["foo"], 100);
399399

400400
data["foo"] = 200.into();
401401

402-
assert!(data["foo"].is(200));
402+
assert_eq!(data["foo"], 200);
403403
}
404404

405405
#[test]
@@ -414,7 +414,7 @@ fn parse_and_index_mut_from_null() {
414414
data["foo"]["bar"] = 100.into();
415415

416416
assert!(data.is_object());
417-
assert!(data["foo"]["bar"].is(100));
417+
assert_eq!(data["foo"]["bar"], 100);
418418

419419
assert_eq!(data.dump(), r#"{"foo":{"bar":100}}"#);
420420
}
@@ -423,12 +423,12 @@ fn parse_and_index_mut_from_null() {
423423
fn parse_and_index_from_array() {
424424
let data = parse(r#"[100, 200, false, null, "foo"]"#).unwrap();
425425

426-
assert!(data[0].is(100));
427-
assert!(data[1].is(200));
428-
assert!(data[2].is(false));
429-
assert!(data[3].is_null());
430-
assert!(data[4].is("foo"));
431-
assert!(data[5].is_null());
426+
assert_eq!(data[0], 100);
427+
assert_eq!(data[1], 200);
428+
assert_eq!(data[2], false);
429+
assert_eq!(data[3], Null);
430+
assert_eq!(data[4], "foo");
431+
assert_eq!(data[5], Null);
432432
}
433433

434434
#[test]
@@ -441,8 +441,8 @@ fn parse_and_index_mut_from_array() {
441441
data[3] = "modified".into();
442442
data[5] = "implicid push".into();
443443

444-
assert!(data[3].is("modified"));
445-
assert!(data[5].is("implicid push"));
444+
assert_eq!(data[3], "modified");
445+
assert_eq!(data[5], "implicid push");
446446
}
447447

448448
#[test]
@@ -454,7 +454,7 @@ fn parse_escaped_characters() {
454454
"#).unwrap();
455455

456456
assert!(data.is_string());
457-
assert!(data.is("\r\n\t\u{8}\u{c}\\/\""));
457+
assert_eq!(data, "\r\n\t\u{8}\u{c}\\/\"");
458458
}
459459

460460
#[test]
@@ -465,7 +465,7 @@ fn parse_escaped_unicode() {
465465
466466
"#).unwrap();
467467

468-
assert!(data.is("❤️"));
468+
assert_eq!(data, "❤️");
469469
}
470470

471471
#[test]
@@ -526,11 +526,11 @@ fn iter_entries() {
526526

527527
let (key, value) = entries.next().unwrap();
528528
assert_eq!(key, "a");
529-
assert!(value.is(1));
529+
assert_eq!(value, 1);
530530

531531
let (key, value) = entries.next().unwrap();
532532
assert_eq!(key, "b");
533-
assert!(value.is("foo"));
533+
assert_eq!(value, "foo");
534534

535535
assert!(entries.next().is_none());
536536
}
@@ -563,8 +563,8 @@ fn iter_members() {
563563

564564
let mut members = data.members();
565565

566-
assert!(members.next().unwrap().is(1));
567-
assert!(members.next().unwrap().is("foo"));
566+
assert_eq!(members.next().unwrap(), 1);
567+
assert_eq!(members.next().unwrap(), "foo");
568568
assert!(members.next().is_none());
569569
}
570570

0 commit comments

Comments
 (0)