From 157790ff3452be04b6a14c1bec7aab821a4a8725 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20S=C3=B6rn=C3=A4s?= Date: Thu, 29 Jul 2021 00:29:40 +0200 Subject: hash posts --- cli/Cargo.lock | 23 +++++++++++++++++++++++ cli/Cargo.toml | 1 + cli/src/main.rs | 17 ++++++++++++++++- 3 files changed, 40 insertions(+), 1 deletion(-) diff --git a/cli/Cargo.lock b/cli/Cargo.lock index fb68e73..b69f6d6 100644 --- a/cli/Cargo.lock +++ b/cli/Cargo.lock @@ -14,6 +14,12 @@ version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a" +[[package]] +name = "cfg-if" +version = "0.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822" + [[package]] name = "itoa" version = "0.4.7" @@ -27,6 +33,7 @@ dependencies = [ "rust_decimal", "serde", "serde_json", + "twox-hash", ] [[package]] @@ -105,6 +112,12 @@ dependencies = [ "serde", ] +[[package]] +name = "static_assertions" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f" + [[package]] name = "syn" version = "1.0.74" @@ -116,6 +129,16 @@ dependencies = [ "unicode-xid", ] +[[package]] +name = "twox-hash" +version = "1.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "04f8ab788026715fa63b31960869617cba39117e520eb415b0139543e325ab59" +dependencies = [ + "cfg-if", + "static_assertions", +] + [[package]] name = "unicode-xid" version = "0.2.2" diff --git a/cli/Cargo.toml b/cli/Cargo.toml index 1535896..bdaa3e3 100644 --- a/cli/Cargo.toml +++ b/cli/Cargo.toml @@ -9,3 +9,4 @@ edition = "2018" rust_decimal = { version = "1.15.0", features = ["serde-arbitrary-precision"] } serde = { version = "1.0.126", features = ["derive"] } serde_json = "1.0.64" +twox-hash = { version = "1.6.0", default-features = false } diff --git a/cli/src/main.rs b/cli/src/main.rs index 9ab5a55..656107d 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -2,12 +2,15 @@ use rust_decimal::Decimal; use serde::{Deserialize, Serialize}; use std::convert::{AsRef, TryFrom}; use std::fs; +use std::hash::{Hash, Hasher}; use std::path::{Path, PathBuf}; +use twox_hash::XxHash64; type Account = String; type Category = String; #[derive(Debug)] +#[derive(Hash)] #[derive(Deserialize, Serialize)] enum TransactionKind { Expense, @@ -16,6 +19,7 @@ enum TransactionKind { } #[derive(Debug)] +#[derive(Hash)] #[derive(Deserialize, Serialize)] struct Transaction { description: String, @@ -27,6 +31,7 @@ struct Transaction { } #[derive(Debug)] +#[derive(Hash)] #[derive(Deserialize, Serialize)] struct Post { transaction: Transaction, @@ -63,6 +68,15 @@ impl Store { } Some(res) } + + fn write(&self) { + for post in &self.posts { + let mut h = XxHash64::default(); + post.hash(&mut h); + let file = h.finish(); + println!("{}: {:?}", file, post); + } + } } impl Post { @@ -94,5 +108,6 @@ fn main() { // }); // post.write(&"test").unwrap(); let store = Store::open(PathBuf::from("store")).unwrap(); - println!("{:#?}", store); + // println!("{:#?}", store); + store.write(); } -- cgit v1.2.1