aboutsummaryrefslogtreecommitdiff
path: root/src/api/common/encoding.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/api/common/encoding.rs')
-rw-r--r--src/api/common/encoding.rs22
1 files changed, 22 insertions, 0 deletions
diff --git a/src/api/common/encoding.rs b/src/api/common/encoding.rs
new file mode 100644
index 00000000..e286a784
--- /dev/null
+++ b/src/api/common/encoding.rs
@@ -0,0 +1,22 @@
+//! Module containing various helpers for encoding
+
+/// Encode &str for use in a URI
+pub fn uri_encode(string: &str, encode_slash: bool) -> String {
+ let mut result = String::with_capacity(string.len() * 2);
+ for c in string.chars() {
+ match c {
+ 'a'..='z' | 'A'..='Z' | '0'..='9' | '_' | '-' | '~' | '.' => result.push(c),
+ '/' if encode_slash => result.push_str("%2F"),
+ '/' if !encode_slash => result.push('/'),
+ _ => {
+ result.push_str(
+ &format!("{}", c)
+ .bytes()
+ .map(|b| format!("%{:02X}", b))
+ .collect::<String>(),
+ );
+ }
+ }
+ }
+ result
+}