use std::time::Duration;
use serde::{Deserialize, Serialize};
use serde_with::{serde_as, DurationSeconds};
use validator::{Validate, ValidationError};
use hopr_internal_types::prelude::*;
pub fn validate_is_power_of_two(value: u32) -> Result<(), ValidationError> {
if value > 0 && (value & (value - 1)) == 0 {
Ok(())
} else {
Err(ValidationError::new("The value is not power of 2"))
}
}
#[serde_as]
#[derive(Debug, Clone, Eq, PartialEq, smart_default::SmartDefault, Validate, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct MessageInboxConfiguration {
#[validate(custom(function = "validate_is_power_of_two"))]
#[serde(default = "default_capacity")]
#[default(default_capacity())]
pub capacity: u32,
#[serde_as(as = "DurationSeconds<u64>")]
#[serde(default = "just_15_minutes")]
#[default(just_15_minutes())]
pub max_age: Duration,
#[serde(default = "default_excluded_tags")]
#[default(default_excluded_tags())]
pub excluded_tags: Vec<Tag>,
}
#[inline]
fn just_15_minutes() -> Duration {
const RAW_15_MINUTES: Duration = Duration::from_secs(15 * 60);
RAW_15_MINUTES
}
#[inline]
fn default_capacity() -> u32 {
512
}
#[inline]
fn default_excluded_tags() -> Vec<Tag> {
vec![DEFAULT_APPLICATION_TAG]
}