crier

Unnamed repository; edit this file 'description' to name the repository.
Info | Log | Files | Refs

commit 3f5a0515225f4a5b781e625aad35c82af07bc0a2
parent 20fa2e3e3ab61f5c3167c8e5a38582c69f7525e7
Author: lash <dev@holbrook.no>
Date:   Sun, 30 Jun 2024 14:35:13 +0100

Add unit test, digest and add

Diffstat:
Msrc/lib.rs | 67+++++++++++++++++++++++++++++++++++++++++++------------------------
1 file changed, 43 insertions(+), 24 deletions(-)

diff --git a/src/lib.rs b/src/lib.rs @@ -12,8 +12,8 @@ pub struct Sequencer { } pub struct SequencerEntry { + pub digest: u32, entry: Entry, - digest: u64, } impl Sequencer { @@ -25,51 +25,70 @@ impl Sequencer { } } - pub fn add<H: Hash>(&self, entry: SequencerEntry) { -// entry.hash(&self, entry); + pub fn add(&mut self, entry: Entry) -> bool { + let o = SequencerEntry::new(entry); + if self.items.contains_key(&o.digest) { + return false; + } + self.items.insert(o.digest, o.into()); + return true; + } +} + +impl Iterator for Sequencer { + type Item = Vec<u8>; + + fn next(&mut self) -> Option<Self::Item> { + let c: u32; + + c = self.item_keys[self.crsr]; + return Some(self.items[&c].clone()); } } impl SequencerEntry { - pub fn new(entry: Entry, summer: &mut Sha512Hasher) -> SequencerEntry { + pub fn new(entry: Entry) -> SequencerEntry { let mut o = SequencerEntry { entry: entry, digest: 0, }; - o.hash(summer); - o.digest = summer.finish(); + let mut h = Sha512Hasher::default(); + o.hash(&mut h); + o.digest = h.finish() as u32; o } } -impl Hash for SequencerEntry { - fn hash<H: Hasher>(&self, h: &mut H) { - h.write(self.entry.id.as_bytes()); +impl Into<Vec<u8>> for SequencerEntry { + fn into(self) -> Vec<u8> { + return String::from(self.entry.id).into_bytes(); } } - -impl Iterator for Sequencer { - type Item = Vec<u8>; - - fn next(&mut self) -> Option<Self::Item> { - let c: u32; - - c = self.item_keys[self.crsr]; - return Some(self.items[&c].clone()); +impl Hash for SequencerEntry { + fn hash<H: Hasher>(&self, h: &mut H) { + h.write(self.entry.id.as_bytes()); } } #[cfg(test)] mod tests { - use rs_sha512::Sha512Hasher; - use super::SequencerEntry; + use super::Sequencer; use feed_rs::model::Entry; #[test] - fn test_entry() { - let mut h = Sha512Hasher::default(); - let src = Entry::default(); - let entry = SequencerEntry::new(src, &mut h); + fn test_entry_guard() { + let mut r: bool; + let mut seq = Sequencer::new(); + let mut src = Entry::default(); + src.id = String::from("foo"); + r = seq.add(src); + assert!(r); + + + let mut src_two = Entry::default(); + src_two.id = String::from("foo"); + r = seq.add(src_two); + assert!(!r); } }