use std::{hash::Hash, str::FromStr};
use crate::net::{self, tcp};
use serde::{Deserialize, Serialize};
#[cfg_attr(docsrs, doc(cfg(madsim)))]
#[derive(Debug, Default, Serialize, Deserialize, PartialEq, Hash, Clone)]
pub struct Config {
#[serde(default)]
pub net: net::Config,
#[serde(default)]
pub tcp: tcp::TcpConfig,
}
impl Config {
pub fn hash(&self) -> u64 {
ahash::RandomState::with_seed(0).hash_one(self)
}
}
impl FromStr for Config {
type Err = toml::de::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
toml::from_str(s)
}
}
impl ToString for Config {
fn to_string(&self) -> String {
toml::to_string_pretty(self).unwrap()
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::time::Duration;
#[test]
fn parse() {
let config: Config = r#"
[net]
packet_loss_rate = 0.1
send_latency = { start = { secs = 0, nanos = 1000000 }, end = { secs = 0, nanos = 10000000 } }
[tcp]
"#
.parse()
.unwrap();
assert_eq!(
config,
Config {
net: net::Config {
packet_loss_rate: 0.1,
send_latency: Duration::from_millis(1)..Duration::from_millis(10)
},
tcp: tcp::TcpConfig {}
}
);
}
}