|
|
@@ -0,0 +1,31 @@
|
|
|
+use base64::engine::general_purpose::URL_SAFE_NO_PAD;
|
|
|
+use base64::Engine;
|
|
|
+use flate2::write::DeflateEncoder;
|
|
|
+use flate2::Compression;
|
|
|
+use std::io::prelude::*;
|
|
|
+
|
|
|
+/// Compress string and encode in a URL safe form
|
|
|
+fn compress_string(s: &str) -> String {
|
|
|
+ // Convert string to bytes
|
|
|
+ let bytes = s.as_bytes();
|
|
|
+
|
|
|
+ // Compress the bytes using deflate
|
|
|
+ let mut encoder = DeflateEncoder::new(Vec::new(), Compression::default());
|
|
|
+ encoder.write_all(bytes).expect("Failed to write bytes");
|
|
|
+ let compressed_bytes = encoder.finish().expect("Failed to compress bytes");
|
|
|
+
|
|
|
+ // Encode the compressed bytes to base64
|
|
|
+ URL_SAFE_NO_PAD.encode(compressed_bytes)
|
|
|
+}
|
|
|
+
|
|
|
+#[cfg(test)]
|
|
|
+mod tests {
|
|
|
+ use super::*;
|
|
|
+
|
|
|
+ #[test]
|
|
|
+ fn it_works() {
|
|
|
+ let input_str = "your_string_here";
|
|
|
+ let compressed_str = compress_string(input_str);
|
|
|
+ println!("Compressed and URL-safe encoded string: {}", compressed_str);
|
|
|
+ }
|
|
|
+}
|