Skip to content

Commit

Permalink
refacto: split cell content format and row format
Browse files Browse the repository at this point in the history
  • Loading branch information
sfauvel committed Feb 19, 2025
1 parent fb0d566 commit 320e040
Showing 1 changed file with 52 additions and 55 deletions.
107 changes: 52 additions & 55 deletions mithril-client-cli/src/commands/cardano_db_v2/show.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,21 +85,17 @@ impl CardanoDbShowCommand {
],
];

for digest_location in digest_location_rows(&cardano_db_message.locations.digests) {
cardano_db_table.push(digest_location);
}

for immutables_location in
immutables_location_rows(&cardano_db_message.locations.immutables)
{
cardano_db_table.push(immutables_location);
}

for ancillary_location in
ancillary_location_rows(&cardano_db_message.locations.ancillary)
{
cardano_db_table.push(ancillary_location);
}
cardano_db_table.append(&mut digest_location_rows(
&cardano_db_message.locations.digests,
));

cardano_db_table.append(&mut immutables_location_rows(
&cardano_db_message.locations.immutables,
));

cardano_db_table.append(&mut ancillary_location_rows(
&cardano_db_message.locations.ancillary,
));

cardano_db_table.push(vec![
"Created".cell(),
Expand All @@ -117,59 +113,60 @@ impl CardanoDbShowCommand {
}
}

fn digest_location_iter(locations: &[DigestLocation]) -> impl Iterator<Item = String> + use<'_> {
locations.iter().filter_map(|location| match location {
DigestLocation::Aggregator { uri } => Some(format!("Aggregator, uri: \"{}\"", uri)),
DigestLocation::CloudStorage { uri } => Some(format!("CloudStorage, uri: \"{}\"", uri)),
DigestLocation::Unknown => None,
})
}

fn digest_location_rows(locations: &[DigestLocation]) -> Vec<Vec<CellStruct>> {
let location_name = "Digest location";
format_location_rows("Digest location", digest_location_iter(locations))
}

locations
.iter()
.filter(|location| *location != &DigestLocation::Unknown)
.enumerate()
.filter_map(|(index, location)| match location {
DigestLocation::Aggregator { uri } => Some(vec![
format!("{location_name} ({})", index + 1).cell(),
format!("Aggregator, uri: \"{}\"", uri).cell(),
]),
DigestLocation::CloudStorage { uri } => Some(vec![
format!("{location_name} ({})", index + 1).cell(),
format!("CloudStorage, uri: \"{}\"", uri).cell(),
]),
DigestLocation::Unknown => None,
})
.collect()
fn immutables_location_iter(
locations: &[ImmutablesLocation],
) -> impl Iterator<Item = String> + use<'_> {
locations.iter().filter_map(|location| match location {
ImmutablesLocation::CloudStorage { uri } => match uri {
MultiFilesUri::Template(template_uri) => Some(format!(
"CloudStorage, template_uri: \"{}\"",
template_uri.0
)),
},
ImmutablesLocation::Unknown => None,
})
}

fn immutables_location_rows(locations: &[ImmutablesLocation]) -> Vec<Vec<CellStruct>> {
let location_name = "Immutables location";
format_location_rows("Immutables location", immutables_location_iter(locations))
}

locations
.iter()
.filter(|location| *location != &ImmutablesLocation::Unknown)
.enumerate()
.filter_map(|(index, location)| match location {
ImmutablesLocation::CloudStorage { uri } => match uri {
MultiFilesUri::Template(template_uri) => Some(vec![
format!("{location_name} ({})", index + 1).cell(),
format!("CloudStorage, template_uri: \"{}\"", template_uri.0).cell(),
]),
},
ImmutablesLocation::Unknown => None,
})
.collect()
fn ancillary_location_iter(
locations: &[AncillaryLocation],
) -> impl Iterator<Item = String> + use<'_> {
locations.iter().filter_map(|location| match location {
AncillaryLocation::CloudStorage { uri } => Some(format!("CloudStorage, uri: \"{uri}\"")),
AncillaryLocation::Unknown => None,
})
}

fn ancillary_location_rows(locations: &[AncillaryLocation]) -> Vec<Vec<CellStruct>> {
let location_name = "Ancillary location";
format_location_rows("Ancillary location", ancillary_location_iter(locations))
}

fn format_location_rows(
location_name: &str,
locations: impl Iterator<Item = String>,
) -> Vec<Vec<CellStruct>> {
locations
.iter()
.filter(|location| *location != &AncillaryLocation::Unknown)
.enumerate()
.filter_map(|(index, location)| match location {
AncillaryLocation::CloudStorage { uri } => Some(vec![
.map(|(index, cell_content)| {
vec![
format!("{location_name} ({})", index + 1).cell(),
format!("CloudStorage, uri: \"{}\"", uri).cell(),
]),
AncillaryLocation::Unknown => None,
cell_content.cell(),
]
})
.collect()
}
Expand Down

0 comments on commit 320e040

Please sign in to comment.