Skip to content

Commit 1fb9bbd

Browse files
committed
config: treat empty strings as None
This is preparing for saving config from within web interface. All lines even with empty values should be in TOML to avoid creating new values at the end of file... Changed default `config.toml` and added generic deserializer.
1 parent 071645a commit 1fb9bbd

File tree

2 files changed

+27
-6
lines changed

2 files changed

+27
-6
lines changed

contrib/config.toml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@
1515
# Enable wired USB connection to phone (VID:PID should be specified, zero is wildcard and can be used for single or both fields)
1616
# you can obtain it e.g. using `lsusb` after connecting phone,
1717
# and then use e.g. "18d1:0" which will handle specified phone vendor ID (Google Pixel in this example)
18-
# wired = "0000:0000"
18+
wired = ""
1919

2020
# Use a Google Android Auto Desktop Head Unit emulator instead of real HU device (will listen on TCP 5277 port)
2121
dhu = false
2222

2323
# UDC Controller name (used in special configurations)
24-
# udc = "dummy_udc.0"
24+
udc = ""
2525

2626
### DEBUGGING
2727
# Enable debug info
@@ -38,9 +38,11 @@
3838
advertise = false
3939

4040
# BLE device name (how your bluetooth device is visible)
41-
# btalias = "WirelessAADongle"
41+
# by default it is WirelessAADongle-CPUSERIAL
42+
btalias = ""
4243

4344
# Auto-connect to phone and initiate connection
45+
# empty string/no value: don't auto-connect,
4446
# zeros/wildcard: iterate over all previously connected devices,
4547
# set it to phone BT MAC address for connecting to this specific phone
4648
connect = "00:00:00:00:00:00"
@@ -87,7 +89,7 @@
8789

8890
# Path to script executed when Android Auto session starts/stops and EV battery data is needed
8991
# Argument "start" or "stop" is appended automatically when invoked
90-
# ev_battery_logger = "/usr/bin/some_script"
92+
ev_battery_logger = ""
9193

9294
# Traction battery capacity [Wh]
9395
ev_battery_capacity = 22000

src/main.rs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ use simplelog::*;
1717
use usb_gadget::uevent_listener;
1818
use usb_gadget::UsbGadgetState;
1919

20-
use serde::de::{self, Deserializer, Visitor};
20+
use serde::de::{self, Deserializer, Error as DeError, Visitor};
2121
use serde::Deserialize;
22-
use std::fmt;
22+
use std::fmt::{self, Display};
2323
use std::fs::OpenOptions;
2424
use std::path::PathBuf;
2525
use std::str::FromStr;
@@ -112,6 +112,20 @@ struct Args {
112112
config: PathBuf,
113113
}
114114

115+
pub fn empty_string_as_none<'de, T, D>(deserializer: D) -> Result<Option<T>, D::Error>
116+
where
117+
T: FromStr,
118+
T::Err: Display,
119+
D: Deserializer<'de>,
120+
{
121+
let s: String = Deserialize::deserialize(deserializer)?;
122+
if s.trim().is_empty() {
123+
Ok(None)
124+
} else {
125+
T::from_str(&s).map(Some).map_err(DeError::custom)
126+
}
127+
}
128+
115129
#[derive(Debug, Clone, Deserialize)]
116130
#[serde(default)]
117131
pub struct AppConfig {
@@ -120,12 +134,15 @@ pub struct AppConfig {
120134
hexdump_level: HexdumpLevel,
121135
disable_console_debug: bool,
122136
legacy: bool,
137+
#[serde(default, deserialize_with = "empty_string_as_none")]
123138
connect: Option<Address>,
124139
logfile: PathBuf,
125140
stats_interval: u16,
141+
#[serde(default, deserialize_with = "empty_string_as_none")]
126142
udc: Option<String>,
127143
iface: String,
128144
hostapd_conf: PathBuf,
145+
#[serde(default, deserialize_with = "empty_string_as_none")]
129146
btalias: Option<String>,
130147
keepalive: bool,
131148
timeout_secs: u16,
@@ -137,9 +154,11 @@ pub struct AppConfig {
137154
disable_media_sink: bool,
138155
disable_tts_sink: bool,
139156
developer_mode: bool,
157+
#[serde(default, deserialize_with = "empty_string_as_none")]
140158
wired: Option<UsbId>,
141159
dhu: bool,
142160
ev: bool,
161+
#[serde(default, deserialize_with = "empty_string_as_none")]
143162
ev_battery_logger: Option<PathBuf>,
144163
ev_battery_capacity: u64,
145164
ev_factor: f32,

0 commit comments

Comments
 (0)