refactor: replace if let with match in mode_info handling
This commit is contained in:
@@ -41,128 +41,135 @@ pub fn mode_info(
|
||||
false => get_item_matching(conn, tags, &meta)?,
|
||||
};
|
||||
|
||||
if let Some(item) = item_maybe {
|
||||
let item_id = item.id.unwrap();
|
||||
match item_maybe {
|
||||
Some(item) => {
|
||||
let item_id = item.id.unwrap();
|
||||
|
||||
let item_tags: Vec<String> = crate::db::get_item_tags(conn, &item)?
|
||||
.into_iter()
|
||||
.map(|x| x.name)
|
||||
.collect();
|
||||
let item_tags: Vec<String> = crate::db::get_item_tags(conn, &item)?
|
||||
.into_iter()
|
||||
.map(|x| x.name)
|
||||
.collect();
|
||||
|
||||
let mut table = Table::new();
|
||||
if std::io::stdout().is_terminal() {
|
||||
table.set_format(get_format_box_chars_no_border_line_separator());
|
||||
} else {
|
||||
table.set_format(*format::consts::FORMAT_NO_BORDER_LINE_SEPARATOR);
|
||||
}
|
||||
|
||||
table.add_row(Row::new(vec![
|
||||
Cell::new("ID").with_style(Attr::Bold),
|
||||
Cell::new(&item_id.to_string()),
|
||||
]));
|
||||
|
||||
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());
|
||||
|
||||
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")),
|
||||
]));
|
||||
|
||||
let size_cell = match item.size {
|
||||
Some(size) => Cell::new(format_size(size as u64, args.options.human_readable).as_str()),
|
||||
None => Cell::new("Missing")
|
||||
.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)?;
|
||||
table.add_row(Row::new(vec![
|
||||
Cell::new("Compression").with_style(Attr::Bold),
|
||||
Cell::new(&compression_type.to_string()),
|
||||
]));
|
||||
|
||||
let file_size_cell = match item_path.metadata() {
|
||||
Ok(metadata) => {
|
||||
Cell::new(format_size(metadata.len(), args.options.human_readable).as_str())
|
||||
let mut table = Table::new();
|
||||
if std::io::stdout().is_terminal() {
|
||||
table.set_format(get_format_box_chars_no_border_line_separator());
|
||||
} else {
|
||||
table.set_format(*format::consts::FORMAT_NO_BORDER_LINE_SEPARATOR);
|
||||
}
|
||||
Err(_) => Cell::new("Missing")
|
||||
.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,
|
||||
]));
|
||||
table.add_row(Row::new(vec![
|
||||
Cell::new("ID").with_style(Attr::Bold),
|
||||
Cell::new(&item_id.to_string()),
|
||||
]));
|
||||
|
||||
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());
|
||||
let ts_cell = Cell::new(&item.ts.with_timezone(&Local).format("%F %T %Z").to_string());
|
||||
|
||||
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),
|
||||
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());
|
||||
|
||||
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")),
|
||||
]));
|
||||
|
||||
let size_cell = match item.size {
|
||||
Some(size) => Cell::new(format_size(size as u64, args.options.human_readable).as_str()),
|
||||
None => Cell::new("Missing")
|
||||
.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)?;
|
||||
table.add_row(Row::new(vec![
|
||||
Cell::new("Compression").with_style(Attr::Bold),
|
||||
Cell::new(&compression_type.to_string()),
|
||||
]));
|
||||
|
||||
let file_size_cell = match item_path.metadata() {
|
||||
Ok(metadata) => {
|
||||
Cell::new(format_size(metadata.len(), args.options.human_readable).as_str())
|
||||
}
|
||||
},
|
||||
Err(e) => Cell::new(&e.to_string())
|
||||
.with_style(Attr::ForegroundColor(prettytable::color::RED))
|
||||
.with_style(Attr::Bold),
|
||||
};
|
||||
Err(_) => Cell::new("Missing")
|
||||
.with_style(Attr::ForegroundColor(prettytable::color::RED))
|
||||
.with_style(Attr::Bold),
|
||||
};
|
||||
|
||||
table.add_row(Row::new(vec![
|
||||
Cell::new("File Magic").with_style(Attr::Bold),
|
||||
file_magic_cell,
|
||||
]));
|
||||
|
||||
table.add_row(Row::new(vec![
|
||||
Cell::new("Tags").with_style(Attr::Bold),
|
||||
Cell::new(&item_tags.join(" ")),
|
||||
]));
|
||||
|
||||
let digest_type = item.digest_type.clone();
|
||||
if let Some(digest_type) = digest_type {
|
||||
table.add_row(Row::new(vec![
|
||||
Cell::new("Digest Type").with_style(Attr::Bold),
|
||||
Cell::new(&digest_type),
|
||||
Cell::new("File Size").with_style(Attr::Bold),
|
||||
file_size_cell,
|
||||
]));
|
||||
}
|
||||
|
||||
let digest_value = item.digest_value.clone();
|
||||
if let Some(digest_value) = digest_value {
|
||||
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());
|
||||
|
||||
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),
|
||||
}
|
||||
},
|
||||
Err(e) => Cell::new(&e.to_string())
|
||||
.with_style(Attr::ForegroundColor(prettytable::color::RED))
|
||||
.with_style(Attr::Bold),
|
||||
};
|
||||
|
||||
table.add_row(Row::new(vec![
|
||||
Cell::new("Digest Value").with_style(Attr::Bold),
|
||||
Cell::new(&digest_value),
|
||||
Cell::new("File Magic").with_style(Attr::Bold),
|
||||
file_magic_cell,
|
||||
]));
|
||||
}
|
||||
|
||||
for meta in crate::db::get_item_meta(conn, &item)? {
|
||||
let meta_name = format!("Meta: {}", &meta.name);
|
||||
table.add_row(Row::new(vec![
|
||||
Cell::new(meta_name.as_str()).with_style(Attr::Bold),
|
||||
Cell::new(&meta.value),
|
||||
Cell::new("Tags").with_style(Attr::Bold),
|
||||
Cell::new(&item_tags.join(" ")),
|
||||
]));
|
||||
}
|
||||
|
||||
table.printstd();
|
||||
Ok(())
|
||||
} else {
|
||||
Err(anyhow!("Unable to find matching item in database"))
|
||||
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 => {}
|
||||
}
|
||||
|
||||
let digest_value = item.digest_value.clone();
|
||||
match digest_value {
|
||||
Some(digest_value) => {
|
||||
table.add_row(Row::new(vec![
|
||||
Cell::new("Digest Value").with_style(Attr::Bold),
|
||||
Cell::new(&digest_value),
|
||||
]));
|
||||
},
|
||||
None => {}
|
||||
}
|
||||
|
||||
for meta in crate::db::get_item_meta(conn, &item)? {
|
||||
let meta_name = format!("Meta: {}", &meta.name);
|
||||
table.add_row(Row::new(vec![
|
||||
Cell::new(meta_name.as_str()).with_style(Attr::Bold),
|
||||
Cell::new(&meta.value),
|
||||
]));
|
||||
}
|
||||
|
||||
table.printstd();
|
||||
Ok(())
|
||||
},
|
||||
None => Err(anyhow!("Unable to find matching item in database")),
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user