From d3f53422df382a843c1bf5b23b6783ce5d6f179a Mon Sep 17 00:00:00 2001 From: Jeff Baskin Date: Sat, 24 Dec 2022 11:30:45 -0500 Subject: [PATCH] Refactored common filename function. --- src/morethantext/mod.rs | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/morethantext/mod.rs b/src/morethantext/mod.rs index e4223a9..40c83da 100644 --- a/src/morethantext/mod.rs +++ b/src/morethantext/mod.rs @@ -117,6 +117,11 @@ impl MoreThanText { }) } + fn filename(&self, id: &str) -> String { + let filename = Path::new(&self.dir).join(&id); + filename.into_os_string().into_string().unwrap() + } + async fn add_entry(&self, entry: CacheType) -> Result { let mut id: String = "".to_string(); let mut dup = true; @@ -124,7 +129,7 @@ impl MoreThanText { id = thread_rng().sample_iter(&Alphanumeric).take(32).collect(); dup = Path::new(&self.dir).join(&id).as_path().exists().await; } - match write(Path::new(&self.dir).join(&id), entry.to_bytes()).await { + match write(Path::new(&self.filename(&id)), entry.to_bytes()).await { Ok(_) => (), Err(err) => { let mut error = DBError::new("data write"); @@ -145,7 +150,7 @@ impl MoreThanText { entry.touch(); Ok(entry.clone()) } - None => match read(Path::new(&self.dir).join(&id)).await { + None => match read(Path::new(&self.filename(id))).await { Ok(content) => { let data = CacheEntry::new(CacheType::from_bytes(content)); cache.insert(id.to_string(), data.clone()); @@ -161,7 +166,7 @@ impl MoreThanText { Ok(_) => (), Err(err) => return Err(err), } - match write(Path::new(&self.dir).join(&id), entry.to_bytes()).await { + match write(Path::new(&self.filename(id)), entry.to_bytes()).await { Ok(_) => (), Err(err) => { let mut error = DBError::new("data write");