Skip to content

Commit

Permalink
Sync CLI options with SDK options (#402)
Browse files Browse the repository at this point in the history
* Say hello to more cli opts

* fix lint warning

* Use chrono
  • Loading branch information
andresuribe87 authored Oct 28, 2024
1 parent 2dd3b77 commit f6d347b
Showing 1 changed file with 60 additions and 5 deletions.
65 changes: 60 additions & 5 deletions crates/web5_cli/src/vcs.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use chrono::{DateTime, Utc};
use chrono::{DateTime, FixedOffset};
use clap::Subcommand;
use std::time::SystemTime;
use web5::{
credentials::{
CredentialSubject, Issuer, VerifiableCredential, VerifiableCredentialCreateOptions,
Expand All @@ -10,6 +9,8 @@ use web5::{
};

#[derive(Subcommand, Debug)]
// We allow the warning because memory layout isn't important for the CLI options.
#[allow(clippy::large_enum_variant)]
pub enum Commands {
/// Creates a VC.
///
Expand Down Expand Up @@ -46,6 +47,29 @@ pub enum Commands {
/// If not specified, the VC will not expire
#[arg(long)]
expiration_date: Option<String>,
/// The optional credential status, which may indicate revocation or suspension information.
#[arg(long)]
credential_status: Option<String>,
/// The credential schema, used to validate the data structure of the credential. This is optional.
/// JSON Schema validation is performed if the value is provided, and creation will fail if validation fails.
#[arg(long)]
credential_schema: Option<String>,
/// An optional array of evidence supporting the claims made in the credential.
#[arg(long)]
evidence: Option<String>,
/// The type(s) of the Verifiable Credential.
#[arg(long)]
r#type: Option<Vec<String>>,
/// The context(s) for the Verifiable Credential, which define the meaning of terms within the credential.
#[arg(long)]
context: Option<Vec<String>>,
/// The unique identifier for the Verifiable Credential. This is optional.
#[arg(long)]
id: Option<String>,
/// The issuance date of the credential (in ISO 8601 standard format).
/// If not provided, defaults to the current date and time.
#[arg(long)]
issuance_date: Option<String>,
/// If true, output will be minified
#[arg(long)]
no_indent: bool,
Expand All @@ -70,6 +94,13 @@ impl Commands {
portable_did,
issuer,
expiration_date,
credential_status,
credential_schema,
evidence,
r#type,
context,
id,
issuance_date,
no_indent,
json_escape,
} => {
Expand All @@ -85,13 +116,31 @@ impl Commands {
});
let expiration_date = match expiration_date {
None => None,
Some(d) => match d.parse::<DateTime<Utc>>() {
Ok(datetime) => Some(SystemTime::from(datetime)),
Some(d) => match d.parse::<DateTime<FixedOffset>>() {
Ok(datetime) => Some(datetime.into()),
Err(e) => {
panic!("Error parsing date string: {}", e);
}
},
};
let issuance_date = match issuance_date {
None => None,
Some(d) => match d.parse::<DateTime<FixedOffset>>() {
Ok(datetime) => Some(datetime.into()),
Err(e) => {
panic!("Error parsing date string: {}", e);
}
},
};
let credential_status = credential_status.as_ref().map(|cs| {
serde_json::from_str(cs).expect("Error parsing credential status JSON string")
});
let credential_schema = credential_schema.as_ref().map(|cs| {
serde_json::from_str(cs).expect("Error parsing credential schema JSON string")
});
let evidence = evidence
.as_ref()
.map(|e| serde_json::from_str(e).expect("Error parsing evidence JSON string"));

let vc = VerifiableCredential::create(
issuer,
Expand All @@ -100,8 +149,14 @@ impl Commands {
..Default::default()
},
Some(VerifiableCredentialCreateOptions {
id: id.clone(),
context: context.clone(),
r#type: r#type.clone(),
issuance_date,
expiration_date,
..Default::default()
credential_status,
credential_schema,
evidence,
}),
)
.await
Expand Down

0 comments on commit f6d347b

Please sign in to comment.