Skip to content

Commit

Permalink
config: validate backend from config file
Browse files Browse the repository at this point in the history
  • Loading branch information
eladyn committed Feb 24, 2025
1 parent 062027c commit d8ccb99
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,26 @@ fn possible_backends() -> Vec<&'static str> {
audio_backend::BACKENDS.iter().map(|b| b.0).collect()
}

fn deserialize_backend<'de, D>(de: D) -> Result<Option<String>, D::Error>
where
D: Deserializer<'de>,
{
let backend = String::deserialize(de)?;
let possible_backends = possible_backends();
if possible_backends.contains(&backend.as_str()) {
Ok(Some(backend))
} else {
Err(de::Error::invalid_value(
Unexpected::Str(&backend),
&format!(
"a valid backend (available: {})",
possible_backends.join(", ")
)
.as_str(),
))
}
}

fn number_or_string<'de, D>(de: D) -> Result<Option<u8>, D::Error>
where
D: Deserializer<'de>,
Expand Down Expand Up @@ -265,6 +285,7 @@ pub struct SharedConfigValues {

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

/// The volume controller to use
Expand Down

0 comments on commit d8ccb99

Please sign in to comment.