aboutsummaryrefslogtreecommitdiff
path: root/cache/src/helper.rs
diff options
context:
space:
mode:
Diffstat (limited to 'cache/src/helper.rs')
-rw-r--r--cache/src/helper.rs46
1 files changed, 46 insertions, 0 deletions
diff --git a/cache/src/helper.rs b/cache/src/helper.rs
new file mode 100644
index 0000000..8f73e1e
--- /dev/null
+++ b/cache/src/helper.rs
@@ -0,0 +1,46 @@
+/*
+ This file is part of jellything (https://codeberg.org/metamuffin/jellything)
+ which is licensed under the GNU Affero General Public License (version 3); see /COPYING.
+ Copyright (C) 2025 metamuffin <metamuffin.org>
+*/
+
+use base64::{Engine, prelude::BASE64_URL_SAFE};
+use sha2::Sha256;
+use std::{
+ fmt::Display,
+ hash::{Hash, Hasher},
+};
+
+pub struct HashKey<T>(pub T);
+impl<T: Hash> Display for HashKey<T> {
+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+ use sha2::Digest;
+ struct ShaHasher(Sha256);
+ impl Hasher for ShaHasher {
+ fn finish(&self) -> u64 {
+ unreachable!()
+ }
+ fn write(&mut self, bytes: &[u8]) {
+ self.0.update(bytes);
+ }
+ }
+ let mut d = ShaHasher(sha2::Sha256::new());
+ self.0.hash(&mut d);
+ let d = d.0.finalize();
+ f.write_str(&BASE64_URL_SAFE.encode(d))
+ }
+}
+
+pub struct EscapeKey<T>(pub T);
+impl<T: Display> Display for EscapeKey<T> {
+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+ write!(
+ f,
+ "{}",
+ percent_encoding::utf8_percent_encode(
+ &self.0.to_string(),
+ percent_encoding::NON_ALPHANUMERIC,
+ )
+ )
+ }
+}