use chrono::{DateTime, Utc};
use crate::crypto_suites::CryptoSuite;
#[derive(Clone, Debug, Default)]
#[non_exhaustive]
pub struct SignOptions {
pub context: Option<Vec<String>>,
pub created: Option<DateTime<Utc>>,
pub cryptosuite: Option<CryptoSuite>,
pub proof_purpose: Option<String>,
}
impl SignOptions {
#[must_use = "constructed options must be passed to sign/verify to take effect"]
pub fn new() -> Self {
Self::default()
}
#[must_use = "chained builder call returns self; assign or chain further"]
pub fn with_context(mut self, context: Vec<String>) -> Self {
self.context = Some(context);
self
}
#[must_use = "chained builder call returns self; assign or chain further"]
pub fn with_created(mut self, created: DateTime<Utc>) -> Self {
self.created = Some(created);
self
}
#[must_use = "chained builder call returns self; assign or chain further"]
pub fn with_cryptosuite(mut self, suite: CryptoSuite) -> Self {
self.cryptosuite = Some(suite);
self
}
#[must_use = "chained builder call returns self; assign or chain further"]
pub fn with_proof_purpose(mut self, purpose: impl Into<String>) -> Self {
self.proof_purpose = Some(purpose.into());
self
}
}
#[derive(Clone, Debug, Default)]
#[non_exhaustive]
pub struct VerifyOptions {
pub expected_context: Option<Vec<String>>,
pub allowed_suites: Vec<CryptoSuite>,
}
impl VerifyOptions {
#[must_use = "constructed options must be passed to sign/verify to take effect"]
pub fn new() -> Self {
Self::default()
}
#[must_use = "chained builder call returns self; assign or chain further"]
pub fn with_expected_context(mut self, ctx: Vec<String>) -> Self {
self.expected_context = Some(ctx);
self
}
#[must_use = "chained builder call returns self; assign or chain further"]
pub fn with_allowed_suites(mut self, suites: Vec<CryptoSuite>) -> Self {
self.allowed_suites = suites;
self
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn sign_options_builder_chains() {
let opts = SignOptions::new()
.with_context(vec!["https://example/ctx".into()])
.with_proof_purpose("authentication");
assert_eq!(
opts.context.as_deref(),
Some(&["https://example/ctx".to_string()][..])
);
assert_eq!(opts.proof_purpose.as_deref(), Some("authentication"));
assert!(opts.created.is_none());
}
#[test]
fn verify_options_builder_chains() {
let opts = VerifyOptions::new()
.with_expected_context(vec!["a".into()])
.with_allowed_suites(vec![]);
assert_eq!(
opts.expected_context.as_deref(),
Some(&["a".to_string()][..])
);
assert!(opts.allowed_suites.is_empty());
}
}