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