Skip to content

Commit

Permalink
remove use anyhow::Result
Browse files Browse the repository at this point in the history
  • Loading branch information
chadaustin committed Nov 24, 2024
1 parent d7a0d02 commit ecfd635
Show file tree
Hide file tree
Showing 15 changed files with 65 additions and 78 deletions.
17 changes: 8 additions & 9 deletions src/cmd/benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

use crate::cmd::index;
use anyhow::anyhow;
use anyhow::Result;
use chrono::DateTime;
use chrono::Local;
use clap::Args;
Expand Down Expand Up @@ -31,7 +30,7 @@ pub enum Benchmark {
}

impl Benchmark {
pub async fn run(&self) -> Result<()> {
pub async fn run(&self) -> anyhow::Result<()> {
match self {
Benchmark::Walkdir(cmd) => cmd.run().await,
Benchmark::Jwalk(cmd) => cmd.run().await,
Expand All @@ -51,7 +50,7 @@ pub struct Walkdir {
}

impl Walkdir {
async fn run(&self) -> Result<()> {
async fn run(&self) -> anyhow::Result<()> {
let now = Instant::now();
let mut stat_calls = 0;

Expand Down Expand Up @@ -90,7 +89,7 @@ pub struct Jwalk {
}

impl Jwalk {
async fn run(&self) -> Result<()> {
async fn run(&self) -> anyhow::Result<()> {
let now = Instant::now();
let mut stat_calls = 0;
for entry in jwalk::WalkDir::new(&self.path)
Expand Down Expand Up @@ -137,7 +136,7 @@ pub struct JwalkParStat {
}

impl JwalkParStat {
async fn run(&self) -> Result<()> {
async fn run(&self) -> anyhow::Result<()> {
let now = Instant::now();

let (path_tx, path_rx) = batch_channel::unbounded_sync();
Expand Down Expand Up @@ -209,7 +208,7 @@ pub struct Scan {
}

impl Scan {
async fn run(&self) -> Result<()> {
async fn run(&self) -> anyhow::Result<()> {
let scan = self
.scanner
.as_ref()
Expand Down Expand Up @@ -254,7 +253,7 @@ pub struct Index {
}

impl Index {
async fn run(&self) -> Result<()> {
async fn run(&self) -> anyhow::Result<()> {
let db = Arc::new(Mutex::new(if self.real_db {
Database::open()?
} else {
Expand Down Expand Up @@ -283,7 +282,7 @@ pub struct Validate {
}

impl Validate {
async fn run(&self) -> Result<()> {
async fn run(&self) -> anyhow::Result<()> {
let scanners = scan::get_all_scanners();
let mut results = Vec::with_capacity(scanners.len());
let mut all_scanners = HashSet::new();
Expand All @@ -305,7 +304,7 @@ impl Validate {
.await
.into_iter()
.map(|f| f.unwrap())
.collect::<Result<_>>()?;
.collect::<anyhow::Result<_>>()?;

// path -> {scanner_name -> metadata}
let mut path_to_scanner: HashMap<IMPath, HashMap<&'static str, FileInfo>> = HashMap::new();
Expand Down
3 changes: 1 addition & 2 deletions src/cmd/broken.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::cmd::index::do_index;
use anyhow::Result;
use clap::Args;
use photohash::hash;
use photohash::Database;
Expand All @@ -16,7 +15,7 @@ pub struct Broken {
}

impl Broken {
pub async fn run(&self) -> Result<()> {
pub async fn run(&self) -> anyhow::Result<()> {
let db = Arc::new(Mutex::new(Database::open()?));

let dirs: Vec<_> = self.dirs.iter().map(|d| d.as_ref()).collect();
Expand Down
11 changes: 5 additions & 6 deletions src/cmd/db.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#[cfg(not(unix))]
use anyhow::anyhow;
use anyhow::Context;
use anyhow::Result;
use clap::Args;
use clap::Subcommand;
use photohash::database;
Expand All @@ -14,7 +13,7 @@ use std::process::Command;
pub struct DbPath {}

impl DbPath {
async fn run(&self) -> Result<()> {
async fn run(&self) -> anyhow::Result<()> {
println!("{}", database::get_database_path()?.display());
Ok(())
}
Expand All @@ -25,19 +24,19 @@ impl DbPath {
pub struct DbOpen {}

impl DbOpen {
async fn run(&self) -> Result<()> {
async fn run(&self) -> anyhow::Result<()> {
let mut cmd = Command::new("sqlite3");
cmd.arg(database::get_database_path()?);
Self::exec(cmd).context("failed to run sqlite3")
}

#[cfg(unix)]
fn exec(mut cmd: Command) -> Result<()> {
fn exec(mut cmd: Command) -> anyhow::Result<()> {
Err(cmd.exec().into())
}

#[cfg(not(unix))]
fn exec(mut cmd: Command) -> Result<()> {
fn exec(mut cmd: Command) -> anyhow::Result<()> {
let status = cmd.status()?;
if status.success() {
Ok(())
Expand All @@ -57,7 +56,7 @@ pub enum Db {
}

impl Db {
pub async fn run(&self) -> Result<()> {
pub async fn run(&self) -> anyhow::Result<()> {
match self {
Db::DbPath(cmd) => cmd.run().await,
Db::DbOpen(cmd) => cmd.run().await,
Expand Down
7 changes: 3 additions & 4 deletions src/cmd/diff.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::cmd::index;
use anyhow::Result;
use clap::Args;
use photohash::model::Hash32;
use photohash::model::IMPath;
Expand Down Expand Up @@ -53,7 +52,7 @@ impl Dots {
}

impl Diff {
pub async fn run(&self) -> Result<()> {
pub async fn run(&self) -> anyhow::Result<()> {
let db = Arc::new(Mutex::new(Database::open()?));

let difference =
Expand Down Expand Up @@ -124,7 +123,7 @@ pub struct DestinationIndex {
pub async fn index_destination(
db: &Arc<Mutex<Database>>,
dests: &[&Path],
) -> Result<DestinationIndex> {
) -> anyhow::Result<DestinationIndex> {
let mut index = DestinationIndex::default();

eprint!("scanning destination...");
Expand Down Expand Up @@ -175,7 +174,7 @@ pub async fn compute_difference(
src: PathBuf,
dests: Vec<PathBuf>,
exact: bool,
) -> Result<Differences> {
) -> anyhow::Result<Differences> {
// Begin indexing the source in parallel.
// TODO: If we could guarantee the output channel is sorted, we could
// incrementally display results.
Expand Down
5 changes: 2 additions & 3 deletions src/cmd/exif.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use anyhow::Result;
use clap::Args;
use std::path::Path;
use std::path::PathBuf;

fn read_exif_from_jpeg(path: &Path) -> Result<exif::Exif> {
fn read_exif_from_jpeg(path: &Path) -> anyhow::Result<exif::Exif> {
let file = std::fs::File::open(path)?;

let reader = exif::Reader::new();
Expand All @@ -18,7 +17,7 @@ pub struct Exif {
}

impl Exif {
pub async fn run(&self) -> Result<()> {
pub async fn run(&self) -> anyhow::Result<()> {
for path in &self.paths {
let exif = read_exif_from_jpeg(path)?;
println!("{}", path.display());
Expand Down
5 changes: 2 additions & 3 deletions src/cmd/find.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::cmd::diff::index_destination;
use crate::cmd::index;
use anyhow::Result;
use clap::Args;
use photohash::Database;
use std::collections::BTreeSet;
Expand All @@ -22,7 +21,7 @@ pub struct Find {
}

impl Find {
pub async fn run(mut self) -> Result<()> {
pub async fn run(mut self) -> anyhow::Result<()> {
self.src = self.src.canonicalize()?;
self.dests = self
.dests
Expand Down Expand Up @@ -62,7 +61,7 @@ async fn find_matching(
src: PathBuf,
dests: Vec<PathBuf>,
exact: bool,
) -> Result<FoundMatches> {
) -> anyhow::Result<FoundMatches> {
// Begin indexing the source in parallel.
// TODO: If we could guarantee the output channel is sorted, we could
// incrementally display results.
Expand Down
7 changes: 3 additions & 4 deletions src/cmd/index.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::compute_blake3;
use anyhow::Result;
use clap::Args;
use hex::ToHex;
use photohash::hash;
Expand Down Expand Up @@ -30,7 +29,7 @@ pub struct Index {
}

impl Index {
pub async fn run(&self) -> Result<()> {
pub async fn run(&self) -> anyhow::Result<()> {
let db = Arc::new(Mutex::new(Database::open()?));

let here = Path::new(".");
Expand Down Expand Up @@ -126,7 +125,7 @@ impl Index {
pub fn do_index(
db: &Arc<Mutex<Database>>,
dirs: &[&Path],
) -> Result<mpsc::Receiver<JoinHandle<Result<ProcessFileResult>>>> {
) -> anyhow::Result<mpsc::Receiver<JoinHandle<anyhow::Result<ProcessFileResult>>>> {
let scanner = scan::get_default_scan();
let path_meta_rx = scanner(dirs)?;

Expand Down Expand Up @@ -208,7 +207,7 @@ async fn process_file(
path: String,
file_info: FileInfo,
db_metadata: Option<ContentMetadata>,
) -> Result<ProcessFileResult> {
) -> anyhow::Result<ProcessFileResult> {
let mut blake3_computed = false;

// TODO: only open the file once, and reuse it for any potential image hashing
Expand Down
3 changes: 1 addition & 2 deletions src/cmd/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#![allow(clippy::let_unit_value)]

use anyhow::Result;
use clap::Subcommand;

mod benchmark;
Expand Down Expand Up @@ -29,7 +28,7 @@ pub enum MainCommand {
}

impl MainCommand {
pub async fn run(self) -> Result<()> {
pub async fn run(self) -> anyhow::Result<()> {
match self {
MainCommand::Benchmark(cmd) => cmd.run().await,
MainCommand::Broken(cmd) => cmd.run().await,
Expand Down
3 changes: 1 addition & 2 deletions src/cmd/separate.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::cmd::diff::compute_difference;
use anyhow::bail;
use anyhow::Result;
use clap::Args;
use clap::ValueEnum;
use photohash::Database;
Expand Down Expand Up @@ -61,7 +60,7 @@ impl FromStr for Mode {
}

impl Separate {
pub async fn run(mut self) -> Result<()> {
pub async fn run(mut self) -> anyhow::Result<()> {
// We need to canonicalize to correctly strip_prefix later.
self.src = self.src.canonicalize()?;
self.dests = self
Expand Down
Loading

0 comments on commit ecfd635

Please sign in to comment.