From 4ba802d0c73a1a1664b4d3e17757e54aeefd81f7 Mon Sep 17 00:00:00 2001 From: "Matthias P. Braendli" Date: Tue, 2 Jan 2024 15:12:47 +0100 Subject: Add default config, and start adding form --- src/config.rs | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 60 insertions(+), 7 deletions(-) (limited to 'src/config.rs') diff --git a/src/config.rs b/src/config.rs index 51bf507..33ba361 100644 --- a/src/config.rs +++ b/src/config.rs @@ -2,22 +2,41 @@ use std::fs; use anyhow::Context; use serde::{Deserialize, Serialize}; -#[derive(Serialize, Deserialize, Clone)] +#[derive(Debug, Serialize, Deserialize, Clone)] pub struct FelinetConfig { pub enabled: bool, pub address: String, } -#[derive(Serialize, Deserialize, Clone)] +impl Default for FelinetConfig { + fn default() -> Self { + FelinetConfig { + enabled: false, + address: "https://felinet.cats.radio".to_owned() + } + } +} + +#[derive(Debug, Serialize, Deserialize, Clone)] pub struct TunnelConfig { pub enabled: bool, pub local_ip: String, pub netmask: String, } +impl Default for TunnelConfig { + fn default() -> Self { + TunnelConfig { + enabled: false, + local_ip: "10.73.14.1".to_owned(), + netmask: "255.255.255.0".to_owned(), + } + } +} + type DurationSeconds = std::num::NonZeroU32; -#[derive(Serialize, Deserialize, Clone)] +#[derive(Debug, Serialize, Deserialize, Clone)] pub struct BeaconConfig { pub period_seconds: Option, #[serde(default)] @@ -31,7 +50,23 @@ pub struct BeaconConfig { pub tx_power: Option, } -#[derive(Serialize, Deserialize, Clone)] +impl Default for BeaconConfig { + fn default() -> Self { + BeaconConfig { + period_seconds: None, + max_hops: 3, + latitude: None, + longitude: None, + altitude: None, + comment: None, + antenna_height: None, + antenna_gain: None, + tx_power: None, + } + } +} + +#[derive(Debug, Serialize, Deserialize, Clone)] pub struct Config { pub callsign: String, pub ssid: u8, @@ -39,15 +74,33 @@ pub struct Config { pub icon: u16, pub felinet: FelinetConfig, pub beacon: BeaconConfig, - pub tunnel: Option, + pub tunnel: TunnelConfig, +} + +impl Default for Config { + fn default() -> Self { + Config { + callsign: "CHANGEME".to_owned(), + ssid: 0, + icon: 0, + felinet: Default::default(), + beacon: Default::default(), + tunnel: Default::default(), + } + } } const CONFIGFILE : &str = "node-config.toml"; impl Config { pub fn load() -> anyhow::Result { - let file_contents = fs::read_to_string(CONFIGFILE)?; - toml::from_str(&file_contents).context("parsing config file") + if std::path::Path::new(CONFIGFILE).exists() { + let file_contents = fs::read_to_string(CONFIGFILE)?; + toml::from_str(&file_contents).context("parsing config file") + } + else { + Ok(Default::default()) + } } pub fn store(&self) -> anyhow::Result<()> { -- cgit v1.2.3