From fbbf4d617395774e77098fd0a10aad7d0b619a2e Mon Sep 17 00:00:00 2001 From: "Jakob L. Kreuze" Date: Mon, 18 May 2020 11:32:34 -0400 Subject: Refactor `import_image`. Use `Statement::insert` and the `bail` macro. --- src/main.rs | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/src/main.rs b/src/main.rs index 2739f42..e7febe4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -51,9 +51,9 @@ impl TagBase { } /// Insert the image at `path` into the data. - fn import_image(&self, path: &Path) -> Result { + fn import_image(&self, path: &Path) -> Result { if path.is_dir() { - return Err(anyhow!("`path` does not name a file.")); + bail!("`path` does not name a file."); } let mut file = std::fs::File::open(path)?; @@ -69,17 +69,11 @@ impl TagBase { let split_index = path.len() - file_name.len(); let parent_directory = &path[0..split_index]; - self.conn.execute( - "INSERT INTO images (blake2, filename, orig_dir) VALUES(?,?,?)", - params![hash, file_name, parent_directory], - )?; - - let id: Vec> = self + let id = self .conn - .prepare("SELECT id FROM images WHERE blake2 = ?;")? - .query_and_then(params![hash], |row| row.get(0))? - .collect(); - Ok(*id[0].as_ref().unwrap()) + .prepare("INSERT INTO images (blake2, filename, orig_dir) VALUES(?,?,?)")? + .insert(params![hash, file_name, parent_directory])?; + Ok(id) } /// Insert `tag` into the database, if it does not already exist. -- cgit v1.3