refactor: improve option handling and variable naming in show_item

This commit is contained in:
Andrew Phillips
2025-05-13 18:18:29 -03:00
committed by Andrew Phillips (aider)
parent c167f28d17
commit 62aeb05e76

View File

@@ -49,12 +49,12 @@ pub fn mode_info(
}
fn show_item(
item: Item,
item: Item, // Using the provided struct definition
args: crate::Args,
conn: &mut rusqlite::Connection,
data_path: PathBuf,
) -> anyhow::Result<()> {
let item_id = item.id.unwrap();
let item_id = item.id.unwrap(); // Consider using if let or expect for Option
let item_tags: Vec<String> = crate::db::get_item_tags(conn, &item)?
.into_iter()
@@ -74,18 +74,17 @@ fn show_item(
]));
let ts_cell = Cell::new(&item.ts.with_timezone(&Local).format("%F %T %Z").to_string());
table.add_row(Row::new(vec![
Cell::new("Timestamp").with_style(Attr::Bold),
ts_cell,
]));
let mut item_path = data_path.clone();
item_path.push(item.id.unwrap().to_string());
let mut item_path_buf = data_path.clone(); // Renamed to avoid conflict if item_path is used later
item_path_buf.push(item.id.unwrap().to_string()); // Again, consider safer unwrap
table.add_row(Row::new(vec![
Cell::new("Path").with_style(Attr::Bold),
Cell::new(item_path.to_str().expect("Unable to get item path")),
Cell::new(item_path_buf.to_str().expect("Unable to get item path")),
]));
let size_cell = match item.size {
@@ -94,21 +93,21 @@ fn show_item(
.with_style(Attr::ForegroundColor(prettytable::color::RED))
.with_style(Attr::Bold),
};
table.add_row(Row::new(vec![
Cell::new("Stream Size").with_style(Attr::Bold),
size_cell,
]));
let compression_type = CompressionType::from_str(&item.compression)
// compression_type is CompressionType due to '?'
let compression_type_val = CompressionType::from_str(&item.compression)
.map_err(|e| anyhow!("Failed to parse compression type: {}", e))?;
table.add_row(Row::new(vec![
Cell::new("Compression").with_style(Attr::Bold),
Cell::new(&compression_type.to_string()),
Cell::new(&compression_type_val.to_string()),
]));
let file_size_cell = match item_path.metadata() {
let file_size_cell = match item_path_buf.metadata() {
Ok(metadata) => {
Cell::new(format_size(metadata.len(), args.options.human_readable).as_str())
}
@@ -116,25 +115,20 @@ fn show_item(
.with_style(Attr::ForegroundColor(prettytable::color::RED))
.with_style(Attr::Bold),
};
table.add_row(Row::new(vec![
Cell::new("File Size").with_style(Attr::Bold),
file_size_cell,
]));
let file_magic_cell = match &compression_type {
Ok(compression_type) => {
let compression_engine =
get_compression_engine(compression_type.clone()).expect("Unable to get compression engine");
let magic = compression_engine.magic(item_path.clone());
// Corrected logic for file_magic_cell:
// compression_type_val is already the successfully parsed CompressionType.
// The .expect() here will panic if get_compression_engine returns an Err.
let compression_engine =
get_compression_engine(compression_type_val.clone()).expect("Unable to get compression engine");
let magic_result = compression_engine.magic(item_path_buf.clone()); // Use cloned item_path_buf
match magic {
Ok(magic) => Cell::new(magic.as_str()),
Err(e) => Cell::new(&e.to_string())
.with_style(Attr::ForegroundColor(prettytable::color::RED))
.with_style(Attr::Bold),
}
},
let file_magic_cell = match magic_result {
Ok(magic_str) => Cell::new(magic_str.as_str()),
Err(e) => Cell::new(&e.to_string())
.with_style(Attr::ForegroundColor(prettytable::color::RED))
.with_style(Attr::Bold),
@@ -150,26 +144,26 @@ fn show_item(
Cell::new(&item_tags.join(" ")),
]));
let digest_type = item.digest_type.clone();
match digest_type {
Some(digest_type) => {
table.add_row(Row::new(vec![
Cell::new("Digest Type").with_style(Attr::Bold),
Cell::new(&digest_type),
]));
},
None => {}
// Corrected logic for item.digest_type: String
// Assuming you want to display it only if it's not empty.
let digest_type_string_val = item.digest_type.clone();
if !digest_type_string_val.is_empty() {
table.add_row(Row::new(vec![
Cell::new("Digest Type").with_style(Attr::Bold),
Cell::new(&digest_type_string_val),
]));
}
let digest_value = item.digest_value.clone();
match digest_value {
Some(digest_value) => {
// Correct logic for item.digest_value: Option<String>
let digest_value_opt = item.digest_value.clone();
match digest_value_opt {
Some(dv_str) => {
table.add_row(Row::new(vec![
Cell::new("Digest Value").with_style(Attr::Bold),
Cell::new(&digest_value),
Cell::new(&dv_str),
]));
},
None => {}
None => { /* Do nothing if None, as per original logic */ }
}
for meta in crate::db::get_item_meta(conn, &item)? {