deserialize numbers to string

This commit is contained in:
jb-alvarado 2023-06-19 21:56:41 +02:00
parent 4a578e83ff
commit c02241ffe8

View File

@ -18,20 +18,47 @@ use ffplayout_lib::utils::{
PlayoutConfig, PlayoutStatus, ProcessControl,
};
#[derive(Default, Deserialize, Clone)]
#[derive(Default, Deserialize, Clone, Debug)]
struct TextFilter {
text: Option<String>,
#[serde(default, deserialize_with = "deserialize_number_or_string")]
x: Option<String>,
#[serde(default, deserialize_with = "deserialize_number_or_string")]
y: Option<String>,
#[serde(default, deserialize_with = "deserialize_number_or_string")]
fontsize: Option<String>,
#[serde(default, deserialize_with = "deserialize_number_or_string")]
line_spacing: Option<String>,
fontcolor: Option<String>,
#[serde(default, deserialize_with = "deserialize_number_or_string")]
alpha: Option<String>,
#[serde(default, deserialize_with = "deserialize_number_or_string")]
r#box: Option<String>,
boxcolor: Option<String>,
#[serde(default, deserialize_with = "deserialize_number_or_string")]
boxborderw: Option<String>,
}
fn deserialize_number_or_string<'de, D>(deserializer: D) -> Result<Option<String>, D::Error>
where
D: serde::Deserializer<'de>,
{
let value: Value = Deserialize::deserialize(deserializer)?;
match value {
Value::Number(num) => {
if let Some(number) = num.as_i64() {
Ok(Some(number.to_string()))
} else if let Some(number) = num.as_f64() {
Ok(Some(number.to_string()))
} else {
Err(serde::de::Error::custom("Invalid number format"))
}
}
Value::String(string) => Ok(Some(string)),
_ => Err(serde::de::Error::custom("Invalid value type")),
}
}
impl fmt::Display for TextFilter {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let escaped_text = self