Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sync CLI options with SDK options #402

Merged
merged 3 commits into from
Oct 28, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can issuance date be none?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It cannot. Will default to current date and time.

Some(d) => match d.parse::<DateTime<FixedOffset>>() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here it should do the issuance date now()?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The SDK will take care of that. This is just meant to be a passthrough from the CLI to the SDK function.

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
Loading