Skip to content

Commit c867e69

Browse files
committed
refactor: return one line per location instead of concatenating multiple locations with commas.
1 parent 8239c8d commit c867e69

File tree

4 files changed

+116
-150
lines changed

4 files changed

+116
-150
lines changed

mithril-client-cli/src/commands/cardano_db_v2/show.rs

Lines changed: 108 additions & 133 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,7 @@ use crate::{
99
};
1010

1111
use mithril_client::{
12-
common::{
13-
AncillaryLocation, AncillaryLocationDiscriminants, DigestLocation,
14-
DigestLocationDiscriminants, ImmutablesLocation, ImmutablesLocationDiscriminants,
15-
MultiFilesUri,
16-
},
12+
common::{AncillaryLocation, DigestLocation, ImmutablesLocation, MultiFilesUri},
1713
MithrilResult,
1814
};
1915

@@ -89,32 +85,19 @@ impl CardanoDbShowCommand {
8985
],
9086
];
9187

92-
let mut digest_location_index = 1;
93-
for digest_location_type in [
94-
DigestLocationDiscriminants::Aggregator,
95-
DigestLocationDiscriminants::CloudStorage,
96-
] {
97-
if let Some(digest_location) = digest_location_row(
98-
digest_location_index,
99-
digest_location_type,
100-
&cardano_db_message.locations.digests,
101-
) {
102-
cardano_db_table.push(digest_location);
103-
digest_location_index += 1;
104-
}
88+
for digest_location in digest_location_rows(&cardano_db_message.locations.digests) {
89+
cardano_db_table.push(digest_location);
10590
}
10691

107-
if let Some(immutables_location) = immutables_location_row(
108-
ImmutablesLocationDiscriminants::CloudStorage,
109-
&cardano_db_message.locations.immutables,
110-
) {
92+
for immutables_location in
93+
immutables_location_rows(&cardano_db_message.locations.immutables)
94+
{
11195
cardano_db_table.push(immutables_location);
11296
}
11397

114-
if let Some(ancillary_location) = ancillary_location_row(
115-
AncillaryLocationDiscriminants::CloudStorage,
116-
&cardano_db_message.locations.ancillary,
117-
) {
98+
for ancillary_location in
99+
ancillary_location_rows(&cardano_db_message.locations.ancillary)
100+
{
118101
cardano_db_table.push(ancillary_location);
119102
}
120103

@@ -134,97 +117,63 @@ impl CardanoDbShowCommand {
134117
}
135118
}
136119

137-
fn digest_location_row(
138-
index: usize,
139-
location_type: DigestLocationDiscriminants,
140-
locations: &[DigestLocation],
141-
) -> Option<Vec<CellStruct>> {
142-
let uris = locations
120+
fn digest_location_rows(locations: &[DigestLocation]) -> Vec<Vec<CellStruct>> {
121+
let archive_type_location = "Digest location";
122+
123+
locations
143124
.iter()
144-
.filter_map(|location| match (location, location_type) {
145-
(DigestLocation::Aggregator { uri }, DigestLocationDiscriminants::Aggregator) => {
146-
Some(format!("uri: \"{}\"", uri))
125+
.enumerate()
126+
.map(|(index, location)| match location {
127+
DigestLocation::Aggregator { uri } => {
128+
vec![
129+
format!("{archive_type_location} ({})", index + 1).cell(),
130+
format!("Aggregator, uri: \"{}\"", uri).cell(),
131+
]
147132
}
148-
(DigestLocation::CloudStorage { uri }, DigestLocationDiscriminants::CloudStorage) => {
149-
Some(format!("uri: \"{}\"", uri))
133+
DigestLocation::CloudStorage { uri } => {
134+
vec![
135+
format!("{archive_type_location} ({})", index + 1).cell(),
136+
format!("CloudStorage, uri: \"{}\"", uri).cell(),
137+
]
150138
}
151-
_ => None,
152139
})
153-
.collect::<Vec<String>>()
154-
.join(",");
155-
156-
if uris.is_empty() {
157-
None
158-
} else {
159-
Some(vec![
160-
format!("Digest location ({index})").cell(),
161-
format!("{location_type:?}, {uris}").cell(),
162-
])
163-
}
140+
.collect()
164141
}
165142

166-
fn immutables_location_row(
167-
location_type: ImmutablesLocationDiscriminants,
168-
locations: &[ImmutablesLocation],
169-
) -> Option<Vec<CellStruct>> {
170-
let uris = locations
143+
fn immutables_location_rows(locations: &[ImmutablesLocation]) -> Vec<Vec<CellStruct>> {
144+
let archive_type_location = "Immutables location";
145+
146+
locations
171147
.iter()
172-
.map(|location| match (location, location_type) {
173-
(
174-
ImmutablesLocation::CloudStorage { uri },
175-
ImmutablesLocationDiscriminants::CloudStorage,
176-
) => match uri {
148+
.enumerate()
149+
.map(|(index, location)| match location {
150+
ImmutablesLocation::CloudStorage { uri } => match uri {
177151
MultiFilesUri::Template(template_uri) => {
178-
format!("template_uri: \"{}\"", template_uri.0)
152+
vec![
153+
format!("{archive_type_location} ({})", index + 1).cell(),
154+
format!("CloudStorage, template_uri: \"{}\"", template_uri.0).cell(),
155+
]
179156
}
180157
},
181158
})
182-
.collect::<Vec<String>>()
183-
.join(",");
184-
185-
if uris.is_empty() {
186-
None
187-
} else {
188-
Some(vec![
189-
"Immutables location".to_string().cell(),
190-
format!(
191-
"{:?}, {}",
192-
ImmutablesLocationDiscriminants::CloudStorage,
193-
uris
194-
)
195-
.cell(),
196-
])
197-
}
159+
.collect()
198160
}
199161

200-
fn ancillary_location_row(
201-
location_type: AncillaryLocationDiscriminants,
202-
locations: &[AncillaryLocation],
203-
) -> Option<Vec<CellStruct>> {
204-
let uris = locations
162+
fn ancillary_location_rows(locations: &[AncillaryLocation]) -> Vec<Vec<CellStruct>> {
163+
let archive_type_location = "Ancillary location";
164+
165+
locations
205166
.iter()
206-
.map(|location| match (location, location_type) {
207-
(
208-
AncillaryLocation::CloudStorage { uri },
209-
AncillaryLocationDiscriminants::CloudStorage,
210-
) => format!("uri: \"{}\"", uri),
167+
.enumerate()
168+
.map(|(index, location)| match location {
169+
AncillaryLocation::CloudStorage { uri } => {
170+
vec![
171+
format!("{archive_type_location} ({})", index + 1).cell(),
172+
format!("CloudStorage, uri: \"{}\"", uri).cell(),
173+
]
174+
}
211175
})
212-
.collect::<Vec<String>>()
213-
.join(",");
214-
215-
if uris.is_empty() {
216-
None
217-
} else {
218-
Some(vec![
219-
"Ancillary location".to_string().cell(),
220-
format!(
221-
"{:?}, {}",
222-
AncillaryLocationDiscriminants::CloudStorage,
223-
uris
224-
)
225-
.cell(),
226-
])
227-
}
176+
.collect()
228177
}
229178

230179
#[cfg(test)]
@@ -234,18 +183,14 @@ mod tests {
234183
use super::*;
235184

236185
#[test]
237-
fn digest_location_row_returns_none_when_no_uri_found_for_location_type() {
238-
let locations = vec![DigestLocation::Aggregator {
239-
uri: "http://aggregator.net/".to_string(),
240-
}];
241-
242-
let row = digest_location_row(123, DigestLocationDiscriminants::CloudStorage, &locations);
186+
fn digest_location_rows_when_no_uri_found() {
187+
let rows = digest_location_rows(&[]);
243188

244-
assert!(row.is_none());
189+
assert!(rows.is_empty());
245190
}
246191

247192
#[test]
248-
fn digest_location_row_returns_some_when_uri_found_for_location_type() {
193+
fn digest_location_rows_when_uris_found() {
249194
let locations = vec![
250195
DigestLocation::Aggregator {
251196
uri: "http://aggregator.net/".to_string(),
@@ -255,47 +200,77 @@ mod tests {
255200
},
256201
];
257202

258-
let row = digest_location_row(123, DigestLocationDiscriminants::CloudStorage, &locations);
259-
assert!(row.is_some());
203+
let rows = digest_location_rows(&locations);
204+
assert!(rows.len() == 2);
260205

261-
let row = digest_location_row(456, DigestLocationDiscriminants::Aggregator, &locations);
262-
assert!(row.is_some());
206+
let table = rows.table();
207+
let rows_rendered = table.display().unwrap().to_string();
208+
209+
assert!(rows_rendered.contains("Digest location (1)"));
210+
assert!(rows_rendered.contains("CloudStorage, uri: \"http://cloudstorage.com/\""));
211+
assert!(rows_rendered.contains("Digest location (2)"));
212+
assert!(rows_rendered.contains("Aggregator, uri: \"http://aggregator.net/\""));
263213
}
264214

265215
#[test]
266-
fn immutables_location_row_returns_none_when_no_uri_found_for_location_type() {
267-
let row = immutables_location_row(ImmutablesLocationDiscriminants::CloudStorage, &[]);
216+
fn immutables_location_rows_when_no_uri_found() {
217+
let rows = immutables_location_rows(&[]);
268218

269-
assert!(row.is_none());
219+
assert!(rows.is_empty());
270220
}
271221

272222
#[test]
273-
fn immutables_location_row_returns_some_when_uri_found_for_location_type() {
274-
let locations = vec![ImmutablesLocation::CloudStorage {
275-
uri: MultiFilesUri::Template(TemplateUri("http://cloudstorage.com/".to_string())),
276-
}];
223+
fn immutables_location_row_returns_some_when_uri_found() {
224+
let locations = vec![
225+
ImmutablesLocation::CloudStorage {
226+
uri: MultiFilesUri::Template(TemplateUri("http://cloudstorage1.com/".to_string())),
227+
},
228+
ImmutablesLocation::CloudStorage {
229+
uri: MultiFilesUri::Template(TemplateUri("http://cloudstorage2.com/".to_string())),
230+
},
231+
];
232+
233+
let rows = immutables_location_rows(&locations);
234+
235+
assert!(rows.len() == 2);
277236

278-
let row =
279-
immutables_location_row(ImmutablesLocationDiscriminants::CloudStorage, &locations);
237+
let table = rows.table();
238+
let rows_rendered = table.display().unwrap().to_string();
280239

281-
assert!(row.is_some());
240+
assert!(rows_rendered.contains("Immutables location (1)"));
241+
assert!(rows_rendered.contains("CloudStorage, template_uri: \"http://cloudstorage1.com/\""));
242+
assert!(rows_rendered.contains("Immutables location (2)"));
243+
assert!(rows_rendered.contains("CloudStorage, template_uri: \"http://cloudstorage2.com/\""));
282244
}
283245

284246
#[test]
285-
fn ancillary_location_row_returns_none_when_no_uri_found_for_location_type() {
286-
let row = ancillary_location_row(AncillaryLocationDiscriminants::CloudStorage, &[]);
247+
fn ancillary_location_rows_when_no_uri_found() {
248+
let rows = ancillary_location_rows(&[]);
287249

288-
assert!(row.is_none());
250+
assert!(rows.is_empty());
289251
}
290252

291253
#[test]
292-
fn ancillary_location_row_returns_some_when_uri_found_for_location_type() {
293-
let locations = vec![AncillaryLocation::CloudStorage {
294-
uri: "http://cloudstorage.com/".to_string(),
295-
}];
254+
fn ancillary_location_rows_when_uris_found() {
255+
let locations = vec![
256+
AncillaryLocation::CloudStorage {
257+
uri: "http://cloudstorage1.com/".to_string(),
258+
},
259+
AncillaryLocation::CloudStorage {
260+
uri: "http://cloudstorage2.com/".to_string(),
261+
},
262+
];
263+
264+
let rows = ancillary_location_rows(&locations);
265+
266+
assert!(rows.len() == 2);
296267

297-
let row = ancillary_location_row(AncillaryLocationDiscriminants::CloudStorage, &locations);
268+
let table = rows.table();
269+
let rows_rendered = table.display().unwrap().to_string();
298270

299-
assert!(row.is_some());
271+
assert!(rows_rendered.contains("Ancillary location (1)"));
272+
assert!(rows_rendered.contains("CloudStorage, uri: \"http://cloudstorage1.com/\""));
273+
assert!(rows_rendered.contains("Ancillary location (2)"));
274+
assert!(rows_rendered.contains("CloudStorage, uri: \"http://cloudstorage2.com/\""));
300275
}
301276
}

mithril-client/src/type_alias.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,9 @@ pub use mithril_common::messages::CardanoStakeDistributionListItemMessage as Car
6868
/// `mithril-common` re-exports
6969
pub mod common {
7070
pub use mithril_common::entities::{
71-
AncillaryLocationDiscriminants, BlockHash, BlockNumber, CardanoDbBeacon, ChainPoint,
72-
CompressionAlgorithm, DigestLocationDiscriminants, Epoch, ImmutableFileNumber,
73-
ImmutablesLocationDiscriminants, ProtocolMessage, ProtocolMessagePartKey,
74-
ProtocolParameters, SlotNumber, StakeDistribution, TransactionHash,
71+
BlockHash, BlockNumber, CardanoDbBeacon, ChainPoint, CompressionAlgorithm, Epoch,
72+
ImmutableFileNumber, ProtocolMessage, ProtocolMessagePartKey, ProtocolParameters,
73+
SlotNumber, StakeDistribution, TransactionHash,
7574
};
7675
cfg_unstable! {
7776
pub use mithril_common::entities::{

mithril-common/src/entities/cardano_database.rs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use semver::Version;
22
use serde::{Deserialize, Serialize};
33
use sha2::{Digest, Sha256};
4-
use strum::EnumDiscriminants;
54

65
use crate::{
76
entities::{CardanoDbBeacon, CompressionAlgorithm},
@@ -71,9 +70,7 @@ impl CardanoDatabaseSnapshot {
7170
}
7271

7372
/// Locations of the immutable file digests.
74-
#[derive(
75-
Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize, EnumDiscriminants,
76-
)]
73+
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
7774
#[serde(rename_all = "snake_case", tag = "type")]
7875
pub enum DigestLocation {
7976
/// Aggregator digest route location.
@@ -89,9 +86,7 @@ pub enum DigestLocation {
8986
}
9087

9188
/// Locations of the immutable files.
92-
#[derive(
93-
Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize, EnumDiscriminants,
94-
)]
89+
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
9590
#[serde(rename_all = "snake_case", tag = "type")]
9691
pub enum ImmutablesLocation {
9792
/// Cloud storage location.
@@ -102,9 +97,7 @@ pub enum ImmutablesLocation {
10297
}
10398

10499
/// Locations of the ancillary files.
105-
#[derive(
106-
Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize, EnumDiscriminants,
107-
)]
100+
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
108101
#[serde(rename_all = "snake_case", tag = "type")]
109102
pub enum AncillaryLocation {
110103
/// Cloud storage location.

mithril-common/src/entities/mod.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,8 @@ pub use block_number::BlockNumber;
3535
pub use block_range::{BlockRange, BlockRangeLength, BlockRangesSequence};
3636
pub use cardano_chain_point::{BlockHash, ChainPoint};
3737
pub use cardano_database::{
38-
AncillaryLocation, AncillaryLocationDiscriminants, ArtifactsLocations, CardanoDatabaseSnapshot,
39-
DigestLocation, DigestLocationDiscriminants, ImmutablesLocation,
40-
ImmutablesLocationDiscriminants,
38+
AncillaryLocation, ArtifactsLocations, CardanoDatabaseSnapshot, DigestLocation,
39+
ImmutablesLocation,
4140
};
4241
pub use cardano_db_beacon::CardanoDbBeacon;
4342
pub use cardano_network::CardanoNetwork;

0 commit comments

Comments
 (0)