fix(webnsupdate): make IP none when query is empty

This happens when one of the IPs hasn't changed (but the other has) in
the FRITZ!Box DDNS client.
This commit is contained in:
Jalil David Salamé Messina 2025-01-31 21:04:36 +01:00
parent 09bd450a46
commit 82012066e8
Signed by: jalil
GPG key ID: F016B9E770737A0B

View file

@ -414,6 +414,24 @@ fn main() -> Result<()> {
.wrap_err("failed to run main loop")
}
/// Serde deserialization decorator to map empty Strings to None,
///
/// Adapted from: <https://github.com/tokio-rs/axum/blob/main/examples/query-params-with-empty-strings/src/main.rs>
fn empty_string_as_none<'de, D, T>(de: D) -> Result<Option<T>, D::Error>
where
D: serde::Deserializer<'de>,
T: std::str::FromStr,
T::Err: std::fmt::Display,
{
use serde::Deserialize;
let opt = Option::<&'de str>::deserialize(de)?;
match opt {
None | Some("") => Ok(None),
Some(s) => s.parse::<T>().map_err(serde::de::Error::custom).map(Some),
}
}
#[derive(Debug, serde::Deserialize)]
#[serde(deny_unknown_fields)]
struct FritzBoxUpdateParams {
@ -422,10 +440,10 @@ struct FritzBoxUpdateParams {
#[serde(default)]
domain: Option<String>,
/// IPv4 address for the domain
#[serde(default)]
#[serde(default, deserialize_with = "empty_string_as_none")]
ipv4: Option<Ipv4Addr>,
/// IPv6 address for the domain
#[serde(default)]
#[serde(default, deserialize_with = "empty_string_as_none")]
ipv6: Option<Ipv6Addr>,
/// IPv6 prefix for the home network
#[allow(unused)]