diff options
Diffstat (limited to 'src/main.rs')
| -rw-r--r-- | src/main.rs | 28 |
1 files changed, 25 insertions, 3 deletions
diff --git a/src/main.rs b/src/main.rs index 2e80a25..989262f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -100,8 +100,7 @@ impl TagBase { Ok(()) } - /// Insert the image at `path` into the data. - fn import_image(&self, path: &Path, hash: &str) -> Result<i64> { + fn split_path(&self, path: &Path) -> Result<(String, String)> { if path.is_dir() { bail!("`path` does not name a file."); } @@ -111,9 +110,15 @@ impl TagBase { .and_then(|os| os.to_str()) .ok_or(anyhow!("Couldn't parse file name."))?; let path = path.to_str().ok_or(anyhow!("Couldn't parse path."))?; - let split_index = path.len() - file_name.len(); let parent_directory = &path[0..split_index]; + + Ok((file_name.to_string(), parent_directory.to_string())) + } + + /// Insert the image at `path` into the data. + fn import_image(&self, path: &Path, hash: &str) -> Result<i64> { + let (file_name, parent_directory) = self.split_path(path)?; let id = self .conn .prepare("INSERT INTO images (blake2, filename, orig_dir) VALUES(?,?,?)")? @@ -134,6 +139,15 @@ impl TagBase { Ok(()) } + /// Return the internal id for `path`. + fn image_id(&self, path: &Path) -> Result<i64> { + let (file_name, path) = self.split_path(path)?; + Ok(self + .conn + .prepare("SELECT * FROM images WHERE orig_dir = ? AND filename = ?")? + .query_row(params![path, file_name], |row| Ok(row.get::<_, i64>(0)))??) + } + /// Return the internal id for `tag`. fn tag_id(&self, tag: &str) -> Result<i64> { Ok(self @@ -360,6 +374,14 @@ fn main() { tb.tag_image(id, arg).unwrap(); } } + "id_for" => { + if args.len() < 3 { + eprintln!("usage: {} id_for PATH ", args[0]); + std::process::exit(1); + } + let path = Path::new(&args[2]); + println!("{}", tb.image_id(&path).unwrap()); + } "add_tags" => { if args.len() < 3 { eprintln!("usage: {} add_tags ID [TAGS ...]", args[0]); |