Skip to content

Commit d8ccb99

Browse files
committed
config: validate backend from config file
1 parent 062027c commit d8ccb99

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

src/config.rs

+21
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,26 @@ fn possible_backends() -> Vec<&'static str> {
173173
audio_backend::BACKENDS.iter().map(|b| b.0).collect()
174174
}
175175

176+
fn deserialize_backend<'de, D>(de: D) -> Result<Option<String>, D::Error>
177+
where
178+
D: Deserializer<'de>,
179+
{
180+
let backend = String::deserialize(de)?;
181+
let possible_backends = possible_backends();
182+
if possible_backends.contains(&backend.as_str()) {
183+
Ok(Some(backend))
184+
} else {
185+
Err(de::Error::invalid_value(
186+
Unexpected::Str(&backend),
187+
&format!(
188+
"a valid backend (available: {})",
189+
possible_backends.join(", ")
190+
)
191+
.as_str(),
192+
))
193+
}
194+
}
195+
176196
fn number_or_string<'de, D>(de: D) -> Result<Option<u8>, D::Error>
177197
where
178198
D: Deserializer<'de>,
@@ -265,6 +285,7 @@ pub struct SharedConfigValues {
265285

266286
/// The audio backend to use
267287
#[arg(long, short, value_parser = possible_backends())]
288+
#[serde(deserialize_with = "deserialize_backend")]
268289
backend: Option<String>,
269290

270291
/// The volume controller to use

0 commit comments

Comments
 (0)