-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Alex Jones <[email protected]>
- Loading branch information
1 parent
a1762e2
commit bc74b11
Showing
6 changed files
with
112 additions
and
31 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
use std::error::Error; | ||
use std::io::Bytes; | ||
use crate::config::{Conf, save_config}; | ||
use base64::{Engine as _, engine::{self, general_purpose}, alphabet}; | ||
|
||
impl Conf { | ||
fn encode_cache_key(&self,raw_cache_key: &str) -> String { | ||
general_purpose::STANDARD.encode(raw_cache_key) | ||
} | ||
|
||
pub fn upsert_into_cache(mut self, raw_cache_key: &str, payload: &str) -> Result<(), Box<dyn Error>> { | ||
|
||
let encoded_key = self.encode_cache_key(raw_cache_key); | ||
let encoded_payload = self.encode_cache_key(payload); | ||
// append the new data to the hashmap | ||
self.stored_advice.insert(encoded_key, | ||
encoded_payload); | ||
|
||
// Save the config | ||
save_config(self)?; | ||
Ok(()) | ||
} | ||
pub fn fetch_from_cache(&self,raw_cache_key: &str) -> Option<String> { | ||
let encoded_key = self.encode_cache_key(raw_cache_key); | ||
let found_keys: Vec<&String> = self.stored_advice.iter().filter_map(|(k,v)| | ||
if k == encoded_key.as_str() {Some(v)} else {None}).collect(); | ||
|
||
// TODO: verify this | ||
if found_keys.is_empty() { | ||
return None | ||
} | ||
|
||
let mut found = String::new(); | ||
let decoded_bytes = match general_purpose::STANDARD.decode(found_keys.first().unwrap().to_string()) { | ||
Ok(bytes) => { | ||
let decoded_string = match String::from_utf8(bytes) { | ||
Ok(s) => found = s, | ||
Err(e) => { | ||
eprintln!("Error converting bytes to String: {}", e); | ||
} | ||
}; | ||
}, | ||
Err(e) => { | ||
eprintln!("Error decoding Base64: {}", e); | ||
} | ||
}; | ||
Some(found) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters