Skip to content

Commit 98798d0

Browse files
committed
RUST-789 Use more standard conversion constuctors for ObjectId
1 parent a47e426 commit 98798d0

File tree

12 files changed

+33
-27
lines changed

12 files changed

+33
-27
lines changed

examples/serialize.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ fn main() {
99
let arr = vec![
1010
Bson::String("blah".to_string()),
1111
Bson::DateTime(chrono::Utc::now()),
12-
Bson::ObjectId(oid::ObjectId::with_bytes([
12+
Bson::ObjectId(oid::ObjectId::from_bytes([
1313
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
1414
])),
1515
];

src/bson.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ impl From<u64> for Bson {
278278

279279
impl From<[u8; 12]> for Bson {
280280
fn from(a: [u8; 12]) -> Bson {
281-
Bson::ObjectId(oid::ObjectId::with_bytes(a))
281+
Bson::ObjectId(oid::ObjectId::from_bytes(a))
282282
}
283283
}
284284

@@ -607,7 +607,7 @@ impl Bson {
607607
match keys.as_slice() {
608608
["$oid"] => {
609609
if let Ok(oid) = doc.get_str("$oid") {
610-
if let Ok(oid) = ObjectId::with_string(oid) {
610+
if let Ok(oid) = ObjectId::parse_str(oid) {
611611
return Bson::ObjectId(oid);
612612
}
613613
}

src/de/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ pub(crate) fn deserialize_bson_kvp<R: Read + ?Sized>(
263263
for x in &mut objid {
264264
*x = read_u8(reader)?;
265265
}
266-
Bson::ObjectId(oid::ObjectId::with_bytes(objid))
266+
Bson::ObjectId(oid::ObjectId::from_bytes(objid))
267267
}
268268
Some(ElementType::Boolean) => {
269269
let val = read_u8(reader)?;
@@ -345,7 +345,7 @@ pub(crate) fn deserialize_bson_kvp<R: Read + ?Sized>(
345345
reader.read_exact(&mut objid)?;
346346
Bson::DbPointer(DbPointer {
347347
namespace,
348-
id: oid::ObjectId::with_bytes(objid),
348+
id: oid::ObjectId::from_bytes(objid),
349349
})
350350
}
351351
Some(ElementType::MaxKey) => Bson::MaxKey,

src/de/serde.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ impl<'de> Deserialize<'de> for ObjectId {
3333
deserializer
3434
.deserialize_any(BsonVisitor)
3535
.and_then(|bson| match bson {
36-
Bson::String(oid) => ObjectId::with_string(&oid).map_err(de::Error::custom),
36+
Bson::String(oid) => ObjectId::parse_str(&oid).map_err(de::Error::custom),
3737
Bson::ObjectId(oid) => Ok(oid),
3838
_ => {
3939
let err = format!(

src/extjson/models.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ pub(crate) struct ObjectId {
8181

8282
impl ObjectId {
8383
pub(crate) fn parse(self) -> extjson::de::Result<oid::ObjectId> {
84-
let oid = oid::ObjectId::with_string(self.oid.as_str())?;
84+
let oid = oid::ObjectId::parse_str(self.oid.as_str())?;
8585
Ok(oid)
8686
}
8787
}

src/oid.rs

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,13 @@ impl FromStr for ObjectId {
8484
type Err = Error;
8585

8686
fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
87-
Self::with_string(s)
87+
Self::parse_str(s)
88+
}
89+
}
90+
91+
impl From<[u8; 12]> for ObjectId {
92+
fn from(bytes: [u8; 12]) -> Self {
93+
Self { id: bytes }
8894
}
8995
}
9096

@@ -105,25 +111,25 @@ impl ObjectId {
105111
buf[COUNTER_OFFSET..(COUNTER_SIZE + COUNTER_OFFSET)]
106112
.clone_from_slice(&counter[..COUNTER_SIZE]);
107113

108-
ObjectId::with_bytes(buf)
114+
ObjectId::from_bytes(buf)
109115
}
110116

111117
/// Constructs a new ObjectId wrapper around the raw byte representation.
112-
pub fn with_bytes(bytes: [u8; 12]) -> ObjectId {
118+
pub const fn from_bytes(bytes: [u8; 12]) -> ObjectId {
113119
ObjectId { id: bytes }
114120
}
115121

116122
/// Creates an ObjectID using a 12-byte (24-char) hexadecimal string.
117-
pub fn with_string(s: &str) -> Result<ObjectId> {
118-
let bytes: Vec<u8> = hex::decode(s.as_bytes())?;
123+
pub fn parse_str(s: impl AsRef<str>) -> Result<ObjectId> {
124+
let bytes: Vec<u8> = hex::decode(s.as_ref().as_bytes())?;
119125
if bytes.len() != 12 {
120126
Err(Error::ArgumentError {
121127
message: "Provided string must be a 12-byte hexadecimal string.".to_owned(),
122128
})
123129
} else {
124130
let mut byte_array: [u8; 12] = [0; 12];
125131
byte_array[..].copy_from_slice(&bytes[..]);
126-
Ok(ObjectId::with_bytes(byte_array))
132+
Ok(ObjectId::from_bytes(byte_array))
127133
}
128134
}
129135

@@ -268,33 +274,33 @@ mod test {
268274

269275
#[test]
270276
fn test_display() {
271-
let id = super::ObjectId::with_string("53e37d08776f724e42000000").unwrap();
277+
let id = super::ObjectId::parse_str("53e37d08776f724e42000000").unwrap();
272278

273279
assert_eq!(format!("{}", id), "53e37d08776f724e42000000")
274280
}
275281

276282
#[test]
277283
fn test_debug() {
278-
let id = super::ObjectId::with_string("53e37d08776f724e42000000").unwrap();
284+
let id = super::ObjectId::parse_str("53e37d08776f724e42000000").unwrap();
279285

280286
assert_eq!(format!("{:?}", id), "ObjectId(53e37d08776f724e42000000)")
281287
}
282288

283289
#[test]
284290
fn test_timestamp() {
285-
let id = super::ObjectId::with_string("000000000000000000000000").unwrap();
291+
let id = super::ObjectId::parse_str("000000000000000000000000").unwrap();
286292
// "Jan 1st, 1970 00:00:00 UTC"
287293
assert_eq!(Utc.ymd(1970, 1, 1).and_hms(0, 0, 0), id.timestamp());
288294

289-
let id = super::ObjectId::with_string("7FFFFFFF0000000000000000").unwrap();
295+
let id = super::ObjectId::parse_str("7FFFFFFF0000000000000000").unwrap();
290296
// "Jan 19th, 2038 03:14:07 UTC"
291297
assert_eq!(Utc.ymd(2038, 1, 19).and_hms(3, 14, 7), id.timestamp());
292298

293-
let id = super::ObjectId::with_string("800000000000000000000000").unwrap();
299+
let id = super::ObjectId::parse_str("800000000000000000000000").unwrap();
294300
// "Jan 19th, 2038 03:14:08 UTC"
295301
assert_eq!(Utc.ymd(2038, 1, 19).and_hms(3, 14, 8), id.timestamp());
296302

297-
let id = super::ObjectId::with_string("FFFFFFFF0000000000000000").unwrap();
303+
let id = super::ObjectId::parse_str("FFFFFFFF0000000000000000").unwrap();
298304
// "Feb 7th, 2106 06:28:15 UTC"
299305
assert_eq!(Utc.ymd(2106, 2, 7).and_hms(6, 28, 15), id.timestamp());
300306
}

src/serde_helpers.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ pub mod hex_string_as_object_id {
279279

280280
/// Serializes a hex string as an ObjectId.
281281
pub fn serialize<S: Serializer>(val: &str, serializer: S) -> Result<S::Ok, S::Error> {
282-
match ObjectId::with_string(val) {
282+
match ObjectId::parse_str(val) {
283283
Ok(oid) => oid.serialize(serializer),
284284
Err(_) => Err(ser::Error::custom(format!(
285285
"cannot convert {} to ObjectId",

src/tests/modules/bson.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ fn to_json() {
2020
let mut doc = Document::new();
2121
doc.insert(
2222
"_id",
23-
Bson::ObjectId(ObjectId::with_bytes(*b"abcdefghijkl")),
23+
Bson::ObjectId(ObjectId::from_bytes(*b"abcdefghijkl")),
2424
);
2525
doc.insert("first", Bson::Int32(1));
2626
doc.insert("second", Bson::String("foo".to_owned()));
@@ -115,7 +115,7 @@ fn from_impls() {
115115
let oid = ObjectId::new();
116116
assert_eq!(
117117
Bson::from(b"abcdefghijkl"),
118-
Bson::ObjectId(ObjectId::with_bytes(*b"abcdefghijkl"))
118+
Bson::ObjectId(ObjectId::from_bytes(*b"abcdefghijkl"))
119119
);
120120
assert_eq!(Bson::from(oid), Bson::ObjectId(oid));
121121
assert_eq!(

src/tests/modules/macros.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ fn standard_format() {
1010
let mut bytes = [0; 12];
1111
bytes[..12].clone_from_slice(&string_bytes[..12]);
1212

13-
let id = ObjectId::with_bytes(bytes);
13+
let id = ObjectId::from_bytes(bytes);
1414
let date = Utc::now();
1515

1616
let doc = doc! {

src/tests/modules/oid.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::{oid::ObjectId, tests::LOCK};
44
fn string_oid() {
55
let _guard = LOCK.run_concurrently();
66
let s = "123456789012123456789012";
7-
let oid_res = ObjectId::with_string(s);
7+
let oid_res = ObjectId::parse_str(s);
88
assert!(oid_res.is_ok());
99
let actual_s = hex::encode(oid_res.unwrap().bytes());
1010
assert_eq!(s.to_owned(), actual_s);
@@ -14,7 +14,7 @@ fn string_oid() {
1414
fn byte_string_oid() {
1515
let _guard = LOCK.run_concurrently();
1616
let s = "541b1a00e8a23afa832b218e";
17-
let oid_res = ObjectId::with_string(s);
17+
let oid_res = ObjectId::parse_str(s);
1818
assert!(oid_res.is_ok());
1919
let oid = oid_res.unwrap();
2020
let bytes: [u8; 12] = [

0 commit comments

Comments
 (0)