Refactored common filename function.

This commit is contained in:
Jeff Baskin 2022-12-24 11:30:45 -05:00
parent fe29841ffe
commit d3f53422df

View File

@ -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<String, DBError> {
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");