diff options
| author | Jakob L. Kreuze <zerodaysfordays@sdf.org> | 2020-05-18 16:21:21 -0400 |
|---|---|---|
| committer | Jakob L. Kreuze <zerodaysfordays@sdf.org> | 2020-05-18 16:21:21 -0400 |
| commit | 23a9ff86fbea94a6ba6eea28c19139728600ecca (patch) | |
| tree | e8f36e5714fb74e9e0c5ad96494923628fe158c6 | |
| parent | 5c1e5261cefaa65596ff659b8b66d854db58a7e6 (diff) | |
Factor out file hashing.
| -rw-r--r-- | src/main.rs | 13 |
1 files changed, 8 insertions, 5 deletions
diff --git a/src/main.rs b/src/main.rs index b2b65cd..b5c0d93 100644 --- a/src/main.rs +++ b/src/main.rs @@ -20,6 +20,13 @@ struct TagBase { conn: Connection, } +fn hash_file(path: &Path) -> Result<String> { + let mut file = std::fs::File::open(path)?; + let mut hasher = Blake2b::new(); + let _ = io::copy(&mut file, &mut hasher)?; + Ok(base64::encode(&hasher.result())) +} + impl TagBase { /// Instantiate a new `TagBase` whose backing database is at `path`. pub fn new(path: &str) -> Result<Self> { @@ -72,11 +79,7 @@ impl TagBase { bail!("`path` does not name a file."); } - let mut file = std::fs::File::open(path)?; - let mut hasher = Blake2b::new(); - let _ = io::copy(&mut file, &mut hasher)?; - let hash = base64::encode(&hasher.result()); - + let hash = hash_file(path)?; let file_name = path .file_name() .and_then(|os| os.to_str()) |