use super::{SnowIDConfig, SnowIDConfigError};
pub(super) const DEFAULT_NODE_BITS: u8 = 10;
pub(super) const DEFAULT_CUSTOM_EPOCH: u64 = 1704067200000; pub(super) const DEFAULT_SPIN_ENABLED: bool = true;
pub(super) const DEFAULT_SPIN_LOOPS: u32 = 64;
pub(super) const DEFAULT_SPIN_YIELD_EVERY: u32 = 16;
#[derive(Debug)]
pub struct SnowIDConfigBuilder {
pub(super) node_bits: u8,
pub(super) custom_epoch: u64,
pub(super) spin_enabled: bool,
pub(super) spin_loops: u32,
pub(super) spin_yield_every: u32,
}
impl SnowIDConfigBuilder {
pub fn new() -> Self {
Self {
node_bits: DEFAULT_NODE_BITS,
custom_epoch: DEFAULT_CUSTOM_EPOCH,
spin_enabled: DEFAULT_SPIN_ENABLED,
spin_loops: DEFAULT_SPIN_LOOPS,
spin_yield_every: DEFAULT_SPIN_YIELD_EVERY,
}
}
pub fn node_bits(mut self, bits: u8) -> Result<Self, SnowIDConfigError> {
if !(6..=16).contains(&bits) {
return Err(SnowIDConfigError::InvalidNodeBits { bits });
}
self.node_bits = bits;
Ok(self)
}
pub const fn epoch(mut self, epoch: u64) -> Self {
self.custom_epoch = epoch;
self
}
pub const fn enable_spin(mut self, enable: bool) -> Self {
self.spin_enabled = enable;
self
}
pub const fn spin_loops(mut self, loops: u32) -> Self {
self.spin_loops = loops;
self
}
pub const fn spin_yield_every(mut self, n: u32) -> Self {
self.spin_yield_every = n;
self
}
pub fn build(self) -> SnowIDConfig {
SnowIDConfig::from_builder(self)
}
}
impl Default for SnowIDConfigBuilder {
fn default() -> Self {
Self::new()
}
}