From cdf22fb217a2bb5927640a0ae15737d2aeea79b1 Mon Sep 17 00:00:00 2001 From: Marvin Vogt Date: Mon, 7 Oct 2024 19:07:05 +0000 Subject: [PATCH 1/3] Adjust multi value attribute display tests --- src/attribute.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/attribute.rs b/src/attribute.rs index 7801575..329b187 100644 --- a/src/attribute.rs +++ b/src/attribute.rs @@ -447,7 +447,7 @@ mod tests { Attribute::unchecked_single("ASNumber", "32934"), "ASNumber: 32934\n" )] - #[case(Attribute::unchecked_single("ASNumber", None), "ASNumber: \n")] + #[case(Attribute::unchecked_single("ASNumber", None), "ASNumber:\n")] #[case( Attribute::unchecked_single("ASName", "FACEBOOK"), "ASName: FACEBOOK\n" @@ -488,8 +488,8 @@ mod tests { ] ), concat!( - "remarks: \n", - " \n", + "remarks:\n", + " \n", ) )] fn attribute_display_multi_line(#[case] attribute: Attribute, #[case] expected: &str) { From 3c7e0ff4fdc79373eada6e86eb8351ec7f3e9159 Mon Sep 17 00:00:00 2001 From: Marvin Vogt Date: Mon, 7 Oct 2024 20:17:06 +0000 Subject: [PATCH 2/3] Fix test name --- src/attribute.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/attribute.rs b/src/attribute.rs index 329b187..a598307 100644 --- a/src/attribute.rs +++ b/src/attribute.rs @@ -878,7 +878,10 @@ mod tests { Value::unchecked_multi(["multiple", "", "separated", "values"]), vec![Some("multiple".to_string()), None, Some("separated".to_string()), Some("values".to_string())] )] - fn value_into_vec_of_option_str(#[case] value: Value, #[case] expected: Vec>) { + fn value_into_vec_of_option_string( + #[case] value: Value, + #[case] expected: Vec>, + ) { let vec: Vec> = value.into(); assert_eq!(vec, expected); } From 8a9e000742cd7b6544953e14ee4fc3122130a5e8 Mon Sep 17 00:00:00 2001 From: Marvin Vogt Date: Wed, 9 Oct 2024 09:56:42 +0000 Subject: [PATCH 3/3] Adjust Attribute display implementation to pass updated tests --- src/attribute.rs | 56 +++++++++++++++++++++++++++--------------------- 1 file changed, 31 insertions(+), 25 deletions(-) diff --git a/src/attribute.rs b/src/attribute.rs index a598307..4b131fb 100644 --- a/src/attribute.rs +++ b/src/attribute.rs @@ -59,35 +59,29 @@ impl<'a> Attribute<'a> { impl fmt::Display for Attribute<'_> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - match &self.value { - Value::SingleLine(value) => { - writeln!(f, "{:16}{}", format!("{}:", self.name), { - match value { - Some(value) => value, - None => "", - } - }) + let values = self.value.values(); + + let first_value = values.first().expect("must contain at least one value"); + match first_value { + Some(value) => { + writeln!(f, "{:16}{}", format!("{}:", self.name), value)?; } - Value::MultiLine(values) => { - writeln!(f, "{:16}{}", format!("{}:", self.name), { - match &values[0] { - Some(value) => value, - None => "", - } - })?; - - let mut continuation_values = String::new(); - for value in &values[1..] { - continuation_values.push_str(&format!("{:16}{}\n", "", { - match &value { - Some(value) => value, - None => "", - } - })); + None => writeln!(f, "{}:", self.name)?, + } + + let remaining_values = &values[1..]; + for value in remaining_values { + match value { + Some(value) => { + writeln!(f, "{:16}{}", " ", value)?; + } + None => { + writeln!(f, " ")?; } - write!(f, "{continuation_values}") } } + + Ok(()) } } @@ -265,6 +259,18 @@ impl<'a> Value<'a> { } } + fn values(&'a self) -> Vec> { + match self { + Value::SingleLine(value) => { + vec![value.as_ref().map(std::convert::AsRef::as_ref)] + } + Value::MultiLine(values) => values + .iter() + .map(|v| v.as_ref().map(std::convert::AsRef::as_ref)) + .collect(), + } + } + /// The lines that contain content and are non empty. /// /// # Example