fix macro_role: serde_field_string, add config tests

Signed-off-by: fufesou <shuanglongchen@yeah.net>
This commit is contained in:
fufesou 2023-05-19 11:10:24 +08:00
parent cb59825b8a
commit 8c0fa989b6

View File

@ -105,12 +105,9 @@ macro_rules! serde_field_string {
where
D: de::Deserializer<'de>,
{
let s: &str = de::Deserialize::deserialize(deserializer).unwrap_or_default();
Ok(if s.is_empty() {
Self::$default_func()
} else {
s.to_owned()
})
let s: String =
de::Deserialize::deserialize(deserializer).unwrap_or(Self::$default_func());
Ok(s)
}
};
}
@ -252,7 +249,11 @@ pub struct PeerConfig {
#[serde(flatten)]
pub view_only: ViewOnly,
#[serde(default, skip_serializing_if = "Option::is_none")]
#[serde(
default,
skip_serializing_if = "Option::is_none",
deserialize_with = "deserialize_option_resolution"
)]
pub custom_resolution: Option<Resolution>,
// The other scalar value must before this
@ -1495,6 +1496,7 @@ deserialize_default!(deserialize_option_string, Option<String>);
deserialize_default!(deserialize_hashmap_string_string, HashMap<String, String>);
deserialize_default!(deserialize_hashmap_string_bool, HashMap<String, bool>);
deserialize_default!(deserialize_hashmap_string_configoidcprovider, HashMap<String, ConfigOidcProvider>);
deserialize_default!(deserialize_option_resolution, Option<Resolution>);
#[cfg(test)]
mod tests {
@ -1543,4 +1545,34 @@ mod tests {
})
);
}
#[test]
fn test_peer_config_deserialize() {
let default_peer_config = toml::from_str::<PeerConfig>("").unwrap();
// test custom_resolution
{
let wrong_type_str = r#"
view_style = "adaptive"
scroll_style = "scrollbar"
custom_resolution = true
"#;
let mut compare_config = default_peer_config.clone();
compare_config.view_style = "adaptive".to_string();
compare_config.scroll_style = "scrollbar".to_string();
let cfg = toml::from_str::<PeerConfig>(wrong_type_str);
assert_eq!(cfg, Ok(compare_config), "Failed to test wrong_type_str");
let wrong_field_str = r#"
[custom_resolution]
w = 1920
h = 1080
hello = "world"
[ui_flutter]
"#;
let mut compare_config = default_peer_config.clone();
compare_config.custom_resolution = Some(Resolution { w: 1920, h: 1080 });
let cfg = toml::from_str::<PeerConfig>(wrong_field_str);
assert_eq!(cfg, Ok(compare_config), "Failed to test wrong_field_str");
}
}
}