@@ -142,7 +142,13 @@ use std::{
142142
143143use serde:: { Deserialize , Serialize } ;
144144
145- use crate :: { de:: BsonVisitor , spec:: BinarySubtype , Binary , Bson } ;
145+ use crate :: {
146+ de:: BsonVisitor ,
147+ error:: { Error , Result } ,
148+ spec:: BinarySubtype ,
149+ Binary ,
150+ Bson ,
151+ } ;
146152
147153/// Special type name used in the [`Uuid`] serialization implementation to indicate a BSON
148154/// UUID is being serialized or deserialized. The BSON serializers/deserializers will handle this
@@ -189,9 +195,7 @@ impl Uuid {
189195
190196 /// Creates a [`Uuid`] from the provided hex string.
191197 pub fn parse_str ( input : impl AsRef < str > ) -> Result < Self > {
192- let uuid = uuid:: Uuid :: parse_str ( input. as_ref ( ) ) . map_err ( |e| Error :: InvalidUuidString {
193- message : e. to_string ( ) ,
194- } ) ?;
198+ let uuid = uuid:: Uuid :: parse_str ( input. as_ref ( ) ) . map_err ( Error :: invalid_uuid_string) ?;
195199 Ok ( Self :: from_external_uuid ( uuid) )
196200 }
197201
@@ -394,25 +398,23 @@ impl Binary {
394398 pub fn to_uuid_with_representation ( & self , rep : UuidRepresentation ) -> Result < Uuid > {
395399 // If representation is non-standard, then its subtype must be UuidOld
396400 if rep != UuidRepresentation :: Standard && self . subtype != BinarySubtype :: UuidOld {
397- return Err ( Error :: RepresentationMismatch {
398- requested_representation : rep,
399- actual_binary_subtype : self . subtype ,
400- expected_binary_subtype : BinarySubtype :: UuidOld ,
401- } ) ;
401+ return Err ( Error :: uuid_representation_mismatch (
402+ rep,
403+ self . subtype ,
404+ BinarySubtype :: UuidOld ,
405+ ) ) ;
402406 }
403407 // If representation is standard, then its subtype must be Uuid
404408 if rep == UuidRepresentation :: Standard && self . subtype != BinarySubtype :: Uuid {
405- return Err ( Error :: RepresentationMismatch {
406- requested_representation : rep,
407- actual_binary_subtype : self . subtype ,
408- expected_binary_subtype : BinarySubtype :: Uuid ,
409- } ) ;
409+ return Err ( Error :: uuid_representation_mismatch (
410+ rep,
411+ self . subtype ,
412+ BinarySubtype :: UuidOld ,
413+ ) ) ;
410414 }
411415 // Must be 16 bytes long
412416 if self . bytes . len ( ) != 16 {
413- return Err ( Error :: InvalidLength {
414- length : self . bytes . len ( ) ,
415- } ) ;
417+ return Err ( Error :: invalid_uuid_length ( self . bytes . len ( ) ) ) ;
416418 }
417419 let mut buf = [ 0u8 ; 16 ] ;
418420 buf. copy_from_slice ( & self . bytes ) ;
@@ -479,66 +481,3 @@ macro_rules! trait_impls {
479481 } ;
480482}
481483trait_impls ! ( feature = "uuid-1" , uuid:: Uuid ) ;
482-
483- /// Errors that can occur during [`Uuid`] construction and generation.
484- #[ derive( Clone , Debug ) ]
485- #[ non_exhaustive]
486- pub enum Error {
487- /// Error returned when an invalid string is provided to [`Uuid::parse_str`].
488- #[ non_exhaustive]
489- InvalidUuidString { message : String } ,
490-
491- /// Error returned when the representation specified does not match the underlying
492- /// [`crate::Binary`] value in [`crate::Binary::to_uuid_with_representation`].
493- #[ non_exhaustive]
494- RepresentationMismatch {
495- /// The subtype that was expected given the requested representation.
496- expected_binary_subtype : BinarySubtype ,
497-
498- /// The actual subtype of the binary value.
499- actual_binary_subtype : BinarySubtype ,
500-
501- /// The requested representation.
502- requested_representation : UuidRepresentation ,
503- } ,
504-
505- /// Error returned from [`crate::Binary::to_uuid`] if the underling data is not 16 bytes long.
506- #[ non_exhaustive]
507- InvalidLength {
508- /// The actual length of the data.
509- length : usize ,
510- } ,
511- }
512-
513- /// Alias for `Result<T, bson::uuid::Error>`.
514- pub type Result < T > = std:: result:: Result < T , Error > ;
515-
516- impl fmt:: Display for Error {
517- fn fmt ( & self , fmt : & mut fmt:: Formatter ) -> fmt:: Result {
518- match self {
519- Error :: InvalidUuidString { message } => {
520- write ! ( fmt, "{}" , message)
521- }
522- Error :: RepresentationMismatch {
523- expected_binary_subtype,
524- actual_binary_subtype,
525- requested_representation,
526- } => {
527- write ! (
528- fmt,
529- "expected {:?} when converting to UUID with {:?}, isntead got {:?}" ,
530- expected_binary_subtype, requested_representation, actual_binary_subtype
531- )
532- }
533- Error :: InvalidLength { length } => {
534- write ! (
535- fmt,
536- "expected UUID to contain 16 bytes, instead got {}" ,
537- length
538- )
539- }
540- }
541- }
542- }
543-
544- impl std:: error:: Error for Error { }
0 commit comments