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
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
//! Core traits for the library

/// # BuildParam
///
/// This trait is used to build the parameters for the NS3 program.
///
/// Implement this trait and also the `Default` trait on your own config struct.
///
/// ## Example
///
/// ```
/// use serde::{Deserialize, Serialize};
/// use ns3_parallel::{BuildParam, BuildCmd};
///
/// #[derive(Debug, Serialize, Deserialize)]
/// #[serde(default)]
/// pub struct Config {
///     pub sim_time: u32,
///     pub app_name: String,
///     pub policy: Vec<u32>,
/// }
///
/// #[derive(Debug, Clone, Serialize, Deserialize)]
/// pub struct Param {
///     pub sim_time: u32,
///     pub app_name: String,
///     pub policy: u32,
/// }
///
/// impl Default for Config {
///     fn default() -> Self {
///         Config {
///             sim_time: 100,
///             app_name: "ns3-tcp-bbr".to_string(),
///             policy: vec![1, 2, 3],
///         }
///     }
/// }
///
/// impl BuildParam<Param> for Config {
///     fn build_param(&self) -> Vec<Param> {
///         let mut params: Vec<Param> = Vec::new();
///         for policy in &self.policy {
///             let param = Param {
///                 sim_time: self.sim_time,
///                 app_name: self.app_name.clone(),
///                 policy: *policy,
///             };
///             params.push(param);
///         }
///         params
///     }      
/// }
/// impl BuildCmd for Param {
///     fn build_cmd(&self) -> String {
///         format!(
///             "xxx --app-name={} --sim-time={} --policy={}",
///             self.app_name, self.sim_time, self.policy
///         )
///     }
/// }
pub trait BuildParam<P: BuildCmd> {
    fn build_param(&self) -> Vec<P>;
}

/// # BuildCmd
///
/// This trait is used to build the command line for the NS3 program.
///
/// Implement this trait on your own param struct.
///
/// ## Example
///
/// ```
/// use serde::{Deserialize, Serialize};
/// use ns3_parallel::{BuildParam, BuildCmd};
///
/// #[derive(Debug, Serialize, Deserialize)]
/// #[serde(default)]
/// pub struct Config {
///     pub sim_time: u32,
///     pub app_name: String,
///     pub policy: Vec<u32>,
/// }
///
/// #[derive(Debug, Clone, Serialize, Deserialize)]
/// pub struct Param {
///     pub sim_time: u32,
///     pub app_name: String,
///     pub policy: u32,
/// }
///
/// impl Default for Config {
///     fn default() -> Self {
///         Config {
///             sim_time: 100,
///             app_name: "ns3-tcp-bbr".to_string(),
///             policy: vec![1, 2, 3],
///         }
///     }
/// }
///
/// impl BuildParam<Param> for Config {
///     fn build_param(&self) -> Vec<Param> {
///         let mut params: Vec<Param> = Vec::new();
///         for policy in &self.policy {
///             let param = Param {
///                 sim_time: self.sim_time,
///                 app_name: self.app_name.clone(),
///                 policy: *policy,
///             };
///             params.push(param);
///         }
///         params
///     }      
/// }
/// impl BuildCmd for Param {
///     fn build_cmd(&self) -> String {
///         format!(
///             "xxx --app-name={} --sim-time={} --policy={}",
///             self.app_name, self.sim_time, self.policy
///         )
///     }
/// }
pub trait BuildCmd {
    fn build_cmd(&self) -> String;
}