1313
1414use crate :: auth:: Auth ;
1515use crate :: connector;
16+ use crate :: error:: Error ;
1617use crate :: { Client , ConnectError , Event , ToServerAddrs } ;
1718use base64:: engine:: general_purpose:: URL_SAFE_NO_PAD ;
1819use base64:: engine:: Engine ;
1920use futures:: Future ;
20- use std:: fmt:: Formatter ;
21+ use std:: fmt:: { Display , Formatter } ;
2122use std:: {
2223 fmt,
2324 path:: { Path , PathBuf } ,
@@ -337,7 +338,7 @@ impl ConnectOptions {
337338 /// let jwt = load_jwt().await?;
338339 /// let nc = async_nats::ConnectOptions::with_jwt(jwt, move |nonce| {
339340 /// let key_pair = key_pair.clone();
340- /// async move { key_pair.sign(&nonce).map_err(async_nats::AuthError::new) }
341+ /// async move { key_pair.sign(&nonce) }
341342 /// })
342343 /// .connect("localhost")
343344 /// .await?;
@@ -371,7 +372,7 @@ impl ConnectOptions {
371372 /// let nc = async_nats::ConnectOptions::new()
372373 /// .jwt(jwt, move |nonce| {
373374 /// let key_pair = key_pair.clone();
374- /// async move { key_pair.sign(&nonce).map_err(async_nats::AuthError::new) }
375+ /// async move { key_pair.sign(&nonce) }
375376 /// })
376377 /// .connect("localhost")
377378 /// .await?;
@@ -390,7 +391,7 @@ impl ConnectOptions {
390391 Box :: pin ( async move {
391392 let sig = sign_cb ( nonce. as_bytes ( ) . to_vec ( ) )
392393 . await
393- . map_err ( AuthError :: new) ?;
394+ . map_err ( |_| AuthError :: new ( AuthErrorKind :: InvalidSignature ) ) ?;
394395 Ok ( URL_SAFE_NO_PAD . encode ( sig) )
395396 } )
396397 } ) ) ;
@@ -506,7 +507,11 @@ impl ConnectOptions {
506507
507508 Ok ( self . jwt ( jwt. to_owned ( ) , move |nonce| {
508509 let key_pair = key_pair. clone ( ) ;
509- async move { key_pair. sign ( & nonce) . map_err ( AuthError :: new) }
510+ async move {
511+ key_pair
512+ . sign ( & nonce)
513+ . map_err ( |_| AuthErrorKind :: InvalidKeyPair . into ( ) )
514+ }
510515 } ) )
511516 }
512517
@@ -899,27 +904,20 @@ impl<A, T> fmt::Debug for CallbackArg1<A, T> {
899904 }
900905}
901906
902- /// Error report from signing callback.
903- // This was needed because std::io::Error isn't Send.
904- #[ derive( Clone , PartialEq ) ]
905- pub struct AuthError ( String ) ;
906-
907- impl AuthError {
908- pub fn new ( s : impl ToString ) -> Self {
909- Self ( s. to_string ( ) )
910- }
907+ #[ derive( Clone , Copy , Debug , PartialEq ) ]
908+ pub enum AuthErrorKind {
909+ InvalidKeyPair ,
910+ InvalidSignature ,
911911}
912912
913- impl std :: fmt :: Display for AuthError {
913+ impl Display for AuthErrorKind {
914914 fn fmt ( & self , f : & mut Formatter < ' _ > ) -> fmt:: Result {
915- f. write_str ( & format ! ( "AuthError({})" , & self . 0 ) )
916- }
917- }
918-
919- impl std:: fmt:: Debug for AuthError {
920- fn fmt ( & self , f : & mut Formatter < ' _ > ) -> fmt:: Result {
921- f. write_str ( & format ! ( "AuthError({})" , & self . 0 ) )
915+ match self {
916+ Self :: InvalidKeyPair => f. write_str ( "invalid keypair" ) ,
917+ Self :: InvalidSignature => f. write_str ( "invalid signature" ) ,
918+ }
922919 }
923920}
924921
925- impl std:: error:: Error for AuthError { }
922+ /// Error report from signing callback.
923+ pub type AuthError = Error < AuthErrorKind > ;
0 commit comments