From aa3b2f099c6faa9f2ad45618cc3b0d4ae6bc0f4e Mon Sep 17 00:00:00 2001 From: Andrea Giacobino Date: Sun, 18 Sep 2022 11:21:18 +0200 Subject: [PATCH] chore(example): improve example output and docs --- examples/main.rs | 114 +++++++++++++++++++++-------------------------- 1 file changed, 51 insertions(+), 63 deletions(-) diff --git a/examples/main.rs b/examples/main.rs index 24dde83..0310082 100644 --- a/examples/main.rs +++ b/examples/main.rs @@ -1,42 +1,6 @@ use rl2020::{CredentialStatus, RevocationList2020}; use serde_derive::{Deserialize, Serialize}; -#[derive(Debug, Clone, Serialize, Deserialize)] -pub struct VerifableCredential { - #[serde(rename = "credentialStatus")] - credential_status: CredentialStatusExample, -} - -#[derive(Debug, Clone, Serialize, Deserialize)] -pub struct CredentialStatusExample { - #[serde(rename = "id")] - id: String, - #[serde(rename = "type")] - typ: String, - #[serde(rename = "revocationListIndex")] - revocation_list_index: u64, - #[serde(rename = "revocationListCredential")] - revocation_list_credential: String, -} - -/// To be able to use the rl2020 library it's neccessary -/// to implement the CredentialStatus trait -impl CredentialStatus for VerifableCredential { - fn coordinates(&self) -> (String, u64) { - ( - self.credential_status.revocation_list_credential.to_owned(), - self.credential_status.revocation_list_index.to_owned(), - ) - } - - fn type_def(&self) -> (String, String) { - ( - self.credential_status.id.to_owned(), - self.credential_status.typ.to_owned(), - ) - } -} - fn main() { println!("Hello, RevocationList2020!"); @@ -46,39 +10,27 @@ fn main() { // create a credential that uses the revocation list let c = create_credential(); + let c_idx = c.credential_status.revocation_list_index; + // check if the credential is revoked let revoked = rl.is_revoked(&c).unwrap(); - println!( - "credential with id {} is revoked? {}", - c.credential_status.revocation_list_index, revoked - ); // prints false - - println!( - "revoking credential with index {}", - c.credential_status.revocation_list_index - ); + println!("credential at index {} is revoked? {}", c_idx, revoked); + // revoke the credential + println!("revoking credential at index {}", c_idx); rl.revoke(&c).unwrap(); - let revoked = rl.is_revoked(&c).unwrap(); + // print the updated revocation list println!("{}", rl); - - println!( - "credential with id {} is revoked? {}", - c.credential_status.revocation_list_index, revoked - ); // prints true - - println!( - "resetting status for credential with index {}", - c.credential_status.revocation_list_index - ); - - rl.reset(&c).unwrap(); + //check if the credential is revoked let revoked = rl.is_revoked(&c).unwrap(); + println!("credential at index {} is revoked? {}", c_idx, revoked); + // reset the credential revocation + println!("resetting status for credential at index {}", c_idx); + rl.reset(&c).unwrap(); + // print the updated revocation list println!("{}", rl); - - println!( - "credential with id {} is revoked? {}", - c.credential_status.revocation_list_index, revoked - ); // prints false + //check if the credential is revoked + let revoked = rl.is_revoked(&c).unwrap(); + println!("credential at index {} is revoked? {}", c_idx, revoked); } fn create_credential() -> VerifableCredential { @@ -109,3 +61,39 @@ fn create_credential() -> VerifableCredential { }"#; serde_json::from_str::(data).unwrap() } + +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct VerifableCredential { + #[serde(rename = "credentialStatus")] + credential_status: CredentialStatusExample, +} + +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct CredentialStatusExample { + #[serde(rename = "id")] + id: String, + #[serde(rename = "type")] + typ: String, + #[serde(rename = "revocationListIndex")] + revocation_list_index: u64, + #[serde(rename = "revocationListCredential")] + revocation_list_credential: String, +} + +/// To be able to use the rl2020 library it's neccessary +/// to implement the CredentialStatus trait +impl CredentialStatus for VerifableCredential { + fn coordinates(&self) -> (String, u64) { + ( + self.credential_status.revocation_list_credential.to_owned(), + self.credential_status.revocation_list_index.to_owned(), + ) + } + + fn type_def(&self) -> (String, String) { + ( + self.credential_status.id.to_owned(), + self.credential_status.typ.to_owned(), + ) + } +}