Skip to content

Commit d5225db

Browse files
committed
Refactor handle_credential_error to return & propagate error
1 parent 5cda7c9 commit d5225db

File tree

1 file changed

+16
-16
lines changed

1 file changed

+16
-16
lines changed

trustchain-cli/src/bin/main.rs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -133,21 +133,20 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
133133
if mnemonic && file_path.is_some() {
134134
panic!("Please use only one of '--file_path' and '--mnemonic'.")
135135
}
136-
let filename: String;
137-
if !mnemonic {
136+
let filename = if !mnemonic {
138137
// Read doc state from file path
139138
let doc_state = if let Some(file_path) = file_path {
140139
Some(serde_json::from_reader(File::open(file_path)?)?)
141140
} else {
142141
None
143142
};
144-
filename = create_operation(doc_state, verbose)?;
143+
create_operation(doc_state, verbose)?
145144
} else {
146145
let mut mnemonic = String::new();
147146
println!("Enter a mnemonic:");
148147
std::io::stdin().read_line(&mut mnemonic).unwrap();
149-
filename = create_operation_mnemonic(&mnemonic, None)?;
150-
}
148+
create_operation_mnemonic(&mnemonic, None)?
149+
};
151150
println!(
152151
"Created new DID: {}",
153152
filename
@@ -264,7 +263,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
264263
// Handle result
265264
match verify_result {
266265
Err(cred_err) => {
267-
handle_credential_error(&cred_err);
266+
handle_credential_error(cred_err)?;
268267
}
269268
Ok(_) => {
270269
println!("Proof.... ✅");
@@ -357,10 +356,10 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
357356
.await;
358357
// Handle result
359358
match verify_result {
360-
ref _e @ Err(DataCredentialError::CredentialError(ref cred_err)) => {
361-
handle_credential_error(cred_err);
359+
Err(DataCredentialError::CredentialError(cred_err)) => {
360+
handle_credential_error(cred_err)?;
362361
}
363-
_e @ Err(DataCredentialError::MismatchedHashDigests(_, _)) => {
362+
Err(DataCredentialError::MismatchedHashDigests(_, _)) => {
364363
println!("Digest... ❌ (mismatched data hash digests)");
365364
}
366365
Ok(_) => {
@@ -408,28 +407,29 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
408407
Ok(())
409408
}
410409

411-
fn handle_credential_error(err: &CredentialError) {
410+
fn handle_credential_error(err: CredentialError) -> Result<(), CredentialError> {
412411
match err {
413-
_err @ CredentialError::VerificationResultError(_) => {
412+
CredentialError::VerificationResultError(_) => {
414413
println!("Proof... ❌ Invalid");
415414
}
416-
_err @ CredentialError::NoProofPresent => {
415+
CredentialError::NoProofPresent => {
417416
println!("Proof... ❌ (missing proof)");
418417
}
419-
_err @ CredentialError::MissingVerificationMethod => {
418+
CredentialError::MissingVerificationMethod => {
420419
println!("Proof... ❌ (missing verification method)");
421420
}
422-
_err @ CredentialError::NoIssuerPresent => {
421+
CredentialError::NoIssuerPresent => {
423422
println!("Proof.... ✅");
424423
println!("Issuer... ❌ (missing issuer)");
425424
}
426-
_err @ CredentialError::VerifierError(_) => {
425+
CredentialError::VerifierError(_) => {
427426
println!("Proof.... ✅");
428427
println!("Issuer... ❌ (with verifier error)");
429428
}
430-
_err @ CredentialError::FailedToDecodeJWT => {
429+
CredentialError::FailedToDecodeJWT => {
431430
println!("Proof.... ❌");
432431
println!("Issuer... ❌");
433432
}
434433
}
434+
Err(err)
435435
}

0 commit comments

Comments
 (0)