#[cfg(feature = "network")]
use std::time::Duration;
use wasm_bindgen::prelude::*;
#[derive(Clone, Debug)]
#[wasm_bindgen(getter_with_clone)]
pub struct DIDCacheConfig {
#[cfg(feature = "network")]
pub(crate) service_address: Option<String>,
pub(crate) cache_capacity: u32,
pub(crate) cache_ttl: u32,
#[cfg(feature = "network")]
pub(crate) network_timeout: Duration,
#[cfg(feature = "network")]
pub(crate) network_cache_limit_count: u32,
pub(crate) max_did_parts: usize,
pub(crate) max_did_size_in_bytes: usize,
}
pub struct DIDCacheConfigBuilder {
#[cfg(feature = "network")]
service_address: Option<String>,
cache_capacity: u32,
cache_ttl: u32,
#[cfg(feature = "network")]
network_timeout: u32,
#[cfg(feature = "network")]
network_cache_limit_count: u32,
max_did_parts: usize,
max_did_size_in_bytes: usize,
}
impl Default for DIDCacheConfigBuilder {
fn default() -> Self {
Self {
#[cfg(feature = "network")]
service_address: None,
cache_capacity: 100,
cache_ttl: 300,
#[cfg(feature = "network")]
network_timeout: 5000,
#[cfg(feature = "network")]
network_cache_limit_count: 100,
max_did_parts: 12,
max_did_size_in_bytes: 1_000,
}
}
}
impl DIDCacheConfigBuilder {
#[cfg(feature = "network")]
pub fn with_network_mode(mut self, service_address: &str) -> Self {
self.service_address = Some(service_address.into());
self
}
pub fn with_cache_capacity(mut self, cache_capacity: u32) -> Self {
self.cache_capacity = cache_capacity;
self
}
pub fn with_cache_ttl(mut self, cache_ttl: u32) -> Self {
self.cache_ttl = cache_ttl;
self
}
#[cfg(feature = "network")]
pub fn with_network_timeout(mut self, network_timeout: u32) -> Self {
self.network_timeout = network_timeout;
self
}
#[cfg(feature = "network")]
pub fn with_network_cache_limit_count(mut self, limit_count: u32) -> Self {
self.network_cache_limit_count = limit_count;
self
}
pub fn with_max_did_parts(mut self, max_did_parts: usize) -> Self {
self.max_did_parts = max_did_parts;
self
}
pub fn with_max_did_size_in_bytes(mut self, max_did_size_in_bytes: usize) -> Self {
self.max_did_size_in_bytes = max_did_size_in_bytes;
self
}
pub fn build(self) -> DIDCacheConfig {
DIDCacheConfig {
#[cfg(feature = "network")]
service_address: self.service_address,
cache_capacity: self.cache_capacity,
cache_ttl: self.cache_ttl,
#[cfg(feature = "network")]
network_timeout: Duration::from_millis(self.network_timeout.into()),
#[cfg(feature = "network")]
network_cache_limit_count: self.network_cache_limit_count,
max_did_parts: self.max_did_parts,
max_did_size_in_bytes: self.max_did_size_in_bytes,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn default_config_has_expected_values() {
let config = DIDCacheConfigBuilder::default().build();
assert_eq!(config.cache_capacity, 100);
assert_eq!(config.cache_ttl, 300);
assert_eq!(config.max_did_parts, 12);
assert_eq!(config.max_did_size_in_bytes, 1_000);
}
#[test]
fn builder_overrides_cache_capacity() {
let config = DIDCacheConfigBuilder::default()
.with_cache_capacity(500)
.build();
assert_eq!(config.cache_capacity, 500);
}
#[test]
fn builder_overrides_cache_ttl() {
let config = DIDCacheConfigBuilder::default().with_cache_ttl(60).build();
assert_eq!(config.cache_ttl, 60);
}
#[test]
fn builder_overrides_max_did_parts() {
let config = DIDCacheConfigBuilder::default()
.with_max_did_parts(5)
.build();
assert_eq!(config.max_did_parts, 5);
}
#[test]
fn builder_overrides_max_did_size() {
let config = DIDCacheConfigBuilder::default()
.with_max_did_size_in_bytes(2_000)
.build();
assert_eq!(config.max_did_size_in_bytes, 2_000);
}
#[test]
fn builder_chaining_works() {
let config = DIDCacheConfigBuilder::default()
.with_cache_capacity(200)
.with_cache_ttl(120)
.with_max_did_parts(8)
.with_max_did_size_in_bytes(500)
.build();
assert_eq!(config.cache_capacity, 200);
assert_eq!(config.cache_ttl, 120);
assert_eq!(config.max_did_parts, 8);
assert_eq!(config.max_did_size_in_bytes, 500);
}
#[test]
fn config_is_cloneable() {
let config = DIDCacheConfigBuilder::default().build();
let cloned = config.clone();
assert_eq!(config.cache_capacity, cloned.cache_capacity);
assert_eq!(config.cache_ttl, cloned.cache_ttl);
assert_eq!(config.max_did_parts, cloned.max_did_parts);
assert_eq!(config.max_did_size_in_bytes, cloned.max_did_size_in_bytes);
}
}