Skip to content

Commit d4b0061

Browse files
committed
Define config default values
1 parent fe5c3f5 commit d4b0061

File tree

6 files changed

+209
-25
lines changed

6 files changed

+209
-25
lines changed

antares/Cargo.lock

Lines changed: 133 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

antares/Cargo.toml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@ edition = "2021"
66
[dependencies]
77
chrono = "0.4.38"
88
rand = "0.8.5"
9-
tokio = { version = "1", features = ["full"] }
10-
hyper = { version = "1", features = ["full"] }
11-
serde = { version = "1", features = ["derive"] }
129
toml = "0.8.19"
1310
async-trait = "0.1.88"
1411
tokio-tungstenite = "0.26.2"
1512
futures-util = "0.3.31"
1613
futures = "0.3.31"
1714
axum = "0.8.4"
15+
tokio = { version = "1", features = ["full"] }
16+
hyper = { version = "1", features = ["full"] }
17+
serde = { version = "1", features = ["derive"] }
18+
clap = { version = "4.5.37", features = ["derive"] }

antares/src/config.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,12 @@ pub struct Config {
1212
pub simulation: SimulationConfig,
1313
pub radar: RadarConfig,
1414
}
15+
16+
impl Default for Config {
17+
fn default() -> Self {
18+
Self {
19+
simulation: SimulationConfig::default(),
20+
radar: RadarConfig::default(),
21+
}
22+
}
23+
}

antares/src/main.rs

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,34 @@
1-
use antares::ShipConfig;
2-
use antares::{Config, Controller};
1+
use antares::{Config, Controller, ShipConfig};
32
use axum::{extract::State, routing::post, Json, Router};
4-
use std::{env, fs, net::SocketAddr, process, sync::Arc};
3+
use clap::Parser;
4+
use std::{fs, net::SocketAddr, process, sync::Arc};
55
use tokio::task;
66

7+
#[derive(Parser)]
8+
#[command(author, version, about)]
9+
struct Args {
10+
#[arg(long)]
11+
config: Option<String>,
12+
}
13+
714
#[tokio::main]
815
async fn main() {
9-
let args: Vec<String> = env::args().collect();
10-
11-
if args.len() != 2 {
12-
eprintln!("Usage: antares <config-file>");
13-
process::exit(1);
14-
}
15-
16-
let config_content = fs::read_to_string(&args[1]).unwrap_or_else(|err| {
17-
eprintln!("Problem reading the config file: {err}");
18-
process::exit(1);
19-
});
20-
21-
let config: Config = toml::from_str(&config_content).unwrap_or_else(|err| {
22-
eprintln!("Problem parsing the config file: {err}");
23-
process::exit(1);
24-
});
16+
let args = Args::parse();
17+
18+
let config = match args.config {
19+
Some(path) => {
20+
let content = fs::read_to_string(path).unwrap_or_else(|err| {
21+
eprintln!("Problem reading the config file: {err}");
22+
process::exit(1);
23+
});
24+
25+
toml::from_str(&content).unwrap_or_else(|err| {
26+
eprintln!("Problem parsing the config file: {err}");
27+
process::exit(1);
28+
})
29+
}
30+
None => Config::default(),
31+
};
2532

2633
let controller_bind_addr = config.simulation.controller_bind_addr.clone();
2734
let controller = Arc::new(Controller::new(config));

antares/src/radar/config.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,16 @@ pub struct RadarConfig {
77
pub bind_addr: String,
88
}
99

10+
impl Default for RadarConfig {
11+
fn default() -> Self {
12+
RadarConfig {
13+
detector: DetectorConfig::default(),
14+
broadcast: BroadcastConfig::default(),
15+
bind_addr: "0.0.0.0:17396".into(),
16+
}
17+
}
18+
}
19+
1020
#[derive(Debug, Deserialize, Clone)]
1121
pub struct DetectorConfig {
1222
pub range: f64,
@@ -15,9 +25,26 @@ pub struct DetectorConfig {
1525
pub start_coordinates: (f64, f64),
1626
}
1727

28+
impl Default for DetectorConfig {
29+
fn default() -> Self {
30+
DetectorConfig {
31+
range: 1000.0,
32+
speed: 0.0,
33+
angle: 0.0,
34+
start_coordinates: (4.0, -72.0),
35+
}
36+
}
37+
}
38+
1839
#[derive(Debug, Deserialize)]
1940
#[serde(tag = "type", rename_all = "lowercase")]
2041
pub enum BroadcastConfig {
2142
Tcp,
2243
WebSocket,
2344
}
45+
46+
impl Default for BroadcastConfig {
47+
fn default() -> Self {
48+
BroadcastConfig::Tcp
49+
}
50+
}

antares/src/simulation/config.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,16 @@ pub struct SimulationConfig {
1010
pub controller_bind_addr: String,
1111
}
1212

13+
impl Default for SimulationConfig {
14+
fn default() -> Self {
15+
SimulationConfig {
16+
emission_interval: 20,
17+
initial_ships: Vec::new(),
18+
controller_bind_addr: "0.0.0.0:17394".into(),
19+
}
20+
}
21+
}
22+
1323
#[derive(Debug, Deserialize)]
1424
pub struct ShipConfig {
1525
pub initial_position: (f64, f64),

0 commit comments

Comments
 (0)