bee_tangle/
config.rs

1// Copyright 2020-2021 IOTA Stiftung
2// SPDX-License-Identifier: Apache-2.0
3
4use serde::Deserialize;
5
6use std::num::NonZeroUsize;
7
8const DEFAULT_BELOW_MAX_DEPTH: u32 = 15;
9// SAFETY: initialised with a non-zero value.
10const DEFAULT_NUM_PARTITIONS: NonZeroUsize = unsafe { NonZeroUsize::new_unchecked(16) };
11const DEFAULT_MAX_EVICTION_RETRIES: usize = 10;
12
13/// A builder type for a tangle configuration.
14#[derive(Default, Deserialize)]
15pub struct TangleConfigBuilder {
16    below_max_depth: Option<u32>,
17    num_partitions: Option<NonZeroUsize>,
18    max_eviction_retries: Option<usize>,
19}
20
21impl TangleConfigBuilder {
22    /// Create a new [`TangleConfigBuilder`].
23    pub fn new() -> Self {
24        Self::default()
25    }
26
27    /// Finish building tangle configuration, to create a [`TangleConfig`].
28    pub fn finish(self) -> TangleConfig {
29        TangleConfig {
30            below_max_depth: self.below_max_depth.unwrap_or(DEFAULT_BELOW_MAX_DEPTH),
31            num_partitions: self.num_partitions.unwrap_or(DEFAULT_NUM_PARTITIONS),
32            max_eviction_retries: self.max_eviction_retries.unwrap_or(DEFAULT_MAX_EVICTION_RETRIES),
33        }
34    }
35}
36
37/// The configuration state of a tangle.
38#[derive(Clone)]
39pub struct TangleConfig {
40    below_max_depth: u32,
41    num_partitions: NonZeroUsize,
42    max_eviction_retries: usize,
43}
44
45impl TangleConfig {
46    /// Begin building a new [`TangleConfig`].
47    pub fn build() -> TangleConfigBuilder {
48        TangleConfigBuilder::new()
49    }
50
51    /// Get the value of `below_max_depth`.
52    pub fn below_max_depth(&self) -> u32 {
53        self.below_max_depth
54    }
55
56    /// Get the value of `num_partitions`.
57    pub fn num_partitions(&self) -> NonZeroUsize {
58        self.num_partitions
59    }
60
61    /// Get the value of `max_eviction_retries`.
62    pub fn max_eviction_retries(&self) -> usize {
63        self.max_eviction_retries
64    }
65}