Skip to content

Commit adb3a06

Browse files
authored
RUST-672 Handle reserved values for BinarySubtype (#269)
* RUST-672 Handle reserved values for BinarySubtype * use test lock
1 parent a97fb4c commit adb3a06

File tree

3 files changed

+18
-0
lines changed

3 files changed

+18
-0
lines changed

src/spec.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ const BINARY_SUBTYPE_UUID_OLD: u8 = 0x03;
5353
const BINARY_SUBTYPE_UUID: u8 = 0x04;
5454
const BINARY_SUBTYPE_MD5: u8 = 0x05;
5555
const BINARY_SUBTYPE_ENCRYPTED: u8 = 0x06;
56+
const BINARY_SUBTYPE_USER_DEFINED: u8 = 0x80;
5657

5758
/// All available BSON element types.
5859
///
@@ -151,6 +152,7 @@ pub enum BinarySubtype {
151152
Md5,
152153
Encrypted,
153154
UserDefined(u8),
155+
Reserved(u8),
154156
}
155157

156158
impl From<BinarySubtype> for u8 {
@@ -165,6 +167,7 @@ impl From<BinarySubtype> for u8 {
165167
BinarySubtype::Md5 => BINARY_SUBTYPE_MD5,
166168
BinarySubtype::Encrypted => BINARY_SUBTYPE_ENCRYPTED,
167169
BinarySubtype::UserDefined(x) => x,
170+
BinarySubtype::Reserved(x) => x,
168171
}
169172
}
170173
}
@@ -180,6 +183,7 @@ impl From<u8> for BinarySubtype {
180183
BINARY_SUBTYPE_UUID => BinarySubtype::Uuid,
181184
BINARY_SUBTYPE_MD5 => BinarySubtype::Md5,
182185
BINARY_SUBTYPE_ENCRYPTED => BinarySubtype::Encrypted,
186+
_ if t < BINARY_SUBTYPE_USER_DEFINED => BinarySubtype::Reserved(t),
183187
_ => BinarySubtype::UserDefined(t),
184188
}
185189
}

src/tests/binary_subtype.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
use crate::{spec::BinarySubtype, tests::LOCK};
2+
3+
#[test]
4+
fn from_u8() {
5+
let _guard = LOCK.run_concurrently();
6+
// Check the endpoints of the defined, reserved, and user-defined subtype ranges.
7+
assert_eq!(BinarySubtype::from(0x00), BinarySubtype::Generic);
8+
assert_eq!(BinarySubtype::from(0x06), BinarySubtype::Encrypted);
9+
assert_eq!(BinarySubtype::from(0x07), BinarySubtype::Reserved(0x07));
10+
assert_eq!(BinarySubtype::from(0x7F), BinarySubtype::Reserved(0x7F));
11+
assert_eq!(BinarySubtype::from(0x80), BinarySubtype::UserDefined(0x80));
12+
assert_eq!(BinarySubtype::from(0xFF), BinarySubtype::UserDefined(0xFF));
13+
}

src/tests/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
mod binary_subtype;
12
mod modules;
23
mod serde;
34
mod spec;

0 commit comments

Comments
 (0)