diff --git a/src/attribute.rs b/src/attribute.rs index 7801575..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 @@ -447,7 +453,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 +494,8 @@ mod tests { ] ), concat!( - "remarks: \n", - " \n", + "remarks:\n", + " \n", ) )] fn attribute_display_multi_line(#[case] attribute: Attribute, #[case] expected: &str) { @@ -878,7 +884,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); }