about summary refs log tree commit diff stats
path: root/src/db.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/db.rs')
-rw-r--r--src/db.rs17
1 files changed, 17 insertions, 0 deletions
diff --git a/src/db.rs b/src/db.rs
index 20ad5a1..c0e4cda 100644
--- a/src/db.rs
+++ b/src/db.rs
@@ -1,5 +1,6 @@
 use fd_lock::FdLock;
 use serde::{Deserialize, Serialize};
+use sha2::{Sha256, Digest};
 
 use std::fs;
 use std::fs::File;
@@ -58,6 +59,22 @@ impl Posts {
         out
     }
 
+    // Hash the contents of clinte.json and store it in your
+    // home directory. Useful for determining if there are
+    // new posts.
+    pub fn hash(&self) -> error::Result<()> {
+        let body = error::helper(serde_json::to_string_pretty(&self), "Couldn't reserialize json to record hash");
+        let mut hasher = Sha256::new();
+        hasher.update(body);
+        let hash = format!("{:x}", hasher.finalize());
+    
+        let homedir = std::env::var("HOME")?;
+        let writepath = format!("{}/.clinte.sha256", homedir);
+        fs::write(writepath, &hash)?;
+
+        Ok(())
+    }
+
     pub fn replace(&mut self, n: usize, post: Post) {
         self.posts[n] = post;
     }