1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
//! Simulation configuration.

use std::{hash::Hash, str::FromStr};

use crate::net::{self, tcp};
use serde::{Deserialize, Serialize};

/// Simulation configuration.
#[cfg_attr(docsrs, doc(cfg(madsim)))]
#[derive(Debug, Default, Serialize, Deserialize, PartialEq, Hash, Clone)]
pub struct Config {
    /// Network configurations.
    #[serde(default)]
    pub net: net::Config,

    /// Tcp Configurations
    #[serde(default)]
    pub tcp: tcp::TcpConfig,
}

impl Config {
    /// Returns the hash value of this config.
    pub fn hash(&self) -> u64 {
        ahash::RandomState::with_seed(0).hash_one(self)
    }
}

/// Parse a config from TOML.
impl FromStr for Config {
    type Err = toml::de::Error;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        toml::from_str(s)
    }
}

/// Print the config into TOML.
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() {
        // TODO: better way to parse Duration
        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 {}
            }
        );
    }
}