diff options
author | Ben Morrison <ben@gbmor.dev> | 2020-06-16 19:00:08 -0400 |
---|---|---|
committer | Ben Morrison <ben@gbmor.dev> | 2020-06-16 19:00:08 -0400 |
commit | c4c9412c3f459323f5acd91d4654d7ddae1526f0 (patch) | |
tree | adc72eadab449929e597f0a681b2c96bd4022df2 /src | |
parent | d033f0d23934b2d578ca9ef5aaf6ba1cf51aef00 (diff) | |
download | clinte-c4c9412c3f459323f5acd91d4654d7ddae1526f0.tar.gz |
recording hash of posts file in homedir
sha256. This will facilitate checking for new posts on login.
Diffstat (limited to 'src')
-rw-r--r-- | src/db.rs | 17 | ||||
-rw-r--r-- | src/posts.rs | 2 |
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; |