-
-
Notifications
You must be signed in to change notification settings - Fork 176
Expand file tree
/
Copy pathurl_encode.rs
More file actions
25 lines (24 loc) · 934 Bytes
/
Copy pathurl_encode.rs
File metadata and controls
25 lines (24 loc) · 934 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
use std::borrow::Cow;
/// escapes a string for use in a URL using percent encoding
/// for example, spaces are replaced with %20, '/' with %2F, etc.
/// This is useful for constructing URLs in SQL queries.
/// If this function is passed a NULL value, it will return NULL (None in Rust),
/// rather than an empty string or an error.
pub(super) async fn url_encode(raw_text: Option<Cow<'_, str>>) -> Option<Cow<'_, str>> {
Some(match raw_text? {
Cow::Borrowed(inner) => {
let encoded = percent_encoding::percent_encode(
inner.as_bytes(),
percent_encoding::NON_ALPHANUMERIC,
);
encoded.into()
}
Cow::Owned(inner) => {
let encoded = percent_encoding::percent_encode(
inner.as_bytes(),
percent_encoding::NON_ALPHANUMERIC,
);
Cow::Owned(encoded.collect())
}
})
}