From 23a9ff86fbea94a6ba6eea28c19139728600ecca Mon Sep 17 00:00:00 2001 From: "Jakob L. Kreuze" Date: Mon, 18 May 2020 16:21:21 -0400 Subject: Factor out file hashing. --- src/main.rs | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) (limited to 'src') 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 { + 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 { @@ -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()) -- cgit v1.3