summaryrefslogtreecommitdiff
path: root/src/main.rs
diff options
context:
space:
mode:
authorJakob L. Kreuze <zerodaysfordays@sdf.org>2020-05-18 11:32:34 -0400
committerJakob L. Kreuze <zerodaysfordays@sdf.org>2020-05-18 11:59:22 -0400
commitfbbf4d617395774e77098fd0a10aad7d0b619a2e (patch)
tree4d31176d26ea656e5369021c8a79ba7d02c77768 /src/main.rs
parent57f2f9f21a82a8b13c544d62d353894eb3e0a4c7 (diff)
Refactor `import_image`.
Use `Statement::insert` and the `bail` macro.
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs18
1 files 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<i32> {
+ fn import_image(&self, path: &Path) -> Result<i64> {
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<std::result::Result<i32, rusqlite::Error>> = 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.