refactor: update info mode to use ItemService

Co-authored-by: aider (openai/andrew/openrouter/google/gemini-2.5-pro) <aider@aider.chat>
This commit is contained in:
Andrew Phillips
2025-08-24 23:58:50 -03:00
parent 7ec0603e00
commit 7a5bcf2722

View File

@@ -1,17 +1,15 @@
use crate::db::Item;
use crate::modes::common::{format_size, OutputFormat};
use crate::config; use crate::config;
use anyhow::anyhow; use crate::core::types::ItemWithMeta;
use crate::modes::common::{format_size, OutputFormat};
use anyhow::{anyhow, Result};
use serde_json; use serde_json;
use serde_yaml; use serde_yaml;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use clap::Command; use clap::Command;
use clap::error::ErrorKind; use clap::error::ErrorKind;
use std::path::PathBuf; use std::path::PathBuf;
use std::str::FromStr;
use crate::compression_engine::CompressionType; use crate::core::item_service::ItemService;
use crate::db::{get_item, get_item_last, get_item_matching};
use crate::modes::common::get_format_box_chars_no_border_line_separator; use crate::modes::common::get_format_box_chars_no_border_line_separator;
use chrono::prelude::*; use chrono::prelude::*;
use is_terminal::IsTerminal; use is_terminal::IsTerminal;
@@ -25,7 +23,7 @@ pub fn mode_info(
tags: &mut Vec<String>, tags: &mut Vec<String>,
conn: &mut rusqlite::Connection, conn: &mut rusqlite::Connection,
data_path: PathBuf, data_path: PathBuf,
) -> anyhow::Result<()> { ) -> Result<()> {
if !ids.is_empty() && !tags.is_empty() { if !ids.is_empty() && !tags.is_empty() {
cmd.error(ErrorKind::InvalidValue, "Both ID and tags given, you must supply exactly one ID or atleast one tag when using --info").exit(); cmd.error(ErrorKind::InvalidValue, "Both ID and tags given, you must supply exactly one ID or atleast one tag when using --info").exit();
} else if ids.len() > 1 { } else if ids.len() > 1 {
@@ -41,18 +39,12 @@ pub fn mode_info(
} }
} }
let item_maybe = match tags.is_empty() && meta.is_empty() { let item_service = ItemService::new(data_path.clone());
true => match ids.iter().next() { let item_with_meta = item_service
Some(item_id) => get_item(conn, *item_id)?, .find_item(conn, ids, tags, &meta)
None => get_item_last(conn)?, .map_err(|e| anyhow!("Unable to find matching item in database: {}", e))?;
},
false => get_item_matching(conn, tags, &meta)?,
};
match item_maybe { show_item(item_with_meta, settings, data_path)
Some(item) => show_item(item, settings, conn, data_path),
None => Err(anyhow!("Unable to find matching item in database")),
}
} }
#[derive(Serialize, Deserialize)] #[derive(Serialize, Deserialize)]
@@ -70,24 +62,20 @@ struct ItemInfo {
} }
fn show_item( fn show_item(
item: Item, // Using the provided struct definition item_with_meta: ItemWithMeta,
settings: &config::Settings, settings: &config::Settings,
conn: &mut rusqlite::Connection,
data_path: PathBuf, data_path: PathBuf,
) -> anyhow::Result<()> { ) -> Result<()> {
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()
.map(|x| x.name)
.collect();
let output_format = crate::modes::common::settings_output_format(settings); let output_format = crate::modes::common::settings_output_format(settings);
if output_format != OutputFormat::Table { if output_format != OutputFormat::Table {
return show_item_structured(item, settings, conn, data_path, output_format); return show_item_structured(item_with_meta, settings, data_path, output_format);
} }
let item = item_with_meta.item;
let item_id = item.id.unwrap();
let item_tags: Vec<String> = item_with_meta.tags.into_iter().map(|t| t.name).collect();
let mut table = Table::new(); let mut table = Table::new();
if std::io::stdout().is_terminal() { if std::io::stdout().is_terminal() {
table.set_format(get_format_box_chars_no_border_line_separator()); table.set_format(get_format_box_chars_no_border_line_separator());
@@ -125,13 +113,9 @@ fn show_item(
size_cell, size_cell,
])); ]));
// 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![ table.add_row(Row::new(vec![
Cell::new("Compression").with_style(Attr::Bold), Cell::new("Compression").with_style(Attr::Bold),
Cell::new(&compression_type_val.to_string()), Cell::new(&item.compression),
])); ]));
let file_size_cell = match item_path_buf.metadata() { let file_size_cell = match item_path_buf.metadata() {
@@ -152,7 +136,7 @@ fn show_item(
Cell::new(&item_tags.join(" ")), Cell::new(&item_tags.join(" ")),
])); ]));
for meta in crate::db::get_item_meta(conn, &item)? { for meta in item_with_meta.meta {
let meta_name = format!("Meta: {}", &meta.name); let meta_name = format!("Meta: {}", &meta.name);
table.add_row(Row::new(vec![ table.add_row(Row::new(vec![
Cell::new(meta_name.as_str()).with_style(Attr::Bold), Cell::new(meta_name.as_str()).with_style(Attr::Bold),
@@ -165,17 +149,14 @@ fn show_item(
} }
fn show_item_structured( fn show_item_structured(
item: Item, item_with_meta: ItemWithMeta,
settings: &config::Settings, settings: &config::Settings,
conn: &mut rusqlite::Connection,
data_path: PathBuf, data_path: PathBuf,
output_format: OutputFormat, output_format: OutputFormat,
) -> anyhow::Result<()> { ) -> Result<()> {
let item = item_with_meta.item;
let item_id = item.id.unwrap(); let item_id = item.id.unwrap();
let item_tags: Vec<String> = crate::db::get_item_tags(conn, &item)? let item_tags: Vec<String> = item_with_meta.tags.into_iter().map(|t| t.name).collect();
.into_iter()
.map(|x| x.name)
.collect();
let mut item_path_buf = data_path.clone(); let mut item_path_buf = data_path.clone();
item_path_buf.push(item_id.to_string()); item_path_buf.push(item_id.to_string());
@@ -191,14 +172,15 @@ fn show_item_structured(
None => "Missing".to_string(), None => "Missing".to_string(),
}; };
let mut meta_map = std::collections::HashMap::new(); let meta_map = item_with_meta.meta_as_map();
for meta in crate::db::get_item_meta(conn, &item)? {
meta_map.insert(meta.name, meta.value);
}
let item_info = ItemInfo { let item_info = ItemInfo {
id: item_id, id: item_id,
timestamp: item.ts.with_timezone(&chrono::Local).format("%F %T %Z").to_string(), timestamp: item
.ts
.with_timezone(&chrono::Local)
.format("%F %T %Z")
.to_string(),
path: item_path_buf.to_str().unwrap_or("").to_string(), path: item_path_buf.to_str().unwrap_or("").to_string(),
stream_size: item.size.map(|s| s as u64), stream_size: item.size.map(|s| s as u64),
stream_size_formatted, stream_size_formatted,