Skip to content

Commit

Permalink
chore(example): improve example output and docs
Browse files Browse the repository at this point in the history
  • Loading branch information
noandrea committed Sep 18, 2022
1 parent d88c339 commit aa3b2f0
Showing 1 changed file with 51 additions and 63 deletions.
114 changes: 51 additions & 63 deletions examples/main.rs
Original file line number Diff line number Diff line change
@@ -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!");

Expand All @@ -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 {
Expand Down Expand Up @@ -109,3 +61,39 @@ fn create_credential() -> VerifableCredential {
}"#;
serde_json::from_str::<VerifableCredential>(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(),
)
}
}

0 comments on commit aa3b2f0

Please sign in to comment.