hoprd_inbox/
config.rs

1use std::time::Duration;
2
3use serde::{Deserialize, Serialize};
4use serde_with::{serde_as, DurationSeconds};
5use validator::{Validate, ValidationError};
6
7use hopr_internal_types::prelude::*;
8
9pub fn validate_is_power_of_two(value: u32) -> Result<(), ValidationError> {
10    if value > 0 && (value & (value - 1)) == 0 {
11        Ok(())
12    } else {
13        Err(ValidationError::new("The value is not power of 2"))
14    }
15}
16
17/// Holds basic configuration parameters of the `MessageInbox`.
18#[serde_as]
19#[derive(Debug, Clone, Eq, PartialEq, smart_default::SmartDefault, Validate, Serialize, Deserialize)]
20#[serde(deny_unknown_fields)]
21pub struct MessageInboxConfiguration {
22    /// Maximum capacity per-each application tag.
23    ///
24    /// In the current implementation, the capacity must be a power of two.
25    ///
26    /// Defaults to 512.
27    #[validate(custom(function = "validate_is_power_of_two"))]
28    #[serde(default = "default_capacity")]
29    #[default(default_capacity())]
30    pub capacity: u32,
31    /// Maximum age of a message held in the inbox until it is purged.
32    ///
33    /// Defaults to 15 minutes.
34    #[serde_as(as = "DurationSeconds<u64>")]
35    #[serde(default = "just_15_minutes")]
36    #[default(just_15_minutes())]
37    pub max_age: Duration,
38    /// List of tags that are excluded on `push`.
39    ///
40    /// Defaults to \[[DEFAULT_APPLICATION_TAG]\]
41    #[serde(default = "default_excluded_tags")]
42    #[default(default_excluded_tags())]
43    pub excluded_tags: Vec<Tag>,
44}
45
46#[inline]
47fn just_15_minutes() -> Duration {
48    const RAW_15_MINUTES: Duration = Duration::from_secs(15 * 60);
49    RAW_15_MINUTES
50}
51
52#[inline]
53fn default_capacity() -> u32 {
54    512
55}
56
57#[inline]
58fn default_excluded_tags() -> Vec<Tag> {
59    vec![DEFAULT_APPLICATION_TAG]
60}