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 } ,
@@ -340,7 +341,7 @@ impl ConnectOptions {
340341 /// let jwt = load_jwt().await?;
341342 /// let nc = async_nats::ConnectOptions::with_jwt(jwt, move |nonce| {
342343 /// let key_pair = key_pair.clone();
343- /// async move { key_pair.sign(&nonce).map_err(async_nats::AuthError::new) }
344+ /// async move { key_pair.sign(&nonce) }
344345 /// })
345346 /// .connect("localhost")
346347 /// .await?;
@@ -374,7 +375,7 @@ impl ConnectOptions {
374375 /// let nc = async_nats::ConnectOptions::new()
375376 /// .jwt(jwt, move |nonce| {
376377 /// let key_pair = key_pair.clone();
377- /// async move { key_pair.sign(&nonce).map_err(async_nats::AuthError::new) }
378+ /// async move { key_pair.sign(&nonce) }
378379 /// })
379380 /// .connect("localhost")
380381 /// .await?;
@@ -393,7 +394,7 @@ impl ConnectOptions {
393394 Box :: pin ( async move {
394395 let sig = sign_cb ( nonce. as_bytes ( ) . to_vec ( ) )
395396 . await
396- . map_err ( AuthError :: new) ?;
397+ . map_err ( |_| AuthError :: new ( AuthErrorKind :: InvalidSignature ) ) ?;
397398 Ok ( URL_SAFE_NO_PAD . encode ( sig) )
398399 } )
399400 } ) ) ;
@@ -509,7 +510,11 @@ impl ConnectOptions {
509510
510511 Ok ( self . jwt ( jwt. to_owned ( ) , move |nonce| {
511512 let key_pair = key_pair. clone ( ) ;
512- async move { key_pair. sign ( & nonce) . map_err ( AuthError :: new) }
513+ async move {
514+ key_pair
515+ . sign ( & nonce)
516+ . map_err ( |_| AuthErrorKind :: InvalidKeyPair . into ( ) )
517+ }
513518 } ) )
514519 }
515520
@@ -923,27 +928,20 @@ impl<A, T> fmt::Debug for CallbackArg1<A, T> {
923928 }
924929}
925930
926- /// Error report from signing callback.
927- // This was needed because std::io::Error isn't Send.
928- #[ derive( Clone , PartialEq ) ]
929- pub struct AuthError ( String ) ;
930-
931- impl AuthError {
932- pub fn new ( s : impl ToString ) -> Self {
933- Self ( s. to_string ( ) )
934- }
931+ #[ derive( Clone , Copy , Debug , PartialEq ) ]
932+ pub enum AuthErrorKind {
933+ InvalidKeyPair ,
934+ InvalidSignature ,
935935}
936936
937- impl std :: fmt :: Display for AuthError {
937+ impl Display for AuthErrorKind {
938938 fn fmt ( & self , f : & mut Formatter < ' _ > ) -> fmt:: Result {
939- f. write_str ( & format ! ( "AuthError({})" , & self . 0 ) )
940- }
941- }
942-
943- impl std:: fmt:: Debug for AuthError {
944- fn fmt ( & self , f : & mut Formatter < ' _ > ) -> fmt:: Result {
945- f. write_str ( & format ! ( "AuthError({})" , & self . 0 ) )
939+ match self {
940+ Self :: InvalidKeyPair => f. write_str ( "invalid keypair" ) ,
941+ Self :: InvalidSignature => f. write_str ( "invalid signature" ) ,
942+ }
946943 }
947944}
948945
949- impl std:: error:: Error for AuthError { }
946+ /// Error report from signing callback.
947+ pub type AuthError = Error < AuthErrorKind > ;
0 commit comments