11//! Module containing functionality related to BSON ObjectIds.
22//! For more information, see the documentation for the [`ObjectId`] type.
33
4+ #[ cfg( not( all( target_arch = "wasm32" , target_os = "unknown" ) ) ) ]
5+ use std:: { convert:: TryInto , time:: SystemTime } ;
46use std:: {
5- error,
67 fmt,
7- result,
88 str:: FromStr ,
99 sync:: atomic:: { AtomicUsize , Ordering } ,
1010} ;
1111
12- #[ cfg( not( all( target_arch = "wasm32" , target_os = "unknown" ) ) ) ]
13- use std:: { convert:: TryInto , time:: SystemTime } ;
14-
1512use hex:: { self , FromHexError } ;
1613use once_cell:: sync:: Lazy ;
1714use rand:: { random, rng, Rng } ;
1815
16+ use crate :: error:: { Error , Result } ;
17+
1918const TIMESTAMP_SIZE : usize = 4 ;
2019const PROCESS_ID_SIZE : usize = 5 ;
2120const COUNTER_SIZE : usize = 3 ;
@@ -29,49 +28,6 @@ const MAX_U24: usize = 0xFF_FFFF;
2928static OID_COUNTER : Lazy < AtomicUsize > =
3029 Lazy :: new ( || AtomicUsize :: new ( rng ( ) . random_range ( 0 ..=MAX_U24 ) ) ) ;
3130
32- /// Errors that can occur during [`ObjectId`] construction and generation.
33- #[ derive( Clone , Debug ) ]
34- #[ non_exhaustive]
35- pub enum Error {
36- /// An invalid character was found in the provided hex string. Valid characters are: `0...9`,
37- /// `a...f`, or `A...F`.
38- #[ non_exhaustive]
39- InvalidHexStringCharacter { c : char , index : usize , hex : String } ,
40-
41- /// An [`ObjectId`]'s hex string representation must be an exactly 12-byte (24-char)
42- /// hexadecimal string.
43- #[ non_exhaustive]
44- InvalidHexStringLength { length : usize , hex : String } ,
45- }
46-
47- /// Alias for Result<T, oid::Error>.
48- pub type Result < T > = result:: Result < T , Error > ;
49-
50- impl fmt:: Display for Error {
51- fn fmt ( & self , fmt : & mut fmt:: Formatter ) -> fmt:: Result {
52- match self {
53- Error :: InvalidHexStringCharacter { c, index, hex } => {
54- write ! (
55- fmt,
56- "invalid character '{}' was found at index {} in the provided hex string: \
57- \" {}\" ",
58- c, index, hex
59- )
60- }
61- Error :: InvalidHexStringLength { length, hex } => {
62- write ! (
63- fmt,
64- "provided hex string representation must be exactly 12 bytes, instead got: \
65- \" {}\" , length {}",
66- hex, length
67- )
68- }
69- }
70- }
71- }
72-
73- impl error:: Error for Error { }
74-
7531/// A wrapper around a raw 12-byte ObjectId.
7632///
7733/// ## `serde` integration
@@ -198,24 +154,22 @@ impl ObjectId {
198154 pub fn parse_str ( s : impl AsRef < str > ) -> Result < ObjectId > {
199155 let s = s. as_ref ( ) ;
200156
201- let bytes: Vec < u8 > = hex:: decode ( s. as_bytes ( ) ) . map_err ( |e| match e {
202- FromHexError :: InvalidHexCharacter { c, index } => Error :: InvalidHexStringCharacter {
203- c,
204- index,
205- hex : s. to_string ( ) ,
206- } ,
207- FromHexError :: InvalidStringLength | FromHexError :: OddLength => {
208- Error :: InvalidHexStringLength {
209- length : s. len ( ) ,
210- hex : s. to_string ( ) ,
157+ let bytes: Vec < u8 > = hex:: decode ( s. as_bytes ( ) ) . map_err ( |e| {
158+ let message = match e {
159+ FromHexError :: InvalidHexCharacter { c, index } => {
160+ format ! ( "invalid hex character {c} encountered at index {index}" )
161+ }
162+ FromHexError :: InvalidStringLength | FromHexError :: OddLength => {
163+ format ! ( "invalid hex string length {}" , s. len( ) )
211164 }
212- }
165+ } ;
166+ Error :: invalid_value ( message)
213167 } ) ?;
214168 if bytes. len ( ) != 12 {
215- Err ( Error :: InvalidHexStringLength {
216- length : s . len ( ) ,
217- hex : s . to_string ( ) ,
218- } )
169+ Err ( Error :: invalid_value ( format ! (
170+ "invalid object ID byte vector length {}" ,
171+ bytes . len ( )
172+ ) ) )
219173 } else {
220174 let mut byte_array: [ u8 ; 12 ] = [ 0 ; 12 ] ;
221175 byte_array[ ..] . copy_from_slice ( & bytes[ ..] ) ;
0 commit comments