use serde::{Deserialize, Serialize};
use crate::{
message::ProtocolVersion,
packet::PayloadLength,
pow::{NonceTrialsPerByte, PayloadLengthExtraBytes},
};
#[derive(Clone, PartialEq, Eq, Debug, Serialize, Deserialize)]
pub struct Config {
#[serde(default = "default_protocol_version")]
protocol_version: ProtocolVersion,
#[serde(default = "default_max_payload_length")]
max_payload_length: PayloadLength,
#[serde(default = "default_nonce_trials_per_byte")]
nonce_trials_per_byte: NonceTrialsPerByte,
#[serde(default = "default_payload_length_extra_bytes")]
payload_length_extra_bytes: PayloadLengthExtraBytes,
}
fn default_protocol_version() -> ProtocolVersion {
ProtocolVersion::new(3)
}
fn default_max_payload_length() -> PayloadLength {
PayloadLength::new(1_600_100)
}
pub(crate) fn default_nonce_trials_per_byte() -> NonceTrialsPerByte {
NonceTrialsPerByte::new(1000)
}
pub(crate) fn default_payload_length_extra_bytes() -> PayloadLengthExtraBytes {
PayloadLengthExtraBytes::new(1000)
}
impl Default for Config {
fn default() -> Self {
Self {
protocol_version: default_protocol_version(),
max_payload_length: default_max_payload_length(),
nonce_trials_per_byte: default_nonce_trials_per_byte(),
payload_length_extra_bytes: default_payload_length_extra_bytes(),
}
}
}
impl Config {
pub fn builder() -> Builder {
Builder::new()
}
pub fn new() -> Self {
Self::default()
}
pub fn protocol_version(&self) -> ProtocolVersion {
self.protocol_version
}
pub fn max_payload_length(&self) -> PayloadLength {
self.max_payload_length
}
pub fn nonce_trials_per_byte(&self) -> NonceTrialsPerByte {
self.nonce_trials_per_byte
}
pub fn payload_length_extra_bytes(&self) -> PayloadLengthExtraBytes {
self.payload_length_extra_bytes
}
}
#[derive(Clone, PartialEq, Eq, Debug, Default)]
pub struct Builder {
config: Config,
}
impl Builder {
fn new() -> Self {
Self::default()
}
pub fn protocol_version(&mut self, v: ProtocolVersion) -> &mut Self {
self.config.protocol_version = v;
self
}
pub fn max_payload_length(&mut self, v: PayloadLength) -> &mut Self {
self.config.max_payload_length = v;
self
}
pub fn nonce_trials_per_byte(&mut self, v: NonceTrialsPerByte) -> &mut Self {
self.config.nonce_trials_per_byte = v;
self
}
pub fn payload_length_extra_bytes(&mut self, v: PayloadLengthExtraBytes) -> &mut Self {
self.config.payload_length_extra_bytes = v;
self
}
pub fn build(&self) -> Config {
self.config.clone()
}
}