Skip to content

Commit

Permalink
RUST-343 Remove old doc! macro syntax (crossterm-rs#167)
Browse files Browse the repository at this point in the history
  • Loading branch information
saghm authored May 14, 2020
1 parent a676305 commit d84ea8c
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 97 deletions.
4 changes: 2 additions & 2 deletions src/bson.rs
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ impl Bson {
Bson::UtcDatetime(ref v) => {
doc! {
"$date": {
"$numberLong" => (v.timestamp() * 1000) + v.nanosecond() as i64 / 1_000_000,
"$numberLong": (v.timestamp() * 1000) + v.nanosecond() as i64 / 1_000_000,
}
}
}
Expand All @@ -461,7 +461,7 @@ impl Bson {
#[cfg(feature = "decimal128")]
Bson::Decimal128(ref v) => {
doc! {
"$numberDecimal" => (v.to_string())
"$numberDecimal": (v.to_string())
}
}
Bson::Undefined => {
Expand Down
34 changes: 0 additions & 34 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,56 +92,31 @@ macro_rules! bson {
};

// Next value is `null`.
(@object $object:ident ($($key:tt)+) (=> null $($rest:tt)*) $copy:tt) => {
$crate::bson!(@object $object [$($key)+] ($crate::bson!(null)) $($rest)*);
};

(@object $object:ident ($($key:tt)+) (: null $($rest:tt)*) $copy:tt) => {
$crate::bson!(@object $object [$($key)+] ($crate::bson!(null)) $($rest)*);
};

// Next value is an array.
(@object $object:ident ($($key:tt)+) (=> [$($array:tt)*] $($rest:tt)*) $copy:tt) => {
$crate::bson!(@object $object [$($key)+] ($crate::bson!([$($array)*])) $($rest)*);
};

(@object $object:ident ($($key:tt)+) (: [$($array:tt)*] $($rest:tt)*) $copy:tt) => {
$crate::bson!(@object $object [$($key)+] ($crate::bson!([$($array)*])) $($rest)*);
};

// Next value is a map.
(@object $object:ident ($($key:tt)+) (=> {$($map:tt)*} $($rest:tt)*) $copy:tt) => {
$crate::bson!(@object $object [$($key)+] ($crate::bson!({$($map)*})) $($rest)*);
};

(@object $object:ident ($($key:tt)+) (: {$($map:tt)*} $($rest:tt)*) $copy:tt) => {
$crate::bson!(@object $object [$($key)+] ($crate::bson!({$($map)*})) $($rest)*);
};

// Next value is an expression followed by comma.
(@object $object:ident ($($key:tt)+) (=> $value:expr , $($rest:tt)*) $copy:tt) => {
$crate::bson!(@object $object [$($key)+] ($crate::bson!($value)) , $($rest)*);
};

(@object $object:ident ($($key:tt)+) (: $value:expr , $($rest:tt)*) $copy:tt) => {
$crate::bson!(@object $object [$($key)+] ($crate::bson!($value)) , $($rest)*);
};

// Last value is an expression with no trailing comma.
(@object $object:ident ($($key:tt)+) (=> $value:expr) $copy:tt) => {
$crate::bson!(@object $object [$($key)+] ($crate::bson!($value)));
};

(@object $object:ident ($($key:tt)+) (: $value:expr) $copy:tt) => {
$crate::bson!(@object $object [$($key)+] ($crate::bson!($value)));
};

// Missing value for last entry. Trigger a reasonable error message.
(@object $object:ident ($($key:tt)+) (=>) $copy:tt) => {
// "unexpected end of macro invocation"
$crate::bson!();
};

(@object $object:ident ($($key:tt)+) (:) $copy:tt) => {
// "unexpected end of macro invocation"
$crate::bson!();
Expand All @@ -155,11 +130,6 @@ macro_rules! bson {
};

// Misplaced key-value separator. Trigger a reasonable error message.
(@object $object:ident () (=> $($rest:tt)*) ($kv_separator:tt $($copy:tt)*)) => {
// Takes no arguments so "no rules expected the token `=>`".
unimplemented!($kv_separator);
};

(@object $object:ident () (: $($rest:tt)*) ($kv_separator:tt $($copy:tt)*)) => {
// Takes no arguments so "no rules expected the token `:`".
unimplemented!($kv_separator);
Expand All @@ -173,10 +143,6 @@ macro_rules! bson {

// Key is fully parenthesized. This avoids clippy double_parens false
// positives because the parenthesization may be necessary here.
(@object $object:ident () (($key:expr) => $($rest:tt)*) $copy:tt) => {
$crate::bson!(@object $object ($key) (=> $($rest)*) (=> $($rest)*));
};

(@object $object:ident () (($key:expr) : $($rest:tt)*) $copy:tt) => {
$crate::bson!(@object $object ($key) (: $($rest)*) (: $($rest)*));
};
Expand Down
4 changes: 2 additions & 2 deletions tests/modules/encoder_decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ fn test_decode_utc_date_time_overflows() {

let decoded = decode_document(&mut Cursor::new(raw)).unwrap();

let expected = doc! { "A" => Utc.timestamp(1_530_492_218, 999 * 1_000_000)};
let expected = doc! { "A": Utc.timestamp(1_530_492_218, 999 * 1_000_000)};
assert_eq!(decoded, expected);
}

Expand All @@ -366,7 +366,7 @@ fn test_encode_decode_decimal128() {
26, 0, 0, 0, 19, 107, 101, 121, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 34, 0,
];

let doc = doc! { "key" => val };
let doc = doc! { "key": val };

let mut buf = Vec::new();
encode_document(&mut buf, &doc).unwrap();
Expand Down
60 changes: 1 addition & 59 deletions tests/modules/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ fn standard_format() {
let doc = doc! {
"float": 2.4,
"string": "hello",
"array" => ["testing", 1, true, [1, 2]],
"array": ["testing", 1, true, [1, 2]],
"doc": {
"fish": "in",
"a": "barrel",
Expand Down Expand Up @@ -47,53 +47,6 @@ fn standard_format() {
assert_eq!(expected, format!("{}", doc));
}

// Support rocket format for backwards-compatibility with pre-0.10.
#[test]
fn rocket_format() {
let id_string = "thisismyname";
let string_bytes: Vec<_> = id_string.bytes().collect();
let mut bytes = [0; 12];
bytes[..12].clone_from_slice(&string_bytes[..12]);

let id = ObjectId::with_bytes(bytes);
let date = Utc::now();

let doc = doc! {
"float" => 2.4,
"string" => "hello",
"array" => ["testing", 1, true, ["nested", 2]],
"doc" => {
"fish" => "in",
"a" => "barrel",
"!" => 1,
},
"bool" => true,
"null" => null,
"regexp" => Bson::Regex(Regex { pattern: "s[ao]d".to_owned(), options: "i".to_owned() }),
"with_wrapped_parens" => (-20),
"code" => Bson::JavaScriptCode("function(x) { return x._id; }".to_owned()),
"i32" => 12,
"i64" => -55,
"timestamp" => Bson::TimeStamp(TimeStamp { time: 0, increment: 229_999_444 }),
"binary" => Binary { subtype: BinarySubtype::Md5, bytes: "thingies".to_owned().into_bytes() },
"_id" => id,
"date" => Bson::UtcDatetime(date),
};

let expected = format!(
"{{ float: 2.4, string: \"hello\", array: [\"testing\", 1, true, [\"nested\", 2]], doc: \
{{ fish: \"in\", a: \"barrel\", !: 1 }}, bool: true, null: null, regexp: /s[ao]d/i, \
with_wrapped_parens: -20, code: function(x) {{ return x._id; }}, i32: 12, i64: -55, \
timestamp: Timestamp(0, 229999444), binary: BinData(5, 0x{}), _id: ObjectId(\"{}\"), \
date: Date(\"{}\") }}",
hex::encode("thingies"),
hex::encode(id_string),
date
);

assert_eq!(expected, format!("{}", doc));
}

#[test]
fn non_trailing_comma() {
let doc = doc! {
Expand All @@ -105,17 +58,6 @@ fn non_trailing_comma() {
assert_eq!(expected, format!("{}", doc));
}

#[test]
fn non_trailing_comma_with_rockets() {
let doc = doc! {
"a" => "foo",
"b" => { "ok": "then" }
};

let expected = "{ a: \"foo\", b: { ok: \"then\" } }".to_string();
assert_eq!(expected, format!("{}", doc));
}

#[test]
#[allow(clippy::float_cmp)]
fn recursive_macro() {
Expand Down

0 comments on commit d84ea8c

Please sign in to comment.