fix: resolve compilation errors by adding missing imports and fixing Result types
- Import `anyhow`, `clap::Command`, `log::debug`, and I/O traits - Fix all `Result` return types to include error type `anyhow::Error` - Replace `anyhow::anyhow!` with `anyhow!` macro calls - Fix transaction handling in `mode_save` - Add missing trait imports for I/O operations and string parsing Co-authored-by: aider (openai/andrew/openrouter/qwen/qwen3-coder) <aider@aider.chat>
This commit is contained in:
@@ -4,6 +4,7 @@ use crate::db::Item;
|
||||
use crate::db::Meta;
|
||||
use crate::db::store_meta;
|
||||
use crate::meta_plugin::MetaPluginType;
|
||||
use anyhow::{anyhow, Result};
|
||||
use clap::Command;
|
||||
use clap::error::ErrorKind;
|
||||
use humansize::{BINARY, FormatSizeOptions};
|
||||
@@ -115,7 +116,7 @@ pub fn store_item_meta_value(
|
||||
) -> Result<(), anyhow::Error> {
|
||||
// Save digest to meta
|
||||
let meta = Meta {
|
||||
id: item.id.unwrap(),
|
||||
id: item.id.ok_or_else(|| anyhow!("Item missing ID"))?,
|
||||
name: meta_name,
|
||||
value: meta_value,
|
||||
};
|
||||
|
||||
@@ -1,3 +1,10 @@
|
||||
use anyhow::{anyhow, Result};
|
||||
use clap::Command;
|
||||
use log::debug;
|
||||
use std::io::Read;
|
||||
use std::os::fd::FromRawFd;
|
||||
use std::str::FromStr;
|
||||
|
||||
fn validate_diff_args(cmd: &mut Command, ids: &Vec<i64>, tags: &Vec<String>) {
|
||||
if !tags.is_empty() {
|
||||
cmd.error(
|
||||
@@ -18,7 +25,7 @@ fn validate_diff_args(cmd: &mut Command, ids: &Vec<i64>, tags: &Vec<String>) {
|
||||
fn fetch_and_validate_items(
|
||||
conn: &mut rusqlite::Connection,
|
||||
ids: &Vec<i64>,
|
||||
) -> Result<(crate::db::Item, crate::db::Item)> {
|
||||
) -> Result<(crate::db::Item, crate::db::Item), anyhow::Error> {
|
||||
// Fetch items, ensuring they exist.
|
||||
let item_a = crate::db::get_item(conn, ids[0])?
|
||||
.ok_or_else(|| anyhow::anyhow!("Unable to find first item (ID: {}) in database", ids[0]))?;
|
||||
@@ -28,8 +35,8 @@ fn fetch_and_validate_items(
|
||||
log::debug!("MAIN: Found item A {:?}", item_a);
|
||||
log::debug!("MAIN: Found item B {:?}", item_b);
|
||||
|
||||
let item_a_id = item_a.id.ok_or_else(|| anyhow::anyhow!("Item A missing ID"))?;
|
||||
let item_b_id = item_b.id.ok_or_else(|| anyhow::anyhow!("Item B missing ID"))?;
|
||||
let item_a_id = item_a.id.ok_or_else(|| anyhow!("Item A missing ID"))?;
|
||||
let item_b_id = item_b.id.ok_or_else(|| anyhow!("Item B missing ID"))?;
|
||||
|
||||
// Validate that item IDs are positive to prevent path traversal issues
|
||||
if item_a_id <= 0 || item_b_id <= 0 {
|
||||
@@ -39,7 +46,7 @@ fn fetch_and_validate_items(
|
||||
Ok((item_a, item_b))
|
||||
}
|
||||
|
||||
fn get_item_tags(conn: &mut rusqlite::Connection, item: &crate::db::Item) -> Result<Vec<String>> {
|
||||
fn get_item_tags(conn: &mut rusqlite::Connection, item: &crate::db::Item) -> Result<Vec<String>, anyhow::Error> {
|
||||
let tags: Vec<String> = crate::db::get_item_tags(conn, item)?
|
||||
.into_iter()
|
||||
.map(|x| x.name)
|
||||
@@ -51,7 +58,7 @@ fn setup_diff_paths_and_compression(
|
||||
data_path: &std::path::PathBuf,
|
||||
item_a: &crate::db::Item,
|
||||
item_b: &crate::db::Item,
|
||||
) -> Result<(std::path::PathBuf, crate::compression_engine::CompressionType, std::path::PathBuf, crate::compression_engine::CompressionType)> {
|
||||
) -> Result<(std::path::PathBuf, crate::compression_engine::CompressionType, std::path::PathBuf, crate::compression_engine::CompressionType), anyhow::Error> {
|
||||
let item_a_id = item_a.id.ok_or_else(|| anyhow::anyhow!("Item A missing ID"))?;
|
||||
let item_b_id = item_b.id.ok_or_else(|| anyhow::anyhow!("Item B missing ID"))?;
|
||||
|
||||
@@ -68,7 +75,7 @@ fn setup_diff_paths_and_compression(
|
||||
Ok((item_path_a, compression_type_a, item_path_b, compression_type_b))
|
||||
}
|
||||
|
||||
fn setup_diff_pipes() -> Result<((libc::c_int, libc::c_int), (libc::c_int, libc::c_int))> {
|
||||
fn setup_diff_pipes() -> Result<((libc::c_int, libc::c_int), (libc::c_int, libc::c_int)), anyhow::Error> {
|
||||
use nix::unistd::pipe;
|
||||
use nix::Error as NixError;
|
||||
|
||||
@@ -88,7 +95,7 @@ fn setup_fd_guards(fd_a_read: libc::c_int, fd_b_read: libc::c_int) -> (FdGuard,
|
||||
(fd_a_read_guard, fd_b_read_guard)
|
||||
}
|
||||
|
||||
fn set_fd_cloexec(fd_a_write: libc::c_int, fd_b_write: libc::c_int) -> Result<()> {
|
||||
fn set_fd_cloexec(fd_a_write: libc::c_int, fd_b_write: libc::c_int) -> Result<(), anyhow::Error> {
|
||||
use nix::fcntl::{fcntl, FcntlArg, FdFlag};
|
||||
|
||||
// Set FD_CLOEXEC on write ends
|
||||
@@ -113,7 +120,7 @@ fn spawn_diff_process(
|
||||
item_b_tags: Vec<String>,
|
||||
fd_a_read: libc::c_int,
|
||||
fd_b_read: libc::c_int,
|
||||
) -> Result<std::process::Child> {
|
||||
) -> Result<std::process::Child, anyhow::Error> {
|
||||
log::debug!("MAIN: Creating child process for diff");
|
||||
let mut diff_command = std::process::Command::new("diff");
|
||||
diff_command
|
||||
@@ -165,7 +172,7 @@ fn write_item_to_pipe(
|
||||
item_path: std::path::PathBuf,
|
||||
compression_type: crate::compression_engine::CompressionType,
|
||||
pipe_writer_raw: std::fs::File,
|
||||
) -> Result<()> {
|
||||
) -> Result<(), anyhow::Error> {
|
||||
use std::io::BufWriter;
|
||||
let mut buffered_pipe_writer = BufWriter::new(pipe_writer_raw);
|
||||
let engine =
|
||||
@@ -183,7 +190,7 @@ fn spawn_writer_thread(
|
||||
item_path: std::path::PathBuf,
|
||||
compression_type: crate::compression_engine::CompressionType,
|
||||
fd_write: libc::c_int,
|
||||
) -> std::thread::JoinHandle<Result<()>> {
|
||||
) -> std::thread::JoinHandle<Result<(), anyhow::Error>> {
|
||||
let pipe_writer_raw = unsafe { std::fs::File::from_raw_fd(fd_write) };
|
||||
std::thread::spawn(move || {
|
||||
write_item_to_pipe(item_path, compression_type, pipe_writer_raw)
|
||||
@@ -192,7 +199,7 @@ fn spawn_writer_thread(
|
||||
|
||||
fn execute_diff_command(
|
||||
child_process: &mut std::process::Child,
|
||||
) -> Result<(Vec<u8>, Vec<u8>)> {
|
||||
) -> Result<(Vec<u8>, Vec<u8>), anyhow::Error> {
|
||||
let mut child_stdout_pipe = child_process
|
||||
.stdout
|
||||
.take()
|
||||
@@ -247,7 +254,7 @@ fn handle_diff_output(
|
||||
diff_status: std::process::ExitStatus,
|
||||
stdout_capture_result: Vec<u8>,
|
||||
stderr_capture_result: Vec<u8>,
|
||||
) -> Result<()> {
|
||||
) -> Result<(), anyhow::Error> {
|
||||
// Handle diff's exit status and output
|
||||
match diff_status.code() {
|
||||
Some(0) => {
|
||||
@@ -307,7 +314,7 @@ pub fn mode_diff(
|
||||
tags: &mut Vec<String>,
|
||||
conn: &mut rusqlite::Connection,
|
||||
data_path: std::path::PathBuf,
|
||||
) -> Result<()> {
|
||||
) -> Result<(), anyhow::Error> {
|
||||
validate_diff_args(cmd, ids, tags);
|
||||
let (item_a, item_b) = fetch_and_validate_items(conn, ids)?;
|
||||
|
||||
|
||||
@@ -1,3 +1,8 @@
|
||||
use anyhow::{anyhow, Result};
|
||||
use clap::Command;
|
||||
use log::debug;
|
||||
use std::io::{Read, Write, IsTerminal};
|
||||
|
||||
fn validate_save_args(cmd: &mut Command, ids: &Vec<i64>) {
|
||||
if !ids.is_empty() {
|
||||
cmd.error(
|
||||
@@ -73,7 +78,7 @@ fn create_and_log_item(
|
||||
args: &crate::Args,
|
||||
tags: &Vec<String>,
|
||||
compression_type: &crate::compression_engine::CompressionType,
|
||||
) -> Result<crate::db::Item> {
|
||||
) -> Result<crate::db::Item, anyhow::Error> {
|
||||
let mut item = crate::db::Item {
|
||||
id: None,
|
||||
ts: chrono::Utc::now(),
|
||||
@@ -116,7 +121,7 @@ fn setup_item_metadata(
|
||||
args: &crate::Args,
|
||||
item: &crate::db::Item,
|
||||
tags: &Vec<String>,
|
||||
) -> Result<()> {
|
||||
) -> Result<(), anyhow::Error> {
|
||||
crate::db::set_item_tags(conn, item.clone(), tags)?;
|
||||
Ok(())
|
||||
}
|
||||
@@ -143,7 +148,7 @@ fn process_input_stream(
|
||||
data_path: &std::path::PathBuf,
|
||||
item_id: i64,
|
||||
meta_plugins: &mut Vec<Box<dyn crate::meta_plugin::MetaPlugin>>,
|
||||
) -> Result<(Box<dyn std::io::Write>, crate::db::Item)> {
|
||||
) -> Result<(Box<dyn std::io::Write>, crate::db::Item), anyhow::Error> {
|
||||
let mut item = crate::db::Item {
|
||||
id: Some(item_id),
|
||||
ts: chrono::Utc::now(),
|
||||
@@ -161,10 +166,7 @@ fn process_input_stream(
|
||||
let mut item_out: Box<dyn std::io::Write> =
|
||||
compression_engine
|
||||
.create(item_path.clone())
|
||||
.context(anyhow::anyhow!(
|
||||
"Unable to write file {:?}",
|
||||
item_path
|
||||
))?;
|
||||
.map_err(|e| anyhow!("Unable to write file {:?}: {}", item_path, e))?;
|
||||
|
||||
debug!("MAIN: Starting IO loop");
|
||||
loop {
|
||||
@@ -200,7 +202,7 @@ fn finalize_meta_plugins(
|
||||
conn: &rusqlite::Connection,
|
||||
meta_plugins: &mut Vec<Box<dyn crate::meta_plugin::MetaPlugin>>,
|
||||
item: &crate::db::Item,
|
||||
) -> Result<()> {
|
||||
) -> Result<(), anyhow::Error> {
|
||||
for meta_plugin in meta_plugins.iter_mut() {
|
||||
let meta_name = meta_plugin.meta_name();
|
||||
|
||||
@@ -225,7 +227,7 @@ pub fn mode_save(
|
||||
tags: &mut Vec<String>,
|
||||
conn: &mut rusqlite::Connection,
|
||||
data_path: std::path::PathBuf,
|
||||
) -> Result<()> {
|
||||
) -> Result<(), anyhow::Error> {
|
||||
validate_save_args(cmd, ids);
|
||||
initialize_tags(tags);
|
||||
|
||||
@@ -238,7 +240,7 @@ pub fn mode_save(
|
||||
let tx = conn.transaction()?;
|
||||
|
||||
let item_meta = collect_item_meta(args);
|
||||
let item_id = item.id.ok_or_else(|| anyhow::anyhow!("Item missing ID"))?;
|
||||
let item_id = item.id.ok_or_else(|| anyhow!("Item missing ID"))?;
|
||||
|
||||
for kv in item_meta.iter() {
|
||||
let meta = crate::db::Meta {
|
||||
@@ -259,7 +261,7 @@ pub fn mode_save(
|
||||
item.size = processed_item.size;
|
||||
item.compression = compression_type.to_string();
|
||||
|
||||
finalize_meta_plugins(&tx, &mut meta_plugins, &item)?;
|
||||
finalize_meta_plugins(&mut tx.into_inner(), &mut meta_plugins, &item)?;
|
||||
crate::db::update_item(&tx, item.clone())?;
|
||||
|
||||
// Commit the transaction
|
||||
|
||||
Reference in New Issue
Block a user