about summary refs log tree commit diff stats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/db.rs17
-rw-r--r--src/posts.rs2
2 files changed, 18 insertions, 1 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;
     }
diff --git a/src/posts.rs b/src/posts.rs
index c44e0f5..0de3d52 100644
--- a/src/posts.rs
+++ b/src/posts.rs
@@ -83,6 +83,7 @@ pub fn display() -> error::Result<()> {
         .unwrap_or_else(|_| 80);
 
     let all = db::Posts::get_all(db::PATH);
+    all.hash()?;
 
     let mut postvec = Vec::new();
     all.posts().iter().enumerate().for_each(|(id, post)| {
@@ -90,7 +91,6 @@ pub fn display() -> error::Result<()> {
             .body
             .trim()
             .chars()
-            .into_iter()
             .enumerate()
             .map(|(i, e)| {
                 let i = i + 1;