diff --git a/src/cli/configuration/mod.rs b/src/cli/configuration/mod.rs index 6d95e3a..08f79fa 100644 --- a/src/cli/configuration/mod.rs +++ b/src/cli/configuration/mod.rs @@ -249,7 +249,6 @@ mod tests { r#" library: github: - instance: https://api.github.com/ owner: jdno repository: flowcrafter workflows: diff --git a/src/github/configuration.rs b/src/github/configuration.rs index b9ce937..148dccb 100644 --- a/src/github/configuration.rs +++ b/src/github/configuration.rs @@ -10,6 +10,7 @@ use crate::github::repository::Repository; #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] pub struct GitHubConfiguration { #[cfg_attr(feature = "serde", serde(default = "default_instance"))] + #[cfg_attr(feature = "serde", serde(skip_serializing_if = "is_default_instance"))] #[builder(default = default_instance())] instance: Url, @@ -44,6 +45,10 @@ fn default_instance() -> Url { Url::parse("https://api.github.com").expect("failed to parse hard-coded GitHub URL 🤯") } +fn is_default_instance(instance: &Url) -> bool { + instance == &default_instance() +} + #[cfg(test)] mod tests { use super::*; @@ -101,6 +106,44 @@ mod tests { assert_send::(); } + #[cfg(feature = "serde")] + #[test] + fn trait_serialize_with_default_instance() { + let configuration = GitHubConfiguration::builder() + .owner("jdno") + .repository("flowcrafter") + .build(); + + let yaml = indoc!( + r#" + owner: jdno + repository: flowcrafter + "# + ); + + assert_eq!(yaml, serde_yaml::to_string(&configuration).unwrap()); + } + + #[cfg(feature = "serde")] + #[test] + fn trait_serialize_with_custom_instance() { + let configuration = GitHubConfiguration::builder() + .instance(Url::parse("https://github.example.com").unwrap()) + .owner("jdno") + .repository("flowcrafter") + .build(); + + let yaml = indoc!( + r#" + instance: https://github.example.com/ + owner: jdno + repository: flowcrafter + "# + ); + + assert_eq!(yaml, serde_yaml::to_string(&configuration).unwrap()); + } + #[test] fn trait_sync() { fn assert_sync() {}