From 8db7a6d9bfc31ceb32e9fdf3162c9237c79502d0 Mon Sep 17 00:00:00 2001 From: Jeff Baskin Date: Sun, 11 Dec 2022 09:34:07 -0500 Subject: [PATCH] Turn cache entry into writable bytes. --- src/morethantext/mod.rs | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/src/morethantext/mod.rs b/src/morethantext/mod.rs index b359bf0..957ac80 100644 --- a/src/morethantext/mod.rs +++ b/src/morethantext/mod.rs @@ -19,9 +19,18 @@ enum CacheEntry { impl CacheEntry { fn entry_type(&self) -> String { match self { - CacheEntry::Raw(_) => "raw".to_string(), + CacheEntry::Raw(_) => "Raw".to_string(), } } + + fn to_bytes(&self) -> Vec { + let mut output = self.entry_type().into_bytes(); + output.push(0); + match self { + CacheEntry::Raw(s) => output.append(&mut s.as_bytes().to_vec()), + } + return output; + } } impl fmt::Display for CacheEntry { @@ -214,8 +223,19 @@ mod cache_entry { use super::*; #[test] - fn raw_type() { + fn raw_get_type() { let holder = CacheEntry::Raw("nothing important".to_string()); - assert_eq!(holder.entry_type(), "raw"); + assert_eq!(holder.entry_type(), "Raw"); + } + + #[test] + fn raw_get_bytes() { + let data = "addams"; + let holder = CacheEntry::Raw(data.to_string()); + let mut expected = holder.entry_type().into_bytes(); + expected.push(0); + expected.append(&mut data.as_bytes().to_vec()); + let output = holder.to_bytes(); + assert_eq!(output, expected); } }