Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor attribute display implementation #135

Merged
merged 4 commits into from
Oct 9, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 38 additions & 29 deletions src/attribute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}
}

Expand Down Expand Up @@ -265,6 +259,18 @@ impl<'a> Value<'a> {
}
}

fn values(&'a self) -> Vec<Option<&'a str>> {
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
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -488,8 +494,8 @@ mod tests {
]
),
concat!(
"remarks: \n",
" \n",
"remarks:\n",
" \n",
)
)]
fn attribute_display_multi_line(#[case] attribute: Attribute, #[case] expected: &str) {
Expand Down Expand Up @@ -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<Option<String>>) {
fn value_into_vec_of_option_string(
#[case] value: Value,
#[case] expected: Vec<Option<String>>,
) {
let vec: Vec<Option<String>> = value.into();
assert_eq!(vec, expected);
}
Expand Down